locked
Why i'm getting exception InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created ? RRS feed

  • Question

  • I have a backgroundworker1 dowork event and inside:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker bgw = (BackgroundWorker)sender;
                    if (bgw.CancellationPending == true)
                    {
                        return;
                    }
                    else
                    {
                        this.BeginInvoke(new MethodInvoker(delegate
                        {
                        timer1.Stop();
                        Button1Code();
                        timer1.Start();
                        trackBar2.Enabled = false;
                        trackBar1.Enabled = false;
                        }));
                        while (true)
                        {
                            bitmaps = ImagesComparison.get_images_with_clouds(b1);
                            for (int i = 0; i < bitmaps.Length; i++)
                            {
                                ConvertTo1or8Bit.BitmapToGIF(bitmaps[i], @"c:\convertedgifs\" + i.ToString("D6") + ".gif");
                            }
                            break;
                        }
                    }
            }

    The exception is on the BeginInvoke part.

    In the last few days i was running the program many times and i didn't have this exception even once.

    And now every times i'm running it i'm getting the exception.

    System.InvalidOperationException was unhandled by user code
      HResult=-2146233079
      Message=Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
      Source=System.Windows.Forms
      StackTrace:
           at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
           at System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args)
           at System.Windows.Forms.Control.BeginInvoke(Delegate method)
           at mws.ScanningClouds.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\ScanningClouds.cs:line 715
           at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
           at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
      InnerException: 
    

    Line 715 is: this.BeginInvoke(new MethodInvoker(delegate

    Wednesday, January 28, 2015 11:24 PM

Answers

  • Move it to the form's shown event and it should solve your problem.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by Chocolade1972 Thursday, January 29, 2015 12:59 AM
    Thursday, January 29, 2015 12:54 AM

All replies

  • What i don't understand is:

    1. Why yesterday and today i was running the program many times and didn't got this exception, and now i'm getting it all the time ?

    2. How to solve it ? 

    Wednesday, January 28, 2015 11:26 PM
  • Maybe the problem is that i'm starting the backgroundworker1 in the form constructor and ishould do it in the Load event ?
    Wednesday, January 28, 2015 11:28 PM
  • Where are you starting the background worker from?  Possibly the constructor of the form?

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Thursday, January 29, 2015 12:37 AM
  • Thomas TSoftware yes. I'm starting it from the constructor of the form.

    It's not form1 it's a new form i show using a button in form1 but the backgroundworker is starting in the form constructor.

    Thursday, January 29, 2015 12:51 AM
  • Move it to the form's shown event and it should solve your problem.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by Chocolade1972 Thursday, January 29, 2015 12:59 AM
    Thursday, January 29, 2015 12:54 AM