progress bar

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

How to use progressBar with loading picture into pictureBox.

Code:

private void InitializeMyTimer()

{

// Set the interval for the timer.

time.Interval = 250;

// Connect the Tick event of the timer to its event handler.

time.Tick += new EventHandler(IncreaseProgressBar);

// Start the timer.

string fName = "F:\\ReportLogo\\pictures\\Coffee Bean.bmp";

try

{

time.Start();

pb.Image = System.Drawing.Image.FromFile(fName);

}

catch (Exception x)

{

MessageBox.Show(x.Message);

}

}



What's wrong?
 
pb.Image = System.Drawing.Image.FromFile(fName);

I suspect that your UI thread is waiting for the this operation to complete;
one option is to obtain the image on a different thread, and then sync back
afterwards. Also note the use of a marquee to avoid a timer.

Marc


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

class Program
{
class ImageForm : Form
{
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ImageForm());
}
private ProgressBar pb;
public ImageForm()
{
Button button = new Button();
button.Text = "Load";
Controls.Add(button);
button.Click += new EventHandler(button_Click);
pb = new ProgressBar();
pb.Dock = DockStyle.Bottom;
pb.Style = ProgressBarStyle.Marquee;
pb.Visible = false;
Controls.Add(pb);
}

void button_Click(object sender, EventArgs e)
{
pb.Visible = true;
ThreadPool.QueueUserWorkItem(LoadImage);
}
private void LoadImage(object state)
{
Thread.Sleep(5000); // to make it look slower to demo
Image img = Image.FromFile(@"C:\WINDOWS\Coffee Bean.bmp");
Invoke((MethodInvoker) delegate {
BackgroundImage = img;
pb.Visible = false;
});
}
}
}
 
Thank you.

Marc Gravell said:
I suspect that your UI thread is waiting for the this operation to
complete; one option is to obtain the image on a different thread, and
then sync back afterwards. Also note the use of a marquee to avoid a
timer.

Marc


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

class Program
{
class ImageForm : Form
{
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ImageForm());
}
private ProgressBar pb;
public ImageForm()
{
Button button = new Button();
button.Text = "Load";
Controls.Add(button);
button.Click += new EventHandler(button_Click);
pb = new ProgressBar();
pb.Dock = DockStyle.Bottom;
pb.Style = ProgressBarStyle.Marquee;
pb.Visible = false;
Controls.Add(pb);
}

void button_Click(object sender, EventArgs e)
{
pb.Visible = true;
ThreadPool.QueueUserWorkItem(LoadImage);
}
private void LoadImage(object state)
{
Thread.Sleep(5000); // to make it look slower to demo
Image img = Image.FromFile(@"C:\WINDOWS\Coffee Bean.bmp");
Invoke((MethodInvoker) delegate {
BackgroundImage = img;
pb.Visible = false;
});
}
}
}
 
A meaningless progress bar is worse than no progress bar at all. If all
you need is to let the user know your app is "thinking" and not frozen,
consider just displaying a animated circle graphic.

Think of installing office 2000; it was "done" five times in a row.
Think of installing IE7 "WTF is this little block going back and forth?"

The apple mac "petal" style indicator is quite nice and non-annoying in
these situations. My thunderbird nntp client has even stolen it. ;)

~Jason

--
 

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

Back
Top