开发者

Multi Thread program

Basic Goal is that i have four progress bar and want to run them at once as button is pressed and i donot have to use background worker have to do by this.

 var t = new Thread(() =>
            {
                try
                {

                }
        });
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

I tried and codded

 for (i = 0; i < 4; i++)
            {
                var t = new Thread(() =>
                {
                    for (double x = 0; x &l开发者_C百科t; 10000; x = x + 0.5)
                    {
                        progressVal=(int)x;
                        this.Invoke(new EventHandler(ProgressBar));
                        Thread.Sleep(2);

                    }
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
            }

 private void ProgressBar(object sender, EventArgs e)
        {
            progressBar1.Value=progressVal;
        }

but cannot think of idea how to manuplate other progress bars


I would put the progress bars into an array:

var pBars = new[] { progressBar1, progressBar2, progressBar3, progressBar4 };
foreach (var pBar in pBars)
{
    new Thread(currentPBar => 
    {
        for (double x = 0; x < 10000; x = x + 0.5)
        {             
            var progress = (int)x;
            Action<ProgressBar, int> del = UpdateProgress;
            Invoke(
                del, 
                new object[] { (ProgressBar)currentPBar, progress }
            );
            Thread.Sleep(2);
        }            
    }).Start(pBar);
}

and the UpdateProgress method:

private void UpdateProgress(ProgressBar pBar, int progress)
{
    pBar.Value = progress;
}

This being said, using a BackgroundWorker is far more adapted to your scenario.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜