1

I have a normal Windows Forms app, but get the following error:

Invoke or begininvoke cannot be called on a control which is not yet created.

This happens within the following code:

Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var args = new string[0];

new MyApp().Run(args);

I am using a splash screen, which I think is where the problems started.

How is this resolved?

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
blade44
  • 447
  • 1
  • 4
  • 7
  • 1
    This code fragment isn't very useful and isn't complete, want to try again on that? – Lazarus Apr 05 '11 at 13:29
  • if you take the `new MyApp().Run(args)` line out, that should solve the problem; or to be a bit more helpful, what @Lazarus said – Andras Zoltan Apr 05 '11 at 13:31
  • 2
    Yes, that's where it probably started. Not posting that code == no solution. Use the built-in .NET support for splash screens instead of cooking your own: http://stackoverflow.com/questions/392864/c-splash-screen-problem/393870#393870 – Hans Passant Apr 05 '11 at 14:37

2 Answers2

1

Why are you passing arguments to your own application? The default entry point definition for a Windows Forms application does not accept any arguments, so have you altered this manually?

Addressing what seems to be your problem, though, try using this instead:

Application.Run(new YourMainForm());

So, ultimately:

Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new YourMainForm());

In order to retrieve the arguments passed to the application by the executer you can use the Environment class which exposes the GetCommandLineArgs method. So should you need to determine these values you can either A) call this method, parse and then store in a strongly typed format by means of properties or something similiar, or, B) call this method, parsing on demand, say, from within your form.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • The reason for that line, is that is how the splash screen is invoked. – blade44 Apr 05 '11 at 14:14
  • Isn't your splash-screen a `Form`? Start the application with a `new` splash screen form instance. – Grant Thomas Apr 05 '11 at 14:17
  • I did just try that, but what's the best way to close it and show the mainform? I can't get a reliabile transition, they seem to both close etc. – blade44 Apr 05 '11 at 14:18
  • Well, it's hard to give you an ideal solution since I don't know your project and there is limited time and space available here, but one way is to have your splash-screen do whatever it needs to do to load before loading the main form; another way is to load the main form initially, show the splash screen, hide the main form, hide the splash screen and re-show the main form - all timed appropriately, obviously. – Grant Thomas Apr 05 '11 at 14:20
0

An old post that I've just hit - it appears there is an MS bug that rears its head, almost randomly it seems, though some machines seem to be worse affected than others.

Thanks to Mr Gallagher for pointing me at the MSDN social article with the work-around.

The trick is to help the Framework realise when the splash screen is actually available by not letting it hide the splash until it's been properly created.

So add a SplashLoadFinished flag as a user setting or similar.

Then set this flag in the Splash Form.Load event (the lines below must be the first and last lines respectively):

Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    My.Settings.SplashLoadFinished = False
    ...
    My.Settings.SplashLoadFinished = True
End Sub

Then put a wait loop in the ApplicationEvents Startup event :

Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
...
If (Me.SplashScreen IsNot Nothing) Then
    'Wait until the Splash has actually been created
    While (Not My.Settings.SplashLoadFinished)
        System.Threading.Thread.Sleep(50)
    End While
End If
...
AndrewD
  • 1,083
  • 9
  • 15