Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Except doevent() is another way that, in this infinite loop run another control in form.

I wanted to do it With backgroundworker, but I failed.!

private void button1_Click(object sender, EventArgs e)
     {
         while (true)
         {

         }
     }
Posted
Updated 1-Feb-11 10:30am
v2
Comments
Henry Minute 1-Feb-11 16:31pm    
I think that you will need to actually ask a question, if you want some help.

There is no question in your message.

If I have understood what you want to do correctly, you need to replace your while loop and whatever code it contains by a BackgroundWorker.

You state that you tried a BackgroundWorker and failed but it is very difficult to give more advice without seeing the code inside the while loop and what the BackgroundWorker you tried looks like.
 
Share this answer
 
A reasonable sample of how to work with BackgroundWorker is shown in my other answer:
Changes step by step to show a control[^].

—SA
 
Share this answer
 
Comments
JF2015 3-Feb-11 2:34am    
Another good answer. You slowly build your own library of answers, so that you can always refer to a previous answer of yourself. 5+
Hello,

Your question is a bit unclear, so it is difficult to give an definite answer.

If it helps this is an example of how you could use a background worker:

C#
BackgroundWorker backgroundWorker;
public Form1()
{
    InitializeComponent();
    backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
    backgroundWorker.WorkerSupportsCancellation = true;
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    while (true)
    {
        // do something...
        Console.WriteLine(DateTime.Now.ToLongTimeString());
        Application.DoEvents();
    }
}
private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
    backgroundWorker.CancelAsync();
}


Obviously there are other ways to do it.

Hope it helps,

Valery.
 
Share this answer
 
Comments
Henry Minute 1-Feb-11 17:29pm    
Why put a DoEvents call in a BackgroundWorker which is already in a different thread from the UI?
Valery Possoz 1-Feb-11 17:50pm    
Maybe I wrote a bit too quickly, anyway. voting 1 is a bit harsh!

If you have somewhere else in the code something like:
while(backgroundWorker.IsBusy)
{
// some code...
}
I experienced that adding DoEvents() in the loop will make backgroundWorker.CancelAsync() work, otherwise it might not work.

while(backgroundWorker.IsBusy)
{
// some code...
Application.DoEvents();
}
Olivier Levrey 2-Feb-11 3:56am    
Using Application.DoEvent is indeed not recommanded at all. You might want to have
a look to my article "Fixing BackgroundWorker IsBusy property" instead.
But it is true voting 1 is harsh...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900