开发者

How to stop a thread in my unmanaged DLL made in C#

I have a C# DLL that starts a thread for polling an USB device. My problem is that the workerObject and workerThread in the StopListenUSB function are null.

public class USBDevice
{
   private Worker workerObject;
   private Thread workerThread;

   public void StartListenUSB(int hwnd, uint wm_USER_KEYBOARD)
   {
      workerObject = new Worker();
      workerTh开发者_StackOverflow社区read = new Thread(workerObject.DoWork);
      workerThread.Start();
   }

   public void StopListenUSB()
   {
      workerObject.RequestStop();
      workerThread.Join();
   }
}

public class Worker
{
   private volatile bool _shouldStop;

   public void RequestStop() { _shouldStop = true; }

   public void DoWork()
   {
      while (!_shouldStop)
      {...}
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜