[英]Is it possible to stop single threaded execution of code from UI?
In my solution I got a user interface where some word automation is started by a buttonclick (lets call that button wordStart). 在我的解决方案中,我得到了一个用户界面,在该界面上,通过clickclick(使该按钮称为wordStart)启动了一些单词自动操作。 I want to break this word automation with another buttonclick (lets call that button wordBreak). 我想用另一个buttonclick(让该按钮称为wordBreak)来打破这个词的自动化。
However when I click the wordStart the user interface freezes while doing the work and it's not possible for me to click the wordBreak button. 但是,当我单击wordStart时,用户界面会在工作时冻结,并且无法单击wordBreak按钮。 I'm still a bit new to programming so for me this must be because the application is single threaded or atleast I could solve it with multithreading. 我对编程还是有点陌生,所以对我来说这一定是因为应用程序是单线程的,或者至少可以用多线程解决它。
So this is a 2 in 1 question. 因此,这是2合1问题。 1. Is it possible to stop the execution of code with a single threaded application? 1.是否可以通过单线程应用程序停止执行代码? 2. How do I stop the execution of code? 2.如何停止执行代码?
For question number 2 I looked a bit around the internet and found these methods which I think will work, but other suggestions are welcome: 对于第二个问题,我环顾了一下互联网,发现了我认为可以使用的这些方法,但也欢迎其他建议:
Application.Exit
Application.Shutdown
Environment.Exit
EDIT: As I thought this should be done with multi threading. 编辑:正如我认为这应该使用多线程来完成。 I don't have that much experience with that so I've added this code to the question if anyone would like to help me out here. 我没有那么多的经验,所以我已经将此代码添加到问题中,如果有人想在这里帮助我。 In the meantime I will look for a solution to this myself. 同时,我将自己寻求解决方案。
private void generateButton_Click(object sender, EventArgs e)
{
//Thread or backgroundworker should handle this event?
commandsChosed(); //Event to be throwed - this starts the word automation
}
private void stopButton_Click(object sender, EventArgs e)
{
//Stop/pause the working thread
}
No, by using only a single thread it gets blocked until the execution is finished. 不,通过仅使用一个线程,它会被阻塞,直到执行完成。 If you want to be able to cancel/pause it, you need to use another thread for the operation. 如果希望能够取消/暂停它,则需要使用另一个线程进行操作。 For instance you can use BackgroundWorker
. 例如,您可以使用BackgroundWorker
。
Just wanted to post my answer to my own question here in case anyone had a similar problem. 只是想在这里发布我对自己问题的回答,以防有人遇到类似问题。 As suggested by others on this question I wasn't able to implement it with a backgroundworker since it doesn't allow OLE functions like use of clipboard and such - but this is specific for what my thread is handling. 正如其他人在这个问题上建议的那样,由于它不允许使用诸如剪贴板之类的OLE功能,因此我无法使用backgroundworker来实现它-但这特定于我的线程正在处理的内容。 A backgroundworker could definitely be useful in a lot of situations - but it can't be set to STA since it's from the threadpool. 后台工作人员在很多情况下肯定是有用的-但由于它来自线程池,因此无法设置为STA。
Thread workerThread;
private void generateButton_Click(object sender, EventArgs e)
{
generateButton.Visible = false;
stopButton.Visible = true;
//Setting up a background thread
workerThread = new Thread(new ThreadStart(handleGenerateButtonClick));
workerThread.SetApartmentState(ApartmentState.STA); //In STA state the thread can use OLE functions like clipboard and handle some UI components as well.
workerThread.IsBackground = true; //It shuts down if the mainthread shuts down
workerThread.Start();
try
{
//Checking whether the currentThread is not the workerThread before blocking the currentThread until workerThread has terminated
if (Thread.CurrentThread != workerThread)
{
//Wait until workerThread has terminated
workerThread.Join();
}
//Sets the window buttons after workerThread has finished
if (!workerThread.IsAlive)
{
generateButton.Visible = true;
stopButton.Visible = false;
}
}
catch
{
}
}
private void stopButton_Click(object sender, EventArgs e)
{
generateButton.Visible = true;
stopButton.Visible = false;
//Stops the worker thread
workerThread.Abort();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.