C# WaitForMultipleObjects问题(线程问题)
现在主要是希望启动线程,然后利用像WaitForMultipleObjects这样的函数将线程挂起,当set一个符合的Event事件时触发线程响应。自己做了一个程序,但是在运行时出错,WaitForMultipleObjects返回值总是0xffffffff无效值,不知道为什么,希望有大虾帮助看看,下面是大概示例:
using System.Threading;
class WatchThread
{
[DllImport("kernel32.dll")]
private static extern int CreateEvent(IntPtr lpEventAttributes,
bool bManualReset,
bool bInitialState,
string lpName);
[DllImport("kernel32.dll")]
private static extern bool SetEvent(int iEvent);
[DllImport("kernel32.dll")]
private static extern uint WaitForMultipleObjects(uint nCount,
ref int[] lpHandles,
bool bWaitAll,
int dwMilliseconds);
const uint iMaxEventNum = 4;
static int[] m_iEvent = new int[4];
IntPtr pt = new IntPtr();
m_iEvent[0] = CreateEvent(pt, false, false, null);
m_iEvent[1] = CreateEvent(pt, false, false, null);
m_iEvent[2] = CreateEvent(pt, false, false, null);
m_iEvent[3] = CreateEvent(pt, false, false, null);
Thread t1 = new Thread(WaitAndProcEvent);
t1.IsBackground = true;
t1.Start();
SetEvent(m_iEvent[0]);
bool bOK = false;
while (!bOK)
{
uint uiRet = 0;
//就是这里,总是得不到正确的值
uiRet = WaitForMultipleObjects(iMaxEventNum, ref m_iEvent, false, Timeout.Infinite);
}
}
------解决方案--------------------GetLastError()
------解决方案--------------------有可能句柄中某一个被别的线程调用
跟踪一下嘛