Csharp中的Mutlti_Thread [英] Mutlti_Thread in Csharp

查看:108
本文介绍了Csharp中的Mutlti_Thread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,如何杀死线程外的线程,

 //  ------算法--------- -

线程T =  Thread(Method_1);
T.Start(Argument_1);

公共 无效 method_2(参数_2)
{
      - - 代码  - -

  如果(测试),杀死(T);

      - - 代码  -  - 
}

//  -------结束算法-------  



注意:我将T用作全局&我使用了"T.Abort()",但不起作用.
因此,它是否具有其他解决方案杀死线程" ??????????????????当您调用abort时,无法知道线程在做什么,因此谁知道在决定终止时它将离开系统或程序的状态.

线程应该设计出一种在需要退出时互相通知的方式.最简单的方法是布尔值.在启动线程之前,将布尔值设置为true,然后在线程方法中定期检查布尔值是否仍然为true.如果不是,则该方法知道应该立即完成所执行的操作,清理并退出.像这样:

// ------ Algo -----------
volatile bool keepRunning = true;
Thread T = new Thread(Method_1);
T.Start (Argument_1);

public void method_2(Argument_2)
{
    // do stuff

    // tell the thread to exit
    if(test) keepRunning = false; 

    // do stuff
}
// ------- End Algo -------

// ------- Method_1 -------
private void Method_1(object param)
{
    // do stuff
    
    if (!keepRunning) return;
    
    // do more stuff
    
    if (!keepRunning) return;

    // etc.
}

// ------- End Method_1 -------



请记住,在将布尔值设置为false之后,线程将继续运行,直到下次检查布尔值为止.因此,您应该警惕执行阻塞操作,因为在这些操作完成之前,它们不允许线程检查布尔值.


如果您有对可用线程的引用,您可以在其上调用任何方法.


How Can I kill the thread outside of it, I mean,

// ------ Algo -----------

Thread T = new Thread(Method_1);
T.Start (Argument_1);

public void method_2(Argument_2)
{ 
     --- Code --- 

  if(test) Kill (T);

     --- Code ----   
}

// ------- End Algo -------



Notice : I used T as a global & I used "T.Abort()", but Does not work.
So, does it have Other Solution " killing a thread " ???

解决方案

Thread.Abort() is about the worst way to kill a thread that there is. There''s no way to know what the thread is doing when you call abort so who knows what state it''s going to leave the system or your program in when it decides to terminate.

The threads should work out some way of notifying each other when one needs to exit; the simplest way is a boolean value. Before you start the thread set the boolean to true and then in the thread method periodically check to see if the boolean is still true. If it''s not then the method knows that it should immediately finish whatever it''s doing, cleanup and exit. Like this:

// ------ Algo -----------
volatile bool keepRunning = true;
Thread T = new Thread(Method_1);
T.Start (Argument_1);

public void method_2(Argument_2)
{
    // do stuff

    // tell the thread to exit
    if(test) keepRunning = false; 

    // do stuff
}
// ------- End Algo -------

// ------- Method_1 -------
private void Method_1(object param)
{
    // do stuff
    
    if (!keepRunning) return;
    
    // do more stuff
    
    if (!keepRunning) return;

    // etc.
}

// ------- End Method_1 -------



Keep in mind that after the boolean is set to false the thread will keep running until the next time that it checks the boolean. For that reason you should be wary of performing blocking operations because they don''t allow the thread to check the boolean until those operations complete.


If you have a reference to your thread available, you can call any method on it.


这篇关于Csharp中的Mutlti_Thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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