Need help to implement multithreading in C#

后端 未结 5 1007
鱼传尺愫
鱼传尺愫 2021-01-25 13:07

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 successfu

5条回答
  •  一向
    一向 (楼主)
    2021-01-25 13:49

    You actually have a lot of options.

    (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) 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) 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) 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 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.

提交回复
热议问题