多线程机制来运行的WinForms code和GUI通信的一些冗长的操作冗长、多线程、机制、操作

2023-09-06 14:21:45 作者:我的媳妇不用蓝颜疼i

我想要什么来实现的:我要进行一些耗时从我的MDI WinForms应用程序(C# - .NET)的操作。

What do I want to achieve: I want to perform some time consuming operations from my MDI winforms application (C# - .NET).

这是MDI子窗体可以创建与操作,这可能需要很长的时间线(0.1秒时,甚至半小时)才能完成。在此期间,我希望UI到用户的操作,包括操纵其他MDI子窗体的数据作出反应。操作完成后,该线程应通知MDI子的计算完成, 使MDI子可以进行后期处理。

An MDI child form may create the thread with the operation, which may take long time (from 0.1 seconds, to even half hour) to complete. In the meantime I want the UI to respond to user actions, including manipulation of data in some other MDI child form. When the operation completes, the thread should notify the MDI child that the calculations are done, so that the MDI child can perform the post-processing.

我怎样才能做到这一点: 我应该使用显式的线程(即创建明确的线程),线程池?或者只是简单地提出你的解决方案。我应该创建前台或后台线程?

How can I achieve this: Should I use explicit threading (i.e., create explicit threads), thread pools? Or simply just propose your solution. Should I create foreground or background threads?

和如何做的线程在GUI中,根据通信您提出解决方案?

And how does the thread communicates with the GUI, according the solution you propose?

如果您知道处理类似情况的工作示例中,请记下。

If you know of a working example that handles a similar situation, please make a note.

推荐答案

这取决于,略有下降。

在一般情况下,I同意贝丽的是, BackgroundWorker的是可能最好在这里选择。我做通知发回用户界面简单,等等。

In general, I agree with BFree that the BackgroundWorker is likely the best option here. I makes notification back to the UI simple, etc.

话虽这么说,我张贴,并质疑是否可能需要使用BackgroundWorker的唯一的原因是这样的语句:

That being said, the only reason I'm posting, and questioning whether you may want to use BackgroundWorker is this statement:

这可能需要很长一段时间(0.1秒时,甚至半小时)才能完成。

which may take long time (from 0.1 seconds, to even half hour) to complete

BackgroundWorker的使用线程池线程来执行它的处理。这意味着关闭你的申请表将终止线程,因为它是一个后台线程。

BackgroundWorker uses a ThreadPool thread to perform its processing. This means that closing your Application form will terminate the thread, since it's a background thread.

如果你的半小时处理工作的东西,你想有继续工作,即使窗体被关闭,你可能想使自己的前台线程执行该操作,并处理UI编组你自己。

If your "half hour" processing "work" is something that you would like to have continue operating, even if the form is closed, you may want to make your own foreground thread to perform this operation, and handle the UI marshaling yourself.

 
精彩推荐