0

In a classic single thread winform application I would like to know if a .NET method can be interrupted by another one.

I know that in some case like for example with the instruction Thread.Sleep(1000); or Application.DoEvents(); then a Timer_Tick can "take" the execution, but is there another case where a method will be interrupted ?

Also if the method is interrupted I would like to know when the execution will come back to the previous method ? At the end of the Timer_Tick ? (if Timer_Tick toke the execution)

16
  • It sounds like you need to cancel a method, which is what Cancellation Tokens are for (but not required to use). You need to have another thread of your application changed a shared variable (like bool stopRunning = true), and your main method periodically checks that it should stop mid flight. There isn't a way to hijack you way into the method. stackoverflow.com/questions/744013/… Commented Sep 25, 2019 at 16:09
  • no I absolutely don't want to cancel any method, and actually I would like all my method not being interrupted in any way but I don't know hows to do that as it appear that they are... Commented Sep 25, 2019 at 16:12
  • 1
    Thread.Sleep(1000); doesn't "interrupt" a method any more than counting to 1 billion 1 billion times does. It just take a long time to execute that one line. Commented Sep 25, 2019 at 16:15
  • 1
    Timer ticks won't be processed during Thread.Sleep if you use a WinForms timer. Other types of timers will allow it. Commented Sep 25, 2019 at 16:22
  • 2
    @fasa - When you call DoEvents(), it runs the Windows Message Pump, and Messages are dispatched, in the case of WM_TIMER messages, these call your Timer_Tick function. This is really no different from whether you have another method (say DoSomething()) that itself calls another method. Nothing is suspended, it just goes off on another function call, and returns back afterwards. Commented Sep 25, 2019 at 16:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.