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)
{...}
}
}
精彩评论