BusyBox Revisited

G

Guest

Hi again experts.

Still having problems with the backgroundworker etc. Here is my test
simulation.

Form1 has a start button
BusyBox has a ‘Please Wait…’ label
Form2 has no controls

When start is clicked, display the BusyBox with a delay. The BusyBox should
also display a progressbar, animated gif or anything that move to indicate
something is happening. After the delay, the BusyBox closes and Form2 is
shown.

Here is my code where I cannot even get the label to display !

Thanks in advance for any help or advice.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs
e)
{
BackgroundWorker bw = sender as BackgroundWorker;
TimeConsumingOperation(bw);

}

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
Form frm = new Form2();
frm.Show();

}

private void TimeConsumingOperation(BackgroundWorker bw)
{
Form frmB = new BusyBox();
frmB.Show();

long i = 0;
while (i != 3000000000)
{
i++;
}
frmB.Dispose();
}

private void startBtn_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}
 
A

Andy

I think your problem is that you're displaying the busy form on the
worker thread... not what you want to do.

Instead, perhaps you can have the process when kicks off the
background worker start a timer to go off in a certain amount of time,
start the BW. That way the UI thread is handling the UI details and
the worker is handling whatever it needs to.

In the ReportProgress callback (if you're using it) you can check if
the busy box is currently shown and if it is, tell it to update its
progress bar.

HTH
Andy
 
G

Guest

Hi Andy,

Many thanks for trying to help. I can't believe something like this is so
difficult. I tried putting the code (see below) on a different thread but
again the BusyBox form will not display the 'Please Wait..' text or gif.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Thread trd = new Thread(new
ThreadStart(this.TimeConsumingOperation));
trd.IsBackground = true;
trd.Start();
}

private void TimeConsumingOperation()
{
Form frmB = new BusyBox();
frmB.Show();

long i = 0;
while (i != 3000000000)
{
i++;
}
frmB.Dispose();
}
}
 
J

Jon Skeet [C# MVP]

jez123456 said:
Many thanks for trying to help. I can't believe something like this is so
difficult. I tried putting the code (see below) on a different thread but
again the BusyBox form will not display the 'Please Wait..' text or gif.

You've still got the form on the same thread as the time consuming
operation though - you do both in TimeConsumingOperation. (And you
don't have a windows message pump on the new thread anyway...)

Create and show the form in your event handler, and start the other
thread to do the actual work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top