0

您好,我有一个需要时间加载的功能。这就是为什么我打算在我的 winform 上放置一个进度条,以便用户知道我的程序仍在运行。但是,我不知道我将如何解决它。有没有人可以帮助指导我。

这是我打算做的事情:

 private void btnProcess_Click(object sender, EventArgs e)
        {
          //function which takes time because it contacts to a server
        }

我想要一个进度条,在我的过程完成后递增并结束。我应该为此使用后台工作人员吗?

***我已按照本教程http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo进行操作,但它不会像加载屏幕一样等待特定功能或事件完成。

***在我的 buttonclick 事件执行完所有功能后,我的进度条没有结束。

我创建了:

private void myBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                myBackgroundWorker.ReportProgress(i);
                System.Threading.Thread.Sleep(100);
            }
        }

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

 private void btnProcess_Click(object sender, EventArgs e)
            {
              myBackgroundWorker.RunWorkerAsync();
              //function which takes time because it contacts to a server
            }

我怎么知道我的 buttonclick 事件何时结束?这样我的进度条也会结束吗?

4

2 回答 2

0

这是两个非常好的例子

http://www.dreamincode.net/forums/topic/112547-using-the-backgroundworker-in-c%23/

http://www.dreamincode.net/forums/topic/246911-c%23-multi-threading-in-a-gui-environment/

希望有帮助

编辑:

public partial class Form1 : Form
{
    //--------------------------------------------------------------------------
    public Form1()
    {
        InitializeComponent();

        //Initialize backgroundworker
        Shown += new EventHandler(Form1_Shown);
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged +=
        new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

        //counter
        YourObject.Counter = 100;
    }

    //--------------------------------------------------------------------------
    void btnClick_Clicked(object sender, EventArgs e)
    {
        //Trigger the background process
        backgroundWorker1.RunWorkerAsync();
    }

    //--------------------------------------------------------------------------
    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        //Your freakishly long process,
        //which needs to be run in the background

        //to make it simple, I'll just do a for loop
        for (int i  = 0; i < 100; i++)
        {
            //The counter will keep track of your process
            //In your case, it might not be a for loop
            //So you will have to decide how to keep track of this counter
            //My suggestion is to decrease/increase this counter
            //right after importants action of your process
            backgroundWorker1.ReportProgress(YourObject.Counter--);
        }
    }

    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        //The counter is updated and displayed with a progress bar 
        YourObject.Counter = e.ProgressPercentage;
    }
}
于 2013-05-02T02:36:34.053 回答
0

是的,你应该。这很简单。

制作一个执行任何工作的后台工作人员btnProcess_Click

让它报告进度:

worker.WorkerReportsProgress = true;

现在,您可以通过订阅的事件触发此进度报告。

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

这样做时,您可以创建一个进度条,该进度条可以基于此worker_ProgressChanged事件自行更新,由您的计算触发。

你可以通过谷歌搜索找到大量的实现。祝你好运,希望这会有帮助。

于 2013-05-02T02:27:22.713 回答