Executing a function from an unmanaged dll inside a C # thread

Here is my problem: I have an unmanaged dll that I created. I call one of these dll functions in my C # code using PInvoke. Under certain conditions, in a dll function, I want to be able to terminate the thread that launches the function. How can i achieve this?

Sorry if this is a stupid question, but I'm a complete noob when it comes to streaming.

+3
source share
3 answers

Man, that sounds dangerous. Why finish the thread? You may be in a thread that you do not have, depending on how you sort the call. In any case, it seems your best bet is to return something to indicate that you want to terminate the thread and process the managed code gracefully. Do not try to kill the thread in your own code.

+3
source

If I understand you correctly, your situation is like this (using pseudocode for brevity)

C#
==

Main()
{
   StartNewThread(MyThread);
   DoStuff();
}

MyThread()
{
    UnmanagedDll.DoSomething();
}

Unmanaged DLL
=============
DoSomething()
{
    // Does something here
}

Is it correct?

If so, then returning from UnmanagedDll.DoSomething () will end the call to the calling thread if there are no statements after calling UnmanagedDll.DoSomething (). So, just return control back to the C # program.

If this is not your situation, please provide more information.

+3
source

I would return a special result from an unmanaged DLL function and (finding a special return value) terminate the stream in C #. Terminating it in a DLL sounds like a very bad idea to me.

0
source

Source: https://habr.com/ru/post/1735649/


All Articles