1

My Application C# calls a C++ dll that runs a slow calculations. So I call the dll inside a thread, and that all works fine.

But how can I stop the thread if the user wants to? I can not tell the dll to stop. The only way i found was to use a volatile bool and check it's value periodically.

The problem is that the Boolean value changed only after some times or after a MessageBox is shown.

C#

 public void AbortMesh()
            { 
                if (currMeshStruct.Value.MeshThread != null && currMeshStruct.Value.MeshThread.IsAlive)
                {                
                    MeshCreator.CancelMesh();
                }
            }

C++

public class MeshCreator
{

    private:
        volatile BOOL m_StopMesh;
    ...
}

STDMETHODIMP MeshCreator::CancelMesh(void)
{
    this->m_StopMesh = TRUE;
    return S_OK;
}

void MeshCreator::ProcessMesh(void)
{
    Int32 processedParts = 0;

    while(processedParts != totalPartsToProcess)
    {
        ContinueProcessing(processedParts);
        processedParts++;

        if (this->m_StopMesh)
        {
            // ExitProcess();
        }      
    }

}

CancelMesh() called while the receiving thread is executing long operations, thus it will be stored for later (in the queue, waiting to be processed).

If the thread is done with its operation, it will come back and call the CancelMesh(), and immediately can process it.

I need a way to simply to execute the CancelMesh() method when it's called not after some time or after a MessageBox.

MRebai
  • 5,344
  • 3
  • 33
  • 52
  • Are you sure the delay is because of the message-box? Maybe the delay is because the 'ContinueProcessing()' method takes time and examine the boolean only once in a while? – PazO Jun 30 '14 at 14:06
  • No the 'ContinueProcessing()' is just a simple example of what i need to, but i tested that Boolean in many places – MRebai Jun 30 '14 at 14:09
  • Your code is just fine. If you call `CancelMesh` then, in due course, when the C++ code checks the value of the flag, the thread will be able to abort. – David Heffernan Jun 30 '14 at 14:09
  • Which .Net version do you use? – Shlomi Borovitz Jun 30 '14 at 14:09
  • @ShlomiBorovitz i used .Net 4 – MRebai Jun 30 '14 at 14:11
  • @DavidHeffernan the problem that when the cancel event is sent it ll call abort mesh which will call cancelMesh but the excution of cancelmesh occur only after the process endede – MRebai Jun 30 '14 at 14:14
  • 1
    Well, call `CancelMesh` as soon as you decide that you wish to terminate. It's really that simple. – David Heffernan Jun 30 '14 at 14:16
  • @DavidHeffernan do you think that it's as simple as that i did that but as i mentioned the cancelmesh function need to have some priority to stop the process of calcul and set the m_stopMesh – MRebai Jun 30 '14 at 14:19
  • @moez Is the C++ dll native C++, or C++/CLI? Can you change it if you need to? – Shlomi Borovitz Jun 30 '14 at 14:20
  • Nope. It's just that simple. Call the function. End of story. – David Heffernan Jun 30 '14 at 14:25
  • @moez The more I look at your code, the more I convinced that David Hefferman is right. Even though you check the boolean in many places - it doesn't mean that the processing between those places is short. I think you should profile the C++ code... Otherwise, and if there is actually a message loop in here - run the processing on a different thread than that of the message loop. Just like you would in UI, or any other case where message loop is used. – Shlomi Borovitz Jun 30 '14 at 14:34
  • no the processing between the places where i check my boolean is very short that's why it looks weird. the function cancel mesh is executed only when i show a messagebox or at the end of my process i m really stacked and i don't know how to resolve it – MRebai Jun 30 '14 at 18:22
  • 1
    @moez The problem is not with your task and its behaviour, the problem is that no one here can't comprehend what's going on and what should be going on in your application - http://stackoverflow.com/questions/24456866/detect-boolean-value-changes-inside-thread. You should describe your system more elaborately and post at least some parts of your working or equivalent code. No offense intended, just your app is still a complete mystery for me. – Eugene Podskal Jun 30 '14 at 19:23

0 Answers0