0

ボタンのクリック後にこれを呼び出します:

FORM1-コード:

{

   ProgressBar.Maximum = 500;    
   myArguments gifargs = new myArguments();    //class for passing arguments to the bgW    
   gifargs.InputFilePath = listBox1.SelectedItem.ToString(); // input filepath    
   gifargs.OutputFilePath = saveFileDialog1.FileName;        //output filepath    
   backgroundWorker1.RunWorkerAsync(gifargs);                // run bgW async with args   
}


// here is bgW doWork
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)   
{   
myArguments args = e.Argument as myArguments; //myArguments class
if (backgroundWorker1.CancellationPending)    
{
  e.Cancel = true;
}
PictureHandler makeAnimatedGIf = new PictureHandler(); // creating new object    
makeAnimatedGIf.imageGif(args.InputFilePath,args.OutputFilePath); //call method with args

makeAnimatedGIf.GifProgress += new PictureHandler.myprogressgetter(this.GifProgressF1);

//add the delegate

ここまで完璧に動作します

これは、bgW.ReportProgress を更新する必要がある私の Callback 関数です。

しかし、それはそこに到達しませんか?

private void GifProgressF1(int i)
{
             System.Threading.Thread.Sleep(500);
            backgroundWorker1.ReportProgress(i);
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
            ProgressBar.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           ProgressBar.Value = 0;
            if (e.Cancelled)
            {
                MessageBox.Show("Process canceled!");
            }
            else
            {
                MessageBox.Show("Process complete!");
            }

        }

<----Picturehandler.cs-コード----->

//Picturehandler クラスのデリゲート定義

public delegate void myprogressgetter(int i);

public myprogressgetter GifProgress;








public void imageGif(string input, string output)
    {
        Process imagemagick = new Process();
        imagemagick.StartInfo.FileName = "convert.exe";
        imagemagick.StartInfo.Arguments = "-monitor -delay 1 " + input + " +map " + output;
        imagemagick.EnableRaisingEvents = false;
        imagemagick.StartInfo.UseShellExecute = false;
        imagemagick.StartInfo.CreateNoWindow = true;
        imagemagick.StartInfo.RedirectStandardOutput = true;
        imagemagick.StartInfo.RedirectStandardError = true;
        imagemagick.Start();
        StreamReader ima = imagemagick.StandardError;
        bool assign2 = false;

        do
        {
            string consolausgabe = ima.ReadLine();
            if (consolausgabe.Contains("Reduce") == true)
            {
                assign2 = true;
            }
            gifprocess(consolausgabe, assign2);

        } while (!ima.EndOfStream);
        imagemagick.WaitForExit();
        imagemagick.Close();
    }

 private void gifprocess(string cline, bool zähl)
    {
        if (cline.Contains("Load"))
        {
            string a1 = cline;
            string[] a11 = a1.Split(new char[] { ':', ',' });
            string a12 = a11[3];
            string[] a13 = a12.Split(new char[] { '%' });
            int load1 = Convert.ToInt32(a13[0]);
            GifProgress(load1;)  //<<<<<------------- this will give me an exception
            // Visual Studio says GifProgress = null in Autos

}

GifProgress(100)または他の整数を呼び出すと、例外(オブジェクト参照がオブジェクトのインスタンスに設定されていません)が発生し、プログレスバーは更新されません。

picturehandler クラスからの進捗情報が UI に表示されません。2 日間試してみました。

同じコードを使用して form2 から textbox.text を取得すると、コールバック関数が正常に機能します。WorkerReportProgress = TRUE。

4

4 に答える 4

1

通常、DoWorkメソッドにはループが含まれます。ループの各反復は への呼び出しで終了する可能性がReportProgressあり、これにより inProgressChangedが実行されます。

数行のコードを実行しているだけなので、 を使用しRunWorkerCompletedて進行状況インジケーターを設定し、ReportProgress完全に忘れてください。

BackgroundWorker をよりよく理解するために使用したチュートリアルを次に示します。

バックグラウンドワーカースレッドが終了する前に終了するため、現在行っていることを行っている場合を除きますmakeAnimatedGIf.imageGif...

于 2012-06-21T20:28:42.417 に答える
0

必ずWorkerReportsProgressをtrueに設定し、Invoke()を使用して別のスレッドからプログレスバーを更新してください。

于 2012-06-21T20:28:13.007 に答える
0

BackgroundWorker の WorkerReportsProgress プロパティが true に設定されていることを確認してください。デフォルトは false

于 2012-06-21T20:25:43.963 に答える
0

呼び出されると予想される関数はGifProgressF1、'PictureHandler' クラスのインスタンスのコールバックとしてのみ参照されます。このコールバックがそのクラスまで完全にどこでどのように呼び出されるか。ただし、あなたの説明から、このクラスがどこから来たのか、何をするのかは明確ではありません。クラスのドキュメントまたはソース コードを参照して、このコールバックがいつ呼び出され、そこから移動するかを正確に確認することをお勧めします。

于 2012-06-21T20:30:42.583 に答える