0

I have a tricky problem, I'm doing some automated UI-Test in C# in a background-thread and now i'm wondering, what's the best way to stop it.

I know that I absolutely shouldn't use Thread.Abort(), but the big problem is i also cannot use a flag, because one testing loop takes about a half hour and it should be stopped instantly.

The only solution to which i have come is to check a flag after every step, which will be about 100 times and will also be to slow, because some steps need some seconds to be done.

Does anyone know another possible solution without using Thread.Abort()?

  • What is your code doing during these seconds of such a step? If it's _waiting_ you could try rewriting your code to accept a `CancellationToken` to immediately stop the wait operation. – C.Evenhuis Jan 16 '17 at 13:21
  • _"one testing **loop** takes about a half hour"_ - What do you mean by loop. if it's like a `for` loop, can't you just test for cancellation token/flag/manualresetevent? Or by _"loop"_ do you mean a _phase_ in which takes 1/2 hour and has no way to periodically check for a cancellation indication? –  Jan 16 '17 at 13:25
  • 1
    Your only two choices, really, are *some* form of cooperation or ripping the rug out from under the thread. The problem usually is that if you're going to rip the rug out, you have *no* idea of what the thread was doing, what state it's left inconsistent, etc. – Damien_The_Unbeliever Jan 16 '17 at 13:25
  • @C.Evenhuis It's searching for some UI-Controls (With UIAutomation.dll from Microsoft, i cannot change these function, so all i can do is to wait until it is finished), but if there are many Controls on a form, it could take up some seconds. –  Jan 16 '17 at 13:25
  • @MickyD One loop is a complete GUI-Test, with many steps, if you mean with cancellation flag => Testing after every step for this flag, it's like i wrote in my question, i would't have to do this about 100 times so i think this will be a very dirty way to program –  Jan 16 '17 at 13:29
  • @Essigwurst: Like i have mentioned, it's not these sort of thread, one loop contains many steps and takes about 30 minutes, so i unfortunately cannot use something like this –  Jan 16 '17 at 13:34
  • 2
    _"so i think this will be a very dirty way to program"_ - well no that's the due-diligent way to program. If something is going to take a long time to process, whether its async or not, you should provide a mechanism to cancel it. `Abort()`ing a thread is _"dirty way to program"_. –  Jan 16 '17 at 13:35
  • Exactly, i don't want to use Abort() but i thought someone knows a better way than checking a flag after every step, i haven't found an answer anywhere so asked for it, but it seems that there is no other way to do this, so i will have no other choice :( Thanks for all the answers ;) –  Jan 16 '17 at 13:40
  • 2
    There are a ton of ways. `CancellationToken`; `ManualResetEvent`; _good ol' flag_. Take your pick –  Jan 16 '17 at 13:43

0 Answers0