VB Equivalent of the following code for threads
What is the vb.net equivalent of the following c# code?
var thread = new Thr开发者_开发技巧ead(() =>
{
Dispatcher.CurrentDispatcher.BeginInvoke ((Action)(() => new MySplashForm().Show()));
Dispatcher.Run();
});
Depends on the version of VB.Net.
Version 10.0
Dim thread As New Thread(
Sub()
Dispatcher.CurrentDispatcher.BeginInvoke (
Sub()
Dim form = new MySplashForm()
form.Show()
End Sub)
Dispatcher.Run()
End Sub)
Version 9.0
Sub ShowForm()
Dim form = new MySplashForm()
form.Show()
End Sub
Sub CreateForm()
Dispatcher.CurrentDispatcher.BeginInvoke(AddressOf ShowForm)
Dispatcher.Run()
End Sub
Dim thread as New Thread(AddressOf CreateForm)
I'm not 100% sure what you're trying to achieve though. You are essentially creating another thread to asynchronously show a form but then immediately forcing the asynchronous operation. It seems like it would be a lot easier to just asynchronously show via BeginInvoke and abandon the idea of creating another thread.
Note that the C# code (and the VB.NET equivalents) will leak a thread. It starts the dispatcher by calling Run, but it never shuts it down.
Obviously, that assumes that you do actually start the thread at some point, which neither example does. You'd need the following code for the rest to be of any use:
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
In whatever code it is that tries to close your splash screen, you'll need something that gets hold of the dispatcher and shuts it down. E.g., your MySplashForm could have this:
Public Sub ShutdownSplashThread()
Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal)
End Sub
That'll enable the thread that hosts the splash form to shut down properly. (And that'll have the side effect of closing the form too, so you don't need to do that.)
精彩评论