需要帮助实现多线程在C#多线程

2023-09-03 07:06:55 作者:>像个大大的傻子

我在做一个应用程序中,由于某种原因,当我点击一个按钮,我要开始新的形式,并在同一时间创造了谷歌文档的新文档。 我已经成功地实现了上述但是当应用程序是忙着做创建在谷歌文档一个新的文档对于目前新加载的窗体的UI冻结。 我读的地方,这样就可以避免,如果我使用多线程。 所以现在我想请问我应该创建两个线程,并在其中的一个,我应该将$ C $下创造新的形式和另外一个我应该把code创建在谷歌文档的文档。 或者我应该只是创建线程中,我应该将code,以创造新的谷歌文档,并让新形式的创造code在主过程? 另外什么是在一个已经写code实现线程最简单的方法? Pleaseprovide具有一定的参考读物如果可能的话。

I am making an app in which for some reason when I click on a button I have to initiate a new form and at the same time create a new document in the google docs. I've successfully implemented the above but while the app is busy making creating a new doc at google docs for that time being the newly loaded form's UI freezes. I read somewhere that this can be avoided if I use multi threading. So now I wanna ask should I create two threads and in one of them I should place the code for creating new form and in the other one I should place the code to create a document in google doc. Or I should just create on thread in which I should place the code to create new google doc and let the new form creating code be in the main process?? Also What would be the easiest way to implement threading in an already written code?? Pleaseprovide with some reference reading material if possible.

推荐答案

您其实有很多的选择。

(1)的BackgroundWorker。如果你真的想最简单的编程模型中的WinForms异步工作方式,这将是这一点。它通常用来做一些异步任务,虽然报告进度,但你不必报告进度,如果你不需要。

(1) BackgroundWorker. If you truly want the easiest programming model for asynchronous work in WinForms, it would be this. It's generally used to do some asynchronous task and report progress though, but you don't have to report progress if you don't need to.

(2)基于事件的异步模式。如果你想一个完全成熟的组件,做一些异步工作,完全控制报告进展情况和自己的自定义事件,那么这是一种方式去做吧。这也有助于您了解线程超过一个BackgroundWorker。因为我是一个视觉化的人,我创建了如何做到这本用的WinForms一个完整的视频presentation

(2) Event Based Asynchronous Pattern. If you want to make a full blown component that does some asynchronous task, have full control of reporting progress and your own custom events, then this is one way to do it. This also helps you understand threading more than a BackgroundWorker. Because I am a visual person, I created a full video presentation on how to do just this with WinForms.

(3)任务并行库。您可以使用TPL用的WinForms,我写了一个very如何做到这一点这里详细的博客文章。

(3) Task Parallel Library. You can use the TPL with WinForms, and I wrote a very detailed blog post on how to do just that here.

(4)异步和等待。请注意,这需要.NET 4.5,C#5.0和C#5.0编译器只包含在Visual Studio 11,这是只有在BETA现在。 However,我也有关于如何做到这本一个完整的博客文章。

(4) Async and Await. Please note that this requires .NET 4.5, C# 5.0 and the C# 5.0 compiler only included in Visual Studio 11, which is only in BETA right now. However, I also have a full blog post on how to do just this.

(5) ISynchronizedInvoke与主题。这是另一种选择,其中I也有关于一个完整的博客。

(5) ISynchronizedInvoke with Threads. This is another option, which I also have a full blog about.

这真的取决于你选择哪一种方法。我的建议是采取简单的介绍一下每挑一个方法的基础上如何推进这个问题的感觉给你,或曾经方法可能会满足您的要求是最好的。

It's really up to you which method you choose. My suggestion is take a brief look at each and pick on a method based on how advanced the subject feels to you, or which ever method might meet your requirement the best.