Waiting for multiple events
Is it possible to wait for multiple ManualResetEvents in .NET cf? If not, is it possible to interrupt a thread that is blocking on an event?
Thanks in advance,
Nille
Is it possible to wait for multiple ManualResetEvents in .NET cf? If not, is it possible to interrupt a thread that is blocking on an event?
Thanks in advance,
Nille
using System; using System.IO; using System.Security.Permissions; using System.Threading; // Request permission to create and write files to C:\TestTest. [assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, All = @"C:\TestTest")] class Test { static void Main() { const int numberOfFiles = 5; string dirName = @"C:\TestTest"; string fileName; byte[] byteArray; Random randomGenerator = new Random(); ManualResetEvent[] manualEvents = new ManualResetEvent[numberOfFiles]; State stateInfo; if(!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } // Queue the work items that create and write to the files. for(int i = 0; i < numberOfFiles; i++) { fileName = string.Concat( dirName, @"\Test", i.ToString(), ".dat"); // Create random data to write to the file. byteArray = new byte[1000000]; randomGenerator.NextBytes(byteArray); manualEvents stateInfo = new State(fileName, byteArray, manualEvents ThreadPool.QueueUserWorkItem(new WaitCallback( Writer.WriteToFile), stateInfo); } // Since ThreadPool threads are background threads, |
However, I'm using .NET Compact Framework and WaitHandle.WaitAny isn't available in cf, as it seems (am I right about this...?). Any other way of doing it?
I'm using the events to synchronize with a DLL containing native C++, and I want to be able to interrupt the synchronization e.g. upon application exit. I have just noticed that it doesn't work very well to pass event handles between the .NET CF application and the DLL, for some reason. Anyone experienced any problems with this? Any event handle I receive from the DLL or pass to the DLL will be invalid, which means that the wait methods will return immediatelly. One might think that this application would block forever:
public class MyBlockingClass { public static void Main(String[] args) { ManualResetEvent evt = new ManualResetEvent(false); evt.Handle = CreateNativeEvent(); evt.WaitOne(); // This should block forever, but it doesn't... } } [DllImport("myDll.dll", EntryPoint="CreateNativeEvent")] |
// And the dll:
extern "C" __declspec(dllexport) HANDLE CreateNativeEvent()
{
// ManualReset + No initial owner
return ::CreateEvent(NULL, true, false, NULL);
}
Any ideas about this?
Thanks in advance,
Nille