locked
Invoke or BeginInvoke cannot be called on a control until the window handle has been created. RRS feed

  • Question

  •  

    Hi ,
    I have a problem when I run an application

    the red line in the  code cause a error message below:




            private void ProcessTrafficData(XmlDocument xmlTrafficData, WebHeaderCollection headers)
            {
                //instantiate a new array of delegate type with null value
                Delegate[] delegateList = null;
                EventArgs eventArgs = null;
                if (TrafficEvent != null)
                {
                    //fill delegatelist array from the current delegates from RequestEventHandler
                    delegateList = TrafficEvent.GetInvocationList();
                    eventArgs = new TrafficEventArgs(xmlTrafficData, headers);
                    if (delegateList != null)
                    {
                        //invoke delegates within their threads
                        foreach (Delegate d in delegateList)
                        {
                            try
                            {
           bool b = (d.Target as ISynchronizeInvoke).InvokeRequired;
           

           object[] obj = { this, eventArgs };
           (d.Target as ISynchronizeInvoke).Invoke(d, obj);


           
        
                            }
                            catch (Exception ex)
                            {
                                TrafficEvent(this, (TrafficEventArgs)eventArgs);
                                MessageBox.Show("Exception :" + ex.Source + ex.Message + ex.StackTrace);
                            }
                        }
                    }
                }
            }


    -----------

    System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has

    been created.
       at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean

    synchronous)
       at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
       at UCAjaxSample.UserAgent.ProcessTrafficData(XmlDocument xmlTrafficData, WebHeaderCollection headers) in line 745

    -----------



    This code is a sample code from Microsoft Downloads site , about
    Unified  Communications 2007.
     

    Can you help me?

    Thanks!


    JSantos
    Friday, August 8, 2008 1:37 PM

Answers

  • You are not using the value of InvokeRequired.  However, that is not likely to be the real problem.  You've got a form instance on which Show() wasn't called.  That's a common mistake, brought on by code like this:

      Form2 frm = new Form2();
      frm.textBox1.Text = "Hello";


    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, August 13, 2008 10:39 AM
    Saturday, August 9, 2008 7:36 PM

All replies

  • You need to force the creation of the handle, something like this (VB code):
    mForm.Handle.ToInt32()

    Just by referencing this Handle will make sure that your invoke will work.
    Friday, August 8, 2008 5:05 PM
  • You are not using the value of InvokeRequired.  However, that is not likely to be the real problem.  You've got a form instance on which Show() wasn't called.  That's a common mistake, brought on by code like this:

      Form2 frm = new Form2();
      frm.textBox1.Text = "Hello";


    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, August 13, 2008 10:39 AM
    Saturday, August 9, 2008 7:36 PM