|
| communication between native c++ thread in dll and c# |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.framework.interop.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| cody |
| GOOD ANSWER |
hi i have the following scenario here:
i have a gui written in c#. the gui has to load a certain dll (written in native c++) and call one function in it which starts a thread. this thread runs during the whole time in the program.
the problem now is the gui has to communicate with this thread. i need to send commands to the thread and the thread has to send results to the gui code.
i already know how to call native code but how can i achieve this communication after the thread has been started?
cody
|
|
|
| |
|
|
| |
| |
| David Stucki [MS] |
| GOOD ANSWER |
If you were doing this same thing all in C++ how would you do it? A possibility would be one thread would signal a shared syncronization object that the worker thread was waiting on (or checking periodically) to do something. I would get this working in straight C++. Then the C++ code that signals the worker thread could be wrapped in a simple exported function call, which you can then use from the managed code.
David Stucki Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
| |
|
|
| |
| |
| cody |
| GOOD ANSWER |
[Original message clipped]
oh my god this sounds very complicated. my experience in threading in c++ is so not good.
ok, if i want to c# gui from my c++ thread i have to call a delegate. but how can i call managed code from native c++? and what do you mean with "shared syncronization object"?
|
|
|
| |
|
|
| |
| |
| Thomas Scheidegger [MVP] NETMaster (VIP) |
| GOOD ANSWER |
> how can i call managed code from native c++?
also with a delegate.
Note, the C++ callback signature must have the __stdcall calling convention, C# only supports this one.
I have prepared a VS.NET solution that shows exactly this: http://dnetmaster.net/source/CSharpGuiCppThread.zip [17KB size] (USE AT YOUR OWN RISK)
It contains two projects: 'CSharpApp' a C# Windows Forms application 'CppDll' a VC++/Win32 DLL
flow:
C# GUI Button => DLL API / C++ starts worker thread
and:
C# delegate <= callback from C++ worker thread || form.Invoke || C# GUI ProgressBar
finally:
C# GUI Button => DLL API / C++ stops worker thread
Just watch for deadlocks & reentrant code!
-- Thomas Scheidegger - MVP .NET - 'NETMaster' http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/
|
|
|
| |
|
|
| |
| |
| cody |
| GOOD ANSWER |
[Original message clipped]
thank you a lot, Thomas, you are great!
cody
|
|
|
| |
|
|
| |
|
| |
| dagos dag |
| GOOD ANSWER |
hi,
thanks very much, your sample code is exelent. I use the DLL thread with a VB application and no problems.
That's really usefull!
bye
-------------------------------- From: dagos dag
|
|
|
| |
|
|
| |
|
|
|
|
| | |
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|