Hello,
I have a gui that uses
TSpectrum3 s = new TSpectrum3();
s->SearchFast((const float**) source, dest, Nbins, Nbins, Nbins, Sigma, Threshold, kFALSE, Window);
Because the “search peak” function can take quite a long time, it is run in a separate thread so that the gui is not blocked. I would like to add a “Cancel” button to stop the calculations if the user wants to.
I tried TTread::Kill() and TThread::Delete(), but this does not seem to work (and to be recommended).
I was wondering if Root provides such a functionality that would allow to stop a calculation without
having to write a separate program using pipes, forks and so on (which seems to be the only way I can
figure out at moment)?
Thank You in advance,
Bertrand Roessli
Hi Bertrand,
Take a look at the very simple macro in attachment, it works for me…
Cheers, Bertrand.
Interrupt_Thread.C (1.95 KB)
Hello,
Thanks for the macro.
Interestingly I cannot stop the macro when clicking on STOP… The only way is to close
the terminal.
I have Root 5.31/1
Bertrand Roessli
Yes, I see, it works only on Windows. I’ll check.
In the meanwhile, if you want to test the macro, you can still add these two lines: if (th1 && th1->GetState() == TThread::kCancelingState)
break;
inside the while() loop.
Cheers, Bertrand.
Yes, adding
if (th1 && th1->GetState() == TThread::kCancelingState)
break;
in the while loop works.
Cheers,
Bertrand Roessli
Hi,
As complement of information: Adding TThread::SetCancelOn(); in the user function solves the problem. For our example:[code]void *JobThread(void *)
{
// this is the thread function actually doing the real job,
// allowing the main thread to process events
int i = 0;
TThread::SetCancelOn(); // to allow to terminate (kill) the thread
printf("\n");
while (1) {
TThread::Sleep(0, 50000000);
printf("%c\r", symb[i]);
fflush(stdout);
if (!symb[++i]) i = 0;
}
return 0;
}[/code]
Thanks to Philippe who noticed that the TThread::SetCancelOn(); has to be placed inside the user function (the function called by the thread), and that it was not enabled by default…
Cheers, Bertrand.
Hello,
Yes it works now.
Thanks a lot,
Bertrand Roessli
PS: p 382 of the manual explains that