question

kirillandy-0856 avatar image
0 Votes"
kirillandy-0856 asked kirillandy-0739 commented

Invoking functions from a non-managed thread

Hello! I was wondering if it is possible to Invoke/BeginInvoke a delegate from a non-managed thread.

To elaborate, I've got a Windows Forms... form which contains a little image (PictureBox object) that indicates the current state of the PC's Wi-Fi connection (online/offline).
I have registered a callback function using the WlanRegisterNotification function from the Native Wi-Fi API. Whenever the network connection state changes, my callback is executed (on a different thread, if I remember correctly) and I get the new connection state in one of the callback function parameters.

I was wondering if I could immediately BeginInvoke a delegate on my form from the callback function in order to change the image displayed in my PictureBox, but I remember vague warnings from various sources which recommend against mixing managed thread and non-managed thread code. I could not find any source that addresses my example in detail, though, and I'm having trouble deducing why my example could be a bad idea.

Has anyone attempted something like this?

windows-apiwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
1 Vote"
RLWA32-6355 answered kirillandy-0739 commented

A common and safe method used to initiate an update to the UI from a non-UI thread is to use a custom window message. The non-UI thread is passed the HWND of the window that handles updating on the UI thread. In your example you could use the pCallbackContext parameter to pass the HWND when you register the callback. The callback function will receive the HWND as one if its parameters. Inside the callback, use PostMessage to post the custom window message to the HWND. You would need to include code in your Winform project to recognize the custom window message and update the UI, typically with a WndProc override.

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ah, yes, that sounds really good! Thanks! I've overridden WndProc before for a couple other things and it never occurred to me to do it here. D'oh! I'll eventually make it a habit to remember that it's another option I have at my disposal and it is also a very flexible approach. Thank you once again!
Btw: is there a way to "Mark as Answer" here or was that a thing in the old forums? If it's still available, then I'd do it in a instant :D!

0 Votes 0 ·

Glad to help. You should be able to accept the response as an answer. Take a look at accepted-answers


0 Votes 0 ·

You asked the question as @kirillandy-0856 so that is the id that you must use to be able to accept the answer. Only the original poster can accept an answer and @kirillandy-0739 is not the original poster.

0 Votes 0 ·
Show more comments
vb2ae avatar image
0 Votes"
vb2ae answered kirillandy-0739 commented

You could try using a dispatch timer. when the network status needs to be changed set a property that you look for in the dispatch timer tick event. If it is set change the image

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I did think of something along those lines in case I needed to look for alternatives. I was more curious about the whole managed/non-managed discussion. On the one hand I've got a basic C++ .lib with my WiFi code and the WiFi callback and on the other I've got a C++/CLI WinForms project that uses that library. I don't know what's what in this case, so I needed clarification. If my .lib code calls a function from a separate (non-managed?) thread that utilizes BeginInvoke() for a control created in another (managed?) thread, are there any possible issues I should be aware of. It's just a case of ignorance on my part.

I very much prefer the solution suggested by @RLWA32-6355 below, it's a familiar approach but I never even considered it :D

0 Votes 0 ·