WinForm应用程序UI挂起长时间运行的操作过程中 [英] WinForm Application UI Hangs during Long-Running Operation

查看:116
本文介绍了WinForm应用程序UI挂起长时间运行的操作过程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序
关于这一点我需要使用一个for循环有2000年左右大量的远程调用 - 3000来电,

I have a windows forms application on which I need to use a for loop having a large number of Remote Calls around 2000 - 3000 calls,

和而循环执行,我失去我的形式和窗体控件控制,因为它成为一个大的过程,一段时间后显示没有响应,但如果我等待了很长它再回来,我想我需要使用一些线程模型为,有什么想法,我怎么能着手解决这一问题?

and while executing the for loop, I loose my control on form and form controls, as it becomes a large process and some time it shows "Not Responding" but if I wait for a long it comes back again, I think I need to use some threading model for that, is there any idea, how can I proceed to solve the issue?

推荐答案

您需要在后台线程中执行长时间运行的操作。

You need to perform the long running operation on a background thread.

有这样做的几种方法。


  1. 您可以排队的线程池线程的方法调用执行(见<一href=\"http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx\">here):

ThreadPool.QueueUserWorkItem(new WaitCallback(YourMethod));

还是在.NET 4.0中,你可以使用TaskFactory:

Task.Factory.StartNew(() => YourMethod());


  • 您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx\">BackgroundWorker用于在方法更多的控制权,如果它完成时,你需要的东西像进度更新或通知。拖动一个BackgroundWorker控件到窗体并附加你的方法对DoWork的事件。当你想运行您的方法,那么刚刚启动工作。当然你也可以手动从code创建BackgroundWorker的,只记得它需要当您完成处置。

  • You could use a BackgroundWorker for more control over the method if you need things like progress updates or notification when it is finished. Drag the a BackgroundWorker control onto your form and attach your method to the dowork event. Then just start the worker when you want to run your method. You can of course create the BackgroundWorker manually from code, just remember that it needs disposing of when you are finished.

    创建一个完全新的线程对你的工作的情况发生。这是最复杂的,是没有必要的,除非你需要在线程真正细粒度控制。如果你想了解这个显示在线程类的MSDN页面。

    Create a totally new thread for your work to happen on. This is the most complex and isn't necessary unless you need really fine grained control over the thread. See the MSDN page on the Thread class if you want to learn about this.

    请记住,任何线程,您不能更新GUI,或从后台线程更改任何GUI控件。如果你想对你必须使用调用(和InvokeRequired)来触发回法GUI线程上的GUI任何东西。请参见 href=\"http://weblogs.asp.net/justin_rogers/pages/126345.aspx\">。

    Remember that with anything threaded, you cannot update the GUI, or change any GUI controls from a background thread. If you want to do anything on the GUI you have to use Invoke (and InvokeRequired) to trigger the method back on the GUI thread. See here.

    这篇关于WinForm应用程序UI挂起长时间运行的操作过程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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