等待线程在C#中完成的问题 [英] Problem in Wait for a thread to finish in C #

查看:74
本文介绍了等待线程在C#中完成的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在WPF和C#中开发应用程序.

我正在使用线程来完成操作.
该操作只需几秒钟即可完成.
问题是,当线程正在处理中时,即使在任务管理器的应用程序"选项卡中,我的窗口(例如,用户界面)也变得不活动,直到线程完成后,应用程序状态仍变为无响应".
完成后,用户界面将恢复正常.

所以我想要那个...
一旦启动线程,就不应阻塞UI,也不要等待完成线程.


我在下面尝试所有.

使用Sleep尝试1:

Hi All,

I am developing a application in WPF and C#.

I am using a thread to accomplished an opration.
The operation takes few second to finish.
The problem is that when the thread is on process my window (e.g. user interface) become Inactive even in Task Manager''s Application tab the application status become Not Responding until the thread is finish.
After finishing, the user interface become normal.

So i want that...
Once a thread is started,it shouldn''t be blocking the UI and also wait to finish the thread.


I try all in below.

Try 1 using Sleep :

Thread th1 = new Thread(operation1);
th1.Start();
Thread.Sleep(5000);



使用Join尝试2:



Try 2 using Join:

Thread th1 = new Thread(operation1);
th1.Start();
Thread.Join();




使用AutoResetEvent尝试3:




Try 3 using AutoResetEvent :

static EventWaitHandle _waitHandle = new AutoResetEvent (false);
Thread th1 = new Thread(operation1);
th1.Start();
_waitHandle.WaitOne();

private void operation1()
       {
           // do the operation

           _waitHandle.Set();
       }

推荐答案

如果您在UI线程中使用Thread.Join,那么如果您没有创建任何线程并在UI中创建了线程,则将其完全拧紧线程放在第一位.此呼叫是阻止呼叫.看起来您对线程很困惑.

有很多好的模式.我在这里收集了有关该主题的过去的答案:
If you use Thread.Join in UI thread, you screw it up exactly you if you did not create any thread and did it in UI thread in first place. This call is the blocking one. It looks like you''re seriously confused about threads.

There is a number of good patterns. I have a collection of my past Answers on the topic here: How to get a keydown event to operate on a different thread in vb.net[^], see other Answers, show more of your code and ask follow-up Questions.

—SA


SA是正确的.使用JoinWaitOneSleep将阻止您的应用程序.如果您要等待线程完成,则意味着您不需要线程.

如果您想等待并向用户显示其进度,那么这可能会很有用:
ProgressForm:链接到BackgroundWorker的简单表单 [
SA is right. Using Join or WaitOne or Sleep will block your application. If you want to wait for the thread to finish then it means you don''t need a thread.

If you want to wait and show its progress to the user, then maybe this will be useful:
ProgressForm: a simple form linked to a BackgroundWorker[^]


您想做什么?线程一旦启动,就不应阻塞UI.

What do you WANT to happen? Once a thread is started, it shouldn''t be blocking the UI.

public void button_Click(...)
{
    Thread thread = new Thread(new ThreadStart(ThreadProc));
    thread.Start();
}

public void ThreadStart()
{
   // do something
}



即使使用BackgroundWorker objec,它也应在不阻塞UI的情况下运行.

如果您需要线程在运行UI时更新UI,那么就足够了.

顺便说一句,您的第一个示例将启动线程,然后将UI休眠5秒钟.

此外,如果要使用Join,为什么还要使用线程?您应该使用Join将线程耦合在一起,以使一个线程直到连接的线程也完成时才结束.

似乎您对.Net中的线程工作方式没有足够的了解.



Even if you use a BackgroundWorker objec, it should run without blocking the UI.

If you need the thread to update the UI as it''s running, that''s simple ennough.

BTW, your first example is going to start the thread, and then sleep the UI for 5 seconds.

Further, why use a thread if you''re going to use Join? You should use Join to couple threads together so that one thread doesn''t finish until the joined thread is also finished.

It looks like you don''t have a firm grasp of how threading works in .Net.


这篇关于等待线程在C#中完成的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆