进度栏c#-加载过程类似

问题描述 投票:0回答:2

您好,我有一个需要花费一些时间才能加载的函数。这就是为什么我计划在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,但它并不像加载屏幕一样等待特定的功能或事件完成。

***我的clickclick事件完成其所有功能后,我的进度栏不会结束。

我创建了:

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

c# winforms progress-bar backgroundworker
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;
    }
}

0
投票

是的,你应该。非常简单。

使后台工作人员执行btnProcess_Click的工作。

已报告进度:

worker.WorkerReportsProgress = true;

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

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

这样做时,您可以创建一个进度条,然后可以根据由计算触发的此worker_ProgressChanged事件来更新自身。

仅通过Google搜索,您可以找到很多实现方法。祝您好运,希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.