0

In my solution I got a user interface where some word automation is started by a buttonclick (lets call that button wordStart). I want to break this word automation with another buttonclick (lets call that button 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. 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. 1. Is it possible to stop the execution of code with a single threaded application? 2. How do I stop the execution of code?

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
    }
4

2 回答 2

0

只是想在这里发布我对自己问题的回答,以防有人遇到类似问题。正如其他人在这个问题上所建议的那样,我无法使用后台工作人员来实现它,因为它不允许使用剪贴板等 OLE 功能 - 但这特定于我的线程正在处理的内容。后台工作者在很多情况下肯定很有用 - 但它不能设置为 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();
    }
于 2014-10-01T12:20:19.970 回答
0

不,通过仅使用单个线程,它会被阻塞,直到执行完成。如果您希望能够取消/暂停它,则需要使用另一个线程进行操作。例如,您可以使用BackgroundWorker.

于 2014-09-30T11:54:02.220 回答