主要是怎么监控一个线程正在运行 用if(“线程”!= null)来断定正在运行。
/// <summary>
/// 创建循环线程 led_Start
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void led_Start()
{
int p;
IntPtr gpiodriver = CreateFile("GIO1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
int bytesReturned = 0;
while (true)
{
for (int i = 0; i < 4; i++)
{
p = 0x01 + i;
DeviceIoControl(gpiodriver, p, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
Thread.Sleep(200);
DeviceIoControl(gpiodriver, p + 3, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
}
}
}
private void button_stop_Click(object sender, EventArgs e)
{
IntPtr gpiodriver = CreateFile("GIO1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (gpiodriver == (IntPtr)(-1))
{
MessageBox.Show("打开GPIO设备失败!");
}
else
{
if (led_start != null)
{
led_start.Abort();
int bytesReturned = 0;
DeviceIoControl(gpiodriver, 0x04, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
DeviceIoControl(gpiodriver, 0x05, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
//DeviceIoControl(gpiodriver, 0x06, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
//DeviceIoControl(gpiodriver, 0x07, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
}
else
{
int bytesReturned = 0;
DeviceIoControl(gpiodriver, 0x04, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
DeviceIoControl(gpiodriver, 0x05, null, 0, null, 0, ref bytesReturned, IntPtr.Zero);
}
}
}