Reputation: 457
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?
Upvotes: 1
Views: 585
Reputation: 1153
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
...
Upvotes: 0
Reputation: 45083
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.
Upvotes: 1