sychronizing asychronous calls to DllImport function

W

William Johnston

Hello,

I need to perform synchronous calls to a DllImport function that is asychronous. (The Dllmport function returns immediately.)

So far I have a timer that waits inside a ThreadPool queue. But I would rather not use anything close to time-sharing.

Here is my code:
[
void mainloop ()
{

// call to asynchronous DllImport function
CreateVOBSegment(hwnd, strSourceVOBFilename, strBegTimeStamp, strEndTimeStamp, strDestFilename);

ThreadPool.QueueUserWorkItem(new WaitCallback(VOBExtractParser.Sleep), autoEvent);
autoEvent.WaitOne();
}
}
/// <summary>
/// thread that waits for 20 seconds
/// </summary>
private static void Sleep(object stateInfo) {
Thread.Sleep(TIMEOUT);
((AutoResetEvent)(stateInfo)).Set();
}

]

Do you have any suggestions?

Regards,
William Johnston
 
D

Doug Semler

Hello,

I need to perform synchronous calls to a DllImport function that is asychronous. (The Dllmport function returns immediately.)

So far I have a timer that waits inside a ThreadPool queue. But I would rather not use anything close to time-sharing.

Here is my code:
[
void mainloop ()
{

// call to asynchronous DllImport function
CreateVOBSegment(hwnd, strSourceVOBFilename, strBegTimeStamp, strEndTimeStamp, strDestFilename);

ThreadPool.QueueUserWorkItem(new WaitCallback(VOBExtractParser.Sleep), autoEvent);
autoEvent.WaitOne();
}
}
/// <summary>
/// thread that waits for 20 seconds
/// </summary>
private static void Sleep(object stateInfo) {
Thread.Sleep(TIMEOUT);
((AutoResetEvent)(stateInfo)).Set();
}

]

How does the API for CreateVobSegment document that it has completed?
Does it post a message to hwnd's message loop (i am guessing since you
pass an HWND)? You could override the WndProc function in this case:
public class Form1 : Form
{
AutoResetEvent processComplete = new AutoResetEvent(false);
const int MESSAGE_CODE_PROCESS_COMPLETE = 0x401;

protected override void WndProc(ref Message m)
{
if (m.Msg == MESSAGE_CODE_PROCESS_COMPLETE)
{
processComplete.Set();
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}
}
 
B

Brian Gideon

Hello,

I need to perform synchronous calls to a DllImport function that is asychronous. (The Dllmport function returns immediately.)

So far I have a timer that waits inside a ThreadPool queue. But I would rather not use anything close to time-sharing.

Here is my code:
[
void mainloop ()
{

// call to asynchronous DllImport function
CreateVOBSegment(hwnd, strSourceVOBFilename, strBegTimeStamp, strEndTimeStamp, strDestFilename);

ThreadPool.QueueUserWorkItem(new WaitCallback(VOBExtractParser.Sleep), autoEvent);
autoEvent.WaitOne();
}
}
/// <summary>
/// thread that waits for 20 seconds
/// </summary>
private static void Sleep(object stateInfo) {
Thread.Sleep(TIMEOUT);
((AutoResetEvent)(stateInfo)).Set();
}

]

Do you have any suggestions?

Regards,
William Johnston

If CreateVOBSegment really is asynchronous thenit must report when it
completes somehow. How does it do that.

Also, there is no advantage to using the ThreadPool the way you are.
The behavior is the same as placing the Thread.Sleep call in the
mainloop method.
 
W

William Johnston

Wow! I didn't realize my use of ThreadPool.

I will ask the technical contact regarding the end of processing message.

Thanks.

William Johnston
 
Top