The Wayback Machine - https://web.archive.org/web/20111013041757/http://www.codeguru.com:80/forum/showthread.php?threadid=462452
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com eBook Library


Newest Articles at CodeGuru.com:

  • Multi-targeting your Windows Phone Application to Run on 7.0 and 7.5 (Mango)
  • Using Facebook and Twitter Helpers in WebMatrix
  • SpinWait.SpinUntil and the Task Parallel Library
  • Add Flare to Your Windows Phone 7 (WP7) App with Simple Animations





  • Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 3rd, 2008, 05:08 AM
    Junior Member
     
    Join Date: Aug 2006
    Posts: 20
    amit_pansuria is an unknown quantity at this point (<10)
    Invoke or beginInvoke cannot be called until the window handle has been created.

    Hi all,

    I m getting above metnioned error intermidently.

    Scenario:
    I am calling splash screen form from separate thread and then calling login screen.

    Find here with attached code snippet.
    Attached Files
    File Type: zip Src.zip (3.3 KB, 174 views)
    Reply With Quote
      #2    
    Old October 3rd, 2008, 05:44 AM
    Senior Member
     
    Join Date: May 2007
    Posts: 1,506
    Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)Mutant_Fruit is a name known to all (1000+)
    Re: Invoke or beginInvoke cannot be called until the window handle has been created.

    I assume you also disabled the cross threading calls exception? Reenable it, then fix your code.

    Never call winforms code from anything other than the main thread.
    __________________
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
    Reply With Quote
      #3    
    Old October 3rd, 2008, 07:52 AM
    darwen's Avatar
    Elite Member
    Power Poster
     
    Join Date: Jan 2002
    Location: Scaro, UK
    Posts: 5,918
    darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)darwen has a brilliant future (2000+)
    Re: Invoke or beginInvoke cannot be called until the window handle has been created.

    Quote:
    I assume you also disabled the cross threading calls exception? Reenable it, then fix your code.

    Never call winforms code from anything other than the main thread.
    He's doing the right thing - i.e. using Invoke/BeginInvoke to ensure that windows forms messages occur on the correct thread. However there appears to be a race condition occurring.

    The condition arises because of the following two parts of the code :

    Code:
    Thread splashThread = new Thread(new ThreadStart(ShowSplash));
    splashThread.Start();
    // ...
    objfrmSplash.Invoke(d); // point A
    
    //...
    
    // This is executed in a separate thread (see above)
    static void ShowSplash()
    {
        objfrmSplash = new frmSplash();
        objfrmSplash.ShowDialog();
    }
    Your code is getting to to point A before the thread has been executed and/or the dialog created and shown. That's a race condition.

    You need to synchronise the calls - i.e. you have to ensure that the form has been shown & created before you call Invoke.

    You can use an event to do this :

    Code:
    class Program
    {
        static public string strCulture;
        static public Form objfrmSplash;
        static public ManualResetEvent _splashFormShown = new ManualResetEvent(false);
    
        // ...
    
            if (login.StartSession())
            {
                Form mainApp = new frmMainApp();
                _splashFormShown.WaitOne(); // wait until form has been created
                objfrmSplash.Invoke(d);
                d = null;
                Application.Run(mainApp);
            }
            else
            {
                _splashFormShown.WaitOne(); // wait until form has been created
                objfrmSplash.Invoke(d);
                d = null;
                Application.Exit();
            }
    
    // ....
    
    static void ShowSplash()
    {
        objfrmSplash = new frmSplash();
        objfrmSplash.Load += new EventHandler(objfrmSplashLoadedEvent);
        objfrmSplash.ShowDialog();
    }
    
    static void objfrmSplashLoadedEvent(object sender, EventArgs args)
    {
        _splashFormShown.Set();
    }
    Get the idea ? You hook into the form's loaded event (by which time the handle will have been created) and then signal the main thread that it's safe to proceed.

    In future if one thread makes assumptions about the state of another thread you HAVE to ensure this with a signal as I've shown. Otherwise you'll get intermittent errors when the assumption you have made is not true and you get a race condition.

    Darwen.
    __________________
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

    Last edited by darwen; October 3rd, 2008 at 07:55 AM.
    Reply With Quote
      #4    
    Old October 5th, 2011, 12:30 AM
    Junior Member
     
    Join Date: Oct 2011
    Posts: 2
    AbhiZ is an unknown quantity at this point (<10)
    Re: Invoke or beginInvoke cannot be called until the window handle has been created.

    Follow these step to resolve the above problem

    1) Restart the computer.
    2) Start the SQL SERVER 2008 setup.
    3) Setup window will apear just focus on it and installation setup will be started in few second.
    If it displaying the error message again again perform these above steps.

    Enjoy the installation..........

    For more query mail me at:-
    abhizprogrammer@yahoo.com
    Reply With Quote
      #5    
    Old October 5th, 2011, 06:29 AM
    Elite Member
     
    Join Date: Jul 2008
    Location: WV
    Posts: 3,195
    DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)
    Re: Invoke or beginInvoke cannot be called until the window handle has been created.

    Not only is this a really old thread but the post above which has revived it has nothing to do with the topic at hand.
    __________________
    Alwyas use [code][/code] tags when posting code.
    Reply With Quote
      #6    
    Old Today, 08:07 AM
    Junior Member
     
    Join Date: Oct 2011
    Posts: 1
    Sunil Ganta is an unknown quantity at this point (<10)
    Re: Invoke or beginInvoke cannot be called until the window handle has been created.

    uncompress it with zip or Rar compression utility on desktop and try
    Reply With Quote
      #7    
    Old Today, 04:33 PM
    Elite Member
     
    Join Date: Jul 2008
    Location: WV
    Posts: 3,195
    DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)DataMiser is a name known to all (1000+)
    Re: Invoke or beginInvoke cannot be called until the window handle has been created.

    Try what? Why? The thread is 3 years old I doubt the OP is still waiting for an answer.
    __________________
    Alwyas use [code][/code] tags when posting code.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Display Modes Rate This Thread

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off


    All times are GMT -5. The time now is 11:17 PM.