-1

I implemented a windows form in c# with a progress bar in marquee style and a backgroudworker to do a job. The progress bar animation is working correctly when the backgroundworker sleeps, but it hangs when the backgroundworker starts to do something. Does anyone know what I am doing wrong?

Thanks in advance for your answers.

Here is my code:

public FormProgressBarMarquee()
    {

        InitializeComponent();

        this.progressBar1.Style = ProgressBarStyle.Marquee;
        this.progressBar1.MarqueeAnimationSpeed = 50;

        Shown += new EventHandler(FormProgressBar_Shown);
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_Completed);
    }

    void FormProgressBar_Shown(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        System.Threading.Thread.Sleep(10000);
        // the progress bar animation works correctly

        longtimerunningprocess.start();
        // the progress bar animation stops           
    }

    void backgroundWorker1_Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        Debug.Print(" :: FormProgressBar :: ...Pack And Go loaded, close form...");
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
Chris
  • 1
  • 3

1 Answers1

0

I could finally solve the problem myself. Thanks anyway for the comment @digvijay which put me on the right track.

The program I am writing is part of a class library. longtimerunningprocess is part of the main program and is executed in the main UI thread.

Although I always try to keep the splash screen in the main UI thread and put the non UI work into a separate thread using a backgroundworker, this time I had to put the splash screen in a separate thread.

To do so, I used this suggestion:

https://stackoverflow.com/a/48946

Community
  • 1
  • 1
Chris
  • 1
  • 3
  • Glad it worked for you without complete code - we can just contemplate the reason which in this case were very few - running on UI thread is always seems to be the issue! – Digvijay May 03 '17 at 08:22