且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按钮单击事件上的多线程代码使应用程序处于挂起状态

更新时间:2023-02-15 20:02:09

注释中提到了潜在的死锁,虽然我不确定是否是这种情况,但应用程序的挂起是由行引起的

There's the potential deadlock mentioned in the comments, and while I'm not sure if that is the case, the hanging of the application is caused by the lines

t1.Join();
t2.Join();

Join告诉UI线程应该阻塞,直到这些线程完成.您可能需要一个单独的回调/事件来在线程完成时触发,或者,如果您使用的是.NET 4.5,则可以使用任务异步功能并在后台线程调用中使用await.

Join is telling the UI thread that it should block until these threads are finished. You either need a separate callback/event to fire when the threads complete, or, if you're using .NET 4.5, you can use the Task Async capabilities and use await on your background thread calls.

查看此示例,了解如何使用新的async/await关键字.

Check out this example on how to use the new async/await keywords.