简体   繁体   English

使用线程时UI无响应

[英]UI not responsive when using Thread

I was using Thread to start two functions AllCase and run them in parallel. 我使用Thread启动两个函数AllCase并并行运行它们。

Thread t1, t2;
t1 = new Thread(new ThreadStart(AllCase))
{
    Name = "Thread1"
};
t1.Start();

t2 = new Thread(new ThreadStart(AllCase))
{
    Name = "Thread2"
};
t2.Start();
t1.Join();
t2.Join();

When Thread starts, I can't interact with the UI of application. Thread启动时,我无法与应用程序的UI进行交互。

I read using BackgroundWorker . 我使用BackgroundWorker阅读。

But I want to get Name = "Thread1" to use, like this: 但是我想让Name = "Thread1"可以使用,像这样:

int idx = int.Parse(Thread.CurrentThread.Name.Replace("Thread", "")) - 1;

The values idx to know this process[idx] to easy manipulate. idx知道这个过程[IDX]易操纵。

And BackgroundWorker is not working correctly in my case. 在我的情况下, BackgroundWorker无法正常工作。

So, have any method to resolve my problem? 那么,有什么方法可以解决我的问题?

remove these two lines: 删除以下两行:

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

and your GUI should become responsive again. 并且您的GUI应该再次响应。 Unless of course you have to wait for the methods to be completed in this order, but then you don't really need to use threads. 当然,除非您必须等待方法按此顺序完成,否则您实际上并不需要使用线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM