1

プログラムについて少し助けが必要です。メイン スレッド (フォーム) が新しいプロセスを開始し、プロセスの終了を待機するときに、プログレス バーをマーキー モードで表示しようとしています。これは、TEX ファイルをコンパイルするために pdflatex を開始し、プロセスの WaitForExit() メソッドが完了するまで進行状況バーに新しいフォームを表示することを意味します。そして、私がそれを正しい方法で行っているのか、それとも別のより良い方法があるのか​​ を知る必要があります. Form を拡張し、新しいスレッドに使用される Progress というクラスがあります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace Measuring
{
public class Progress : Form
{
    public Progress(Form form)
    {
        InitializeComponent(form);
    }

    private void InitializeComponent(Form parent)
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.Font = parent.Font;
        this.Size = new Size(300, 40);
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(parent.Left + ((parent.Width - this.Width) / 2), (parent.Top + ((parent.Height - this.Height) / 2)));

        ProgressBar progressbar = new ProgressBar();
        Label label = new Label();

        label.AutoSize = true;
        label.Text = "Converting file to pdf";
        label.Dock = DockStyle.Top;          

        progressbar.Dock = DockStyle.Bottom;
        progressbar.Maximum = 100;
        progressbar.Minimum = 0;

        progressbar.ForeColor = Color.Green;
        progressbar.Style = ProgressBarStyle.Marquee;
        progressbar.MarqueeAnimationSpeed = 10;


        this.Controls.Add(label);
        this.Controls.Add(progressbar);
    }

    public void Start()
    {
        this.ShowDialog();
    }

    public void Stop()
    {
        this.Close();
    }
}
}

これで、最後にこれを呼び出す streamwrite メソッドがあります。

        Process p = new Process();
        p.StartInfo.FileName = "pdflatex";
        p.StartInfo.Arguments = save.FileName;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        Progress prg = new Progress(this);
        Thread t = new Thread(prg.Start);

        try
        {

            if (p.Start())
            {
                t.Start();
                p.WaitForExit();
                prg.Stop();
                if (p.ExitCode != 0)
                {
                    MessageBox.Show("Conversion to pdf using LaTeX failed!" + Environment.NewLine + "No output file produced.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        catch
        {
            MessageBox.Show("Pdflatex is not installed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

大丈夫そうに見えますが、本当に安全かどうかはわかりません。すべての例外が処理されるわけではないことはわかっていますが、主なものは Start() および Stop() メソッドです。

どうもありがとう。

4

1 に答える 1