5

Possible Duplicate:
C# WinForm Application - UI Hangs during Long-Running Operation

I've created a progress bar example application in C# Windows Forms.

My application runs well but when I tried to move my application position or minimize, it gets "hang" until it's process not complied.

Please help: how to prevent my application from "hang"?

This is my application code:

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

    private void button1_Click(object sender, EventArgs e)
    {
        int value;
        for (value = 0; value != 10000000; value++)
            progressBar1.Value = progressBar1.Value + 1;

        MessageBox.Show("ProgressBar Full", "Done", 
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
        this.Close();
    }

    private void ProgressBar_Load(object sender, EventArgs e)
    {
        progressBar1.Maximum = 10000000;
    }
}
Community
  • 1
  • 1
user1576034
  • 85
  • 2
  • 5
  • 13
  • 3
    Use Background Worker http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx – yogi Aug 04 '12 at 12:22
  • 1
    I don't think the problem is with the `ProgressBar`. Could you show us some code of your application? – matthewr Aug 04 '12 at 12:28

2 Answers2

4

The reason its 'hanging'is because the processes are running in the same thread. Move the progress bars process to another thread using a BackgroundWorker.

To prevent copy + paste, refer to the following links:

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TheGeekZn
  • 3,696
  • 10
  • 55
  • 91
2

The only way to prevent this would be to delegate the workload to another thread.

There are several classes you can use to do so:

Background worker may be the simplest approach as it provides means to return the result to the UI thread. With other options you would need to send the result to the correct thread.

oleksii
  • 35,458
  • 16
  • 93
  • 163