进度条加载类似进程

本文关键字:进程 加载 | 更新日期: 2023-09-27 18:11:49

你好,我有一个需要时间加载的函数。这就是为什么我打算在我的窗体上放一个进度条,这样用户就会知道我的程序还在运行。然而,我不知道如何解决它。这里有人能给我指路吗?

我打算这样做:

 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事件什么时候结束?这样我的进度条也会结束吗?

进度条加载类似进程

这是两个很好的例子

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;
    }
}

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

创建一个后台worker,执行btnProcess_Click所做的任何工作。

让它报告进度:

worker.WorkerReportsProgress = true;

现在您可以让进度报告由您订阅的事件触发。

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

这样做,你可以创建一个进度条,它可以根据这个worker_ProgressChanged事件更新自己,由你的计算触发。

你可以通过谷歌找到很多这样的实现。祝你好运,希望这对你有帮助。