C# 优雅地退出线程

C# 优雅地退出线程,c#,.net,multithreading,C#,.net,Multithreading,我正在用C#开发一个多线程应用程序,现在我意识到,当我通过.Abort()停止线程时,线程有时会抛出错误。Join()方法 我当前用于启动和停止线程的代码如下: public void StartLogging() { if (poller != null && poller.IsAlive) { poller.Abort(); poller.Join(); }

我正在用C#开发一个多线程应用程序,现在我意识到,当我通过
.Abort()停止线程时,线程有时会抛出错误。Join()方法

我当前用于启动和停止线程的代码如下:

public void StartLogging()
    {
        if (poller != null && poller.IsAlive)
        {
            poller.Abort();
            poller.Join();
        }
        poller = new Thread(new ThreadStart(PollUSBDevice));
        poller.IsBackground = true;
        poller.Name = reference.VendorId.ToString() + ":" + reference.ProductId.ToString();
        poller.Start();
        IsLogging = true;
    }

public void StopLogging()
    {
        if (poller != null && poller.IsAlive)
        {
            poller.Abort();
            poller.Join();
            IsLogging = false;
        }
    }

private void PollUSBDevice()
    {
        ...Removed code - executes within milliseconds and I am not worried about stopping here.

        ErrorCode ec = ErrorCode.None;

            ### THIS LOOPS FOR EVER OR UNTIL I CALL .Abort() ###
            while (ec == ErrorCode.None && MyUsbDevice.IsOpen)
            {
                if (poller.ThreadState == System.Threading.ThreadState.AbortRequested)
                {
                    reader.Abort();
                    reader.Dispose();
                    break;
                }
                else
                {
                    byte[] readBuffer = new byte[8];
                    int bytesRead;
                    ec = reader.Read(readBuffer, 100, out bytesRead);

                    Application.Current.Dispatcher.BeginInvoke(
                        new OneArgDelegate(HandleData),
                        new object[] { readBuffer });
                }
            }
        }
        catch (Exception ex)
        {
            Do stuff....
        }
        finally
        {
            Close devices that are running in above while statement
        }
    }
我在这里尝试了Stackoverflow上的其他方法,但是我就是不能理解它们(我对多线程还比较陌生)。最好是在我的父对象
reference
上有一个
bool
开关,我可以检查它。即:

public class Reference
{
    public static bool gracefulStopRequested = false;
}

public void PollUSBDevice
{
     while (ec == ErrorCode.None && !reference.gracefulStopRequested)
     {
         ....
     }
}

有没有人能给我指出一个好的资源,或者给我一个提示,告诉我应该搜索什么搜索词,或者如果你真的很乐于助人,可以做一个你将如何处理这个问题的模型

class Program
{
    static void Main(string[] args)
    {
        Thread poller = new Thread(new ThreadStart(PollUSBDevice));
        poller.Start();

        Console.ReadLine();

        StopPoller();

        Console.WriteLine("Stopped");
        Console.ReadLine();
    }

    public static void StopPoller()
    {
        _PollerStopRequested = true;
    }

    private static bool _PollerStopRequested = false;
    private static void PollUSBDevice()
    {
        while (true && !_PollerStopRequested)
        {
            Console.WriteLine("running");
            Thread.Sleep(500);
        }
    }
}
不过,这只是模拟C#
BackgroundWorker的内置功能,因此您还可以看看: