0
votes

I have an interesting study case for you :)

I have a problema with a wrapper integration. I made a Dll in write in C++. CLR Windows. This Dll is called form a C# application (for a the), till here all rigth.

The C# aplications is form a thrid part and I Cannot modify this source code.

When I call some C++ function since a button for example. The applications si bloqued an I can't doing any more until the C++ function return. I need that when I wating for a C++ function the different the formulary controlls has enabled again so on, I need some additional process.

I try to do it with async methods and treads but I can't figure out the way to deploy it.

C++ function

__declspec(dllexport) HANDLE openport(char *ComPort, int BR);

C# function:

[DllImport("mydll.dll")] public static extern IntPtr openport(string ComPort, int BR);

Thanks in advance for your help.

Regards.

1

1 Answers

0
votes

Your code needs to run in a new thread. Look into the System.Threading namespace for instructions and examples of how to create a new thread. Essentially, you create the thread

Here is an example from one of my old test programs.

Thread thdOneOfTwo = new Thread(new ParameterizedThreadStart(TextLogsWorkout.DoThreadTask));

In the above example, TextLogsWorkout.DoThreadTask is a static method on class TextLogsWorkout, which happens also to contain the above statement.

You have the option of giving each thread a name, and of using a WaitHandle that it can signal when it has completed its assignment. Both are optional, but you must execute the Start method on the instance.

Be aware that you are entering the world of multi-threaded programming, where many hazards await the unwary. If you aren't already, I suggest you read up on mutexes, wait handles, and the intrinsic lock () block.

Of the three, lock() is the simplest way that a single application can synchronize access to a property. Of the other two, WaitHandles and Mutexes are about equal in complexity. However, while a WaitHandle can synchronize activities within a process, a Mutex is a filesystem object, and, as such, can synchronize activities between multiple processes.

In that regard, be aware that if a mutex has a name, as it must if it is to synchronize more than one process, that name must begin with "\?\GLOBALROOT", unless all of them run in the same session. Failure to do this bit me really hard a few years ago.