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
.