进度条c# - 加载类似进程

时间:2013-05-02 02:23:48

标签: c# winforms progress-bar backgroundworker

您好我有一个需要时间加载的功能。这就是为什么我打算在我的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活动什么时候结束?这样我的进度条也会结束?

2 个答案:

答案 0 :(得分:0)

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

让后台工作人员执行btnProcess_Click所做的任何工作。

报告进度:

worker.WorkerReportsProgress = true;

现在您可以通过您订阅的活动触发此进度报告。

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

这样,您可以创建一个进度条,而不是根据计算触发的worker_ProgressChanged事件自行更新。

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

答案 1 :(得分: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;
    }
}