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
    }
n.Stenvang
  • 487
  • 2
  • 6
  • 21
  • You can't really do it on single-threaded and therefore multi-threading is the way to go. The methods you mention are nothing to do with what you are trying to achieve! If you are using version 4 or higher of .NET you could look at TPL (Tasks) to make multi-threading easier. http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx – Belogix Sep 30 '14 at 11:54
  • Start a new thread on button click, http://msdn.microsoft.com/en-in/library/aa645740(v=vs.71).aspx – CodingDefined Sep 30 '14 at 11:54
  • Can you show your code? – Patrick Allwood Sep 30 '14 at 11:55
  • I will add some code to my question in case people want to help me more with this. But from walthers answer and the 2 other comments I think I got what I needed. Now I just need to rethink the architecture of my program. – n.Stenvang Sep 30 '14 at 12:14

2 Answers2

0

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.

walther
  • 13,466
  • 5
  • 41
  • 67
  • Just as I thought even though I had hoped it could be done with a single thread. I must rethink the architecture of my program now. – n.Stenvang Sep 30 '14 at 11:55
  • 1
    If you're trying to steer someone learning multithreading and asynchronous programming in the right direction, Task Parallel Library is a more sensible choice, because it's definitely not going anywhere any time soon (unlike `BackgroundWorker` which has already been dropped from PCL and Windows Store apps AFAIK). – Kirill Shlenskiy Sep 30 '14 at 13:05
  • @KirillShlenskiy, where did you get the info about not being able to use BW in those situations? MSDN doesn't say anything along those lines AFAIK and BW is generally easier to use and to understand for beginners compared to TPL. Also please note I've supplied BW only as an example, not a definitive solution. The most important part are those first 2 sentences.. – walther Sep 30 '14 at 16:54
  • Btw, not sure who downvoted me, but you should at least provide an explanation why do you think my answer is not valid... – walther Sep 30 '14 at 16:57
  • @walther, my comment was based on MSDN specifically listing "*Supported in: Portable Class Library, Supported in: Windows 8 .NET for Windows Phone apps*" on the documentation page for `Task` ("*Version Information*" section), but not `BackgroundWorker`. Your comment made me think that perhaps those pages don't always stay up-to-date, and I tested my assumption on a PCL project (Profile 49). Indeed, `BackgroundWorker` isn't there. – Kirill Shlenskiy Sep 30 '14 at 22:37
  • @KirillShlenskiy, PCL doesn't have multiple references available, but that doesn't mean MS is dropping support for those nor anything along those lines. Purpose of PCL is to provide a library that could be shared among multiple platforms. Not sure about you, but I certainly wouldn't handle multithreading operations in such a library.. That is quite a specific task and should be handled accordingly: platform-dependent. I don't see BackgroundWorker going away any time soon, nor MS marks it as obsolete.. – walther Oct 01 '14 at 00:07
  • Don't know who downvoted you either. One shouldn't be able to downvote without leaving a comment on why he/she downvoted - it doesn't benefit anyone just to downvote. I will say that this answer was all I needed and therefore it must be a good answer. However I will wait I little time to mark it as accepted cause I could still need a little help on the implementation site. – n.Stenvang Oct 01 '14 at 06:32
0

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. 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.

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();
    }
n.Stenvang
  • 487
  • 2
  • 6
  • 21