Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查线程是否在C#中运行?_C#_Multithreading_System - Fatal编程技术网

如何检查线程是否在C#中运行?

如何检查线程是否在C#中运行?,c#,multithreading,system,C#,Multithreading,System,我在C#4.0中创建了一个线程,想知道如何检查它是否正在运行?您可以使用它来检查线程是否正在运行 也就是说,如果您使用的是C#4,那么手动创建“线程”很少是个好主意。您应该考虑使用TPL和任务 >代码>任务类,因为这提供了一个更干净的模型,以便在任务完成后将工作附加到运行,从操作中拔出数据等。 < p>您可以使用以检查线程< /代码>是否正在运行。 也就是说,如果您使用的是C#4,那么手动创建“线程”很少是个好主意。您应该考虑使用TPL和任务 />代码>任务< /代码>类,因为这提供了一个更干净

我在C#4.0中创建了一个线程,想知道如何检查它是否正在运行?

您可以使用它来检查
线程是否正在运行

也就是说,如果您使用的是C#4,那么手动创建“线程”很少是个好主意。您应该考虑使用TPL和<代码>任务<代码> >代码>任务<代码>类,因为这提供了一个更干净的模型,以便在任务完成后将工作附加到运行,从操作中拔出数据等。

< p>您可以使用以检查<代码>线程< /代码>是否正在运行。
也就是说,如果您使用的是C#4,那么手动创建“线程”很少是个好主意。您应该考虑使用TPL和<代码>任务<代码> />代码>任务< /代码>类,因为这提供了一个更干净的模型,以便在任务完成后将工作附加到运行,从操作中拔出数据等。

< P>我使用互斥体来验证这一点。有时只需使用Thread.IsAlive验证线程是否处于活动状态。如果您在后台运行,则IsAlive不安全

试试这个:

private void btnDoSomething()
{
    try
    {
        string nameThread = "testThreadDoSomething";

        var newThread = new Thread(delegate() { this.DoSomething(nameThread); });
        newThread.IsBackground = true;
        newThread.Name = nameThread;
        newThread.Start();

        //Prevent optimization from setting the field before calling Start
        Thread.MemoryBarrier();
    }
    catch (Exception ex)
    {

    }
}

public void DoSomething(string threadName)
{
    bool ownsMutex;

    using (Mutex mutex = new Mutex(true, threadName, out ownsMutex))
    {
        if (ownsMutex)
        {
            Thread.Sleep(300000); // 300 seconds

            if (Monitor.TryEnter(this, 300))
            {
                try
                {
                    // Your Source
                }
                catch (Exception e)
                {
                    string mensagem = "Error : " + e.ToString();
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }

            //mutex.ReleaseMutex();
        }
    }
}

我使用互斥来验证这一点。有时只需使用Thread.IsAlive验证线程是否处于活动状态。如果您在后台运行,则IsAlive不安全

试试这个:

private void btnDoSomething()
{
    try
    {
        string nameThread = "testThreadDoSomething";

        var newThread = new Thread(delegate() { this.DoSomething(nameThread); });
        newThread.IsBackground = true;
        newThread.Name = nameThread;
        newThread.Start();

        //Prevent optimization from setting the field before calling Start
        Thread.MemoryBarrier();
    }
    catch (Exception ex)
    {

    }
}

public void DoSomething(string threadName)
{
    bool ownsMutex;

    using (Mutex mutex = new Mutex(true, threadName, out ownsMutex))
    {
        if (ownsMutex)
        {
            Thread.Sleep(300000); // 300 seconds

            if (Monitor.TryEnter(this, 300))
            {
                try
                {
                    // Your Source
                }
                catch (Exception e)
                {
                    string mensagem = "Error : " + e.ToString();
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }

            //mutex.ReleaseMutex();
        }
    }
}

在.NET4中,Task现在比BackgroundWorker更受欢迎吗?我刚刚了解了Task,但似乎没有像BackgroundWorker那样订阅事件。@StealthRabbi一般来说,你可以用BW和tasks做同样的事情。Continuations和scheduler让您可以完成许多相同的概念。在C#5中,async/await使用了任务,并使BW完全过时,我想。有关UI工作任务的信息,请参阅。在.NET4中,任务现在比BackgroundWorker更受欢迎吗?我刚刚了解了Task,但似乎没有像BackgroundWorker那样订阅事件。@StealthRabbi一般来说,你可以用BW和tasks做同样的事情。Continuations和scheduler让您可以完成许多相同的概念。在C#5中,async/await使用了任务,并使BW完全过时,我想。有关UI工作任务的信息,请参阅。欢迎使用堆栈溢出。请张贴您的密码。到目前为止您尝试了什么?欢迎使用堆栈溢出。请张贴您的密码。到目前为止你试过什么?