简体   繁体   中英

how to close main thread from sub thread

I am working on VSTO. I had a long time consuming process so that i need to implement a stop button to stop the process. so that i create a UI form with stop button and show that UI form in a sub thread when my main process is called. Now i am trying to stop the main process from UI stop button but however i tried it closed all process and the document its self but i need to stop that process only i have tried these for closing process but it not work properly

Enviorment.Exit(1);
process.Kill();
process.Close();
Close();

I am not sure why you are calling a thread "process", but the question is about a sub thread, so I'll answer it for threads.

If you want to end your thread, make it regulary test a public bool, defined in your dialogbox class as "public volatile bool stopRequested = false;" If the bool is set to true, just use the "return" keyword to stop the thread.

To abort the thread from your dialog box, just enter to the btn_Click event: "this.stopRequested = true;"

The thread will now realise the use wants it to stop, and it will jump to the line "return". Thread closed! \\o/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM