0

Can we interrupt a method while its working?Because I have a method that in a thread and I have to interrupt that method when button click. How can I do that?

I tried Abort() method but its not working. Because Abort() is a method which don't have any garanty behavior. I mean if we use that method, we don't sure about method termination.

I have just a method name like DoSomething() because I use a DLL which is written in C++. Hence I dont have a source code of method. And if I click a button, this method must be terminated

Please give me some advice about it.

ceyun
  • 351
  • 4
  • 14
  • http://msdn.microsoft.com/en-us/library/dd997396%28v=vs.110%29.aspx – ikh Aug 22 '14 at 11:14
  • Post some code: Hard to determine how your thread is working.. Because there are so less possibilities how it is implemented – Mark Aug 22 '14 at 11:16
  • possible duplicate of [Stopping a Thread, ManualResetEvent, volatile boolean or cancellationToken](http://stackoverflow.com/questions/13476528/stopping-a-thread-manualresetevent-volatile-boolean-or-cancellationtoken) – Eugene Podskal Aug 22 '14 at 11:28

3 Answers3

1

If you have access to the method implement a flag to abort it and code your way around the problem. This is a conceptual pseudo-C# example of what I'd do:

private bool aborted = false;

public ResultClass Method()
{
    for (i = 0; i < int.MaxValue && !aborted; i++)
    {
      // ...
    }

    if (aborted)
    {
        return null;
    }

    return new ResultClass(...);
}

public void Abort()
{
    aborted = true;
}

This might look like a lot of code but it is cleaner, you can clean-up and handle stuff as you need it. Aborting a thread, instead, is a messy affair and can only happen when the thread hits a WaitSleepJoin state.

If you really need to abort, at least do something like this:

try {
    // abortable code
}
finally
{
    // clean-up resources
}
pid
  • 11,472
  • 6
  • 34
  • 63
  • if I use this code, I cannot escape to out of method. This method just usable instead of Abort() method. Am I right? – ceyun Aug 22 '14 at 13:01
  • I have just a method name like DoSomething(). And if I click a button, this method must be terminated – ceyun Aug 22 '14 at 13:02
  • Better to use a `ManualResetEvent` or some such to do this, or an `Interlocked.Exchange/Interlocked.Read` – Moo-Juice Aug 22 '14 at 13:50
  • Yes, it may be better but really performance and readability/maintainability has to be taken into account when multithreading is involved, so as simple or complex a solution it totally depends on many factors how to best solve such a problem. – pid Aug 22 '14 at 14:01
0

you can use Abort on a thread and not on a method. If you want to interrupt a method use return.

man_luck
  • 1,605
  • 2
  • 20
  • 39
0

I have solved the problem. If someone has problem like that, maybe it can find an answer in following pages

I got some information about my question in below link

Abort call to unmanaged DLL

I found an example about my solving way

Using kernel32 CreateThread/TerminateThread inside Azure Worker Role

And you can use this page. This page contains a lot of code part about PInvoke.

http://www.pinvoke.net/

Community
  • 1
  • 1
ceyun
  • 351
  • 4
  • 14