方法执行代码时显示进度表的最佳方法? [英] Best way to display a progress form while a method is executing code?

查看:49
本文介绍了方法执行代码时显示进度表的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForm加载方法,该方法需要很长时间才能收集一些数据以显示给用户.

I have a WinForm load method that takes a long time to gather some data to display to the user.

在执行此方法时,我用"Loading"字样显示一个带有大字体的表单.

I display a form with a large font with the word "Loading" while this method is executing.

但是有时会出现此错误,并且"Loading"进度表没有关闭,然后最终我的整个应用程序将退出:

However sometimes this error comes up and the "Loading" progress form does not close and then eventually my whole application will just exit:

创建窗口句柄时出错.在System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)

在以load方法执行代码时,是否有更好的方法显示进度/加载表单?

这是我的代码:

Is there a better way to display my progress/loading form while I am executing code in the load method?

This is my code:

//I launch a thread here so that way the Progress_form will display to the user
//while the Load method is still executing code.  I can not use .ShowDialog here
//or it will block.

//Progress_form displays the "Loading" form    
Thread t = new Thread(new ThreadStart(Progress_form));  

t.SetApartmentState(System.Threading.ApartmentState.STA);
t.IsBackground = true;
t.Start();

//This is where all the code is that gets the data from the database.  This could
//take upwards of 20+ seconds.

//Now I want to close the form because I am at the end of the Load Method                         

try
{
   //abort the Progress_form thread (close the form)
   t.Abort();
   //t.Interrupt();
}
catch (Exception)
{
}

推荐答案

A <代码> BackgroundWorker 是执行长时间运行的操作而不锁定UI线程的好方法.

A BackgroundWorker is a great way to perform a long running operation without locking the UI thread.

使用以下代码启动BackgroundWorker并显示加载表单.

Use the following code to start a BackgroundWorker and display a loading form.

// Configure a BackgroundWorker to perform your long running operation.
BackgroundWorker bg = new BackgroundWorker()
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);

// Start the worker.
bg.RunWorkerAsync();

// Display the loading form.
loadingForm = new loadingForm();
loadingForm.ShowDialog();

这将导致在后台线程上执行以下方法.请注意,您无法通过此线程操作UI.尝试这样做将导致异常.

This will cause the following method to be executed on a background thread. Note that you cannot manipulate the UI from this thread. Attempting to do so will result in an exception.

private void bg_DoWork(object sender, DoWorkEventArgs e)
{
    // Perform your long running operation here.
    // If you need to pass results on to the next
    // stage you can do so by assigning a value
    // to e.Result.
}

长时间运行的操作完成后,将在UI线程上调用此方法.现在,您可以安全地更新任何UI控件.在您的示例中,您将要关闭加载表单.

When the long running operation completes, this method will be called on the UI thread. You can now safely update any UI controls. In your example, you would want to close the loading form.

private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Retrieve the result pass from bg_DoWork() if any.
    // Note, you may need to cast it to the desired data type.
    object result = e.Result;

    // Close the loading form.
    loadingForm.Close();

    // Update any other UI controls that may need to be updated.
}

这篇关于方法执行代码时显示进度表的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆