我有一个C#DLL,它启动一个线程来轮询USB设备。我的问题是workerObject
函数中的workerThread
和StopListenUSB
为null
。
public class USBDevice
{
private Worker workerObject;
private Thread workerThread;
public void StartListenUSB(int hwnd, uint wm_USER_KEYBOARD)
{
workerObject = new Worker();
workerThread = 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)
{...}
}
}