Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

C++ Equivalent to Control.BeginInvoke?

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a C++ dll (written in VC6) which creates a window in a thread and sends and recives messages to and from another application. When a message is recieved from the other application the dll will issue a callback to the host program. Since the window is on another thread I have to use Control.BeginInvoke() in my managed C# program that is hosting the dll. That works fine. But there are other people using the dll in different languages like Delphi and VB6. So I wanted to have the dll take care of issuing the callbacks on the same thread as a control or HANDLE they pass through to the dll on initialization. So long story short, is there a C++ equivalent to Control.BeginInvoke()?

Share this post


Link to post
Share on other sites
Advertisement
.NET simulates the BeginInvoke method by first detecting whether the current running thread is the same as the control thread by using the GetWindowThreadProcessId and GetCurrentThreadId functions. If marshaling is needed, it uses RegisterWindowMessage along with PostMessage to send the desired invocation object to the control, which will receive the message on its own thread, where it can be retrieved and acted upon in the message loop for the control.

Share this post


Link to post
Share on other sites
Thanks that is really interesting to know how .NET does it. Wonderfully simple when you think about it but incredibly handy.

The irony is, this dll I have written uses RegisterWindowMessage and PostMessage to communicate to this other application. So the work involved in implementing a type of Control.BeginInvoke would probably be the equivalent to implementing the hidden window as a form and communicating directly to the app without the dll entirely.

Thanks for the info it was very enlightening.

Share this post


Link to post
Share on other sites
I think you can view the code thru Reflector :)


private object MarshaledInvoke(Control caller, Delegate method, object[] args, bool synchronous)
{
int num;
if (!this.IsHandleCreated)
{
throw new InvalidOperationException(SR.GetString("ErrorNoMarshalingThread"));
}
if (((ActiveXImpl) this.Properties.GetObject(PropActiveXImpl)) != null)
{
IntSecurity.UnmanagedCode.Demand();
}
bool flag = false;
if ((SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, this.Handle), out num) == SafeNativeMethods.GetCurrentThreadId()) && synchronous)
{
flag = true;
}
ExecutionContext executionContext = null;
if (!flag)
{
executionContext = ExecutionContext.Capture();
}
ThreadMethodEntry entry = new ThreadMethodEntry(caller, method, args, synchronous, executionContext);
lock (this)
{
if (this.threadCallbackList == null)
{
this.threadCallbackList = new Queue();
}
}
lock (this.threadCallbackList)
{
if (threadCallbackMessage == 0)
{
threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_ThreadCallbackMessage");
}
this.threadCallbackList.Enqueue(entry);
}
if (flag)
{
this.InvokeMarshaledCallbacks();
}
else
{
UnsafeNativeMethods.PostMessage(new HandleRef(this, this.Handle), threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
}
if (!synchronous)
{
return entry;
}
if (!entry.IsCompleted)
{
this.WaitForWaitHandle(entry.AsyncWaitHandle);
}
if (entry.exception != null)
{
throw entry.exception;
}
return entry.retVal;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!