3

I look trough google a bit but cant make it work.

I have some process done when i push a button.

I want to add a "Kill All" button to terrminate everying when pushed, but when i start a process i cant push any other button untill its finnished.

private void button_checkZero_phones_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
        thread.Start();
    }

private void button_kill_all_Click(object sender, EventArgs e)
    {
        System.Environment.Exit(1);
    }
susparsy
  • 1,016
  • 5
  • 22
  • 38

3 Answers3

2

You have to run your "Kill All" method in an async method. The main thread is always blocked when you run a long process.

More info about your problem: WinForm Application UI Hangs during Long-Running Operation

Just use one of the following methods - run Kill All in:

  1. A BackgroundWorker
  2. Another thread
  3. Another task
  4. Use async/await
  5. Reactive extensions

There are a several ways to notify you that the all processes are terminated! It depends on which method you use.

Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • i have another problem now, the worker works like a query, and when i push the button once it runs once, but when i push it again it is running twice – susparsy Aug 28 '13 at 10:54
  • 1
    disable the button when it clicked! – Bassam Alugili Aug 28 '13 at 11:04
  • orfouce i did, but after the process is done i enable it again.. and if i push the button again it doest the work 2 times, and than 3 times... – susparsy Aug 28 '13 at 11:06
  • 1
    post your code you said you gona use backgraound worker you must despose it after using: http://stackoverflow.com/questions/2542326/proper-way-to-dispose-of-a-backgroundworker – Bassam Alugili Aug 28 '13 at 11:09
0

Use async funcations. I found these useful links

http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

http://msdn.microsoft.com/en-us/library/vstudio/hh156513.aspx

Ajay
  • 6,418
  • 18
  • 79
  • 130
0

Is this a Windows app? If you're feeling old-school and enjoy winding up other developers, stick an Application.DoEvents() in your loop of death.

You know you want to.

Of if you want to do it without receiving scorn from all your peers, see this question and answer.

Community
  • 1
  • 1
Zac
  • 1,722
  • 1
  • 19
  • 22