Thread timeout in c#

前端 未结 7 622
春和景丽
春和景丽 2021-01-11 18:41

I\'m new to threading in C#. Is there anyway of setting a timeout for a thread without blocking the calling thread (in C# 3.5)?

If not, is it logical to execute a fu

相关标签:
7条回答
  • 2021-01-11 19:40

    Look at WaitHandle.WaitOne() method with the middleObject scheme.

    Public void main()
    {
        ...
        middleObj.WaitHandle.Reset();
        Thread thrd1 = new Thread(new ThreadStart(middleObj.waiter));
        thrd1.Start();
        middleObj.WaitHandle.WaitOne(timeout);
        ...
    }
    
    
    //And in the middleObj.waiter():
    Public void waiter()
    {
        Thread thrd2 = new Thread(new ThreadStart(targetObj.targetFunc));
        thrd2.Start();
        thrd2.Join();
        this.WaitHandle.Set();
    }
    

    Not sure what would happen to the unfinished thread, though.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题