Reputation: 129
I tried to compile the following code:
public class SplashScreenManager
{
private static readonly object mutex = new object();
public static ISplashScreen CreateSplashScreen(Stream imageStream, Size imageSize)
{
object obj;
Monitor.Enter(obj = SplashScreenManager.mutex);
ISplashScreen vm2;
try
{
SplashScreenWindowViewModel vm = new SplashScreenWindowViewModel();
AutoResetEvent ev = new AutoResetEvent(false);
Thread thread = new Thread(delegate
{
vm.Dispatcher = Dispatcher.CurrentDispatcher;
ev.Set();
Dispatcher.CurrentDispatcher.BeginInvoke(delegate //<- Error 2 here
{
SplashForm splashForm = new SplashForm(imageStream, imageSize)
{
DataContext = vm
};
splashForm.Show();
}, new object[0]);
Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
ev.WaitOne();
vm2 = vm;
}
finally
{
Monitor.Exit(obj);
}
return vm2;
}
}
And got the error:
The call is ambiguous between the following methods or properties: 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' and 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)'
Edit1: I corrected code and get error 2:
Cannot convert anonymous method to type 'System.Windows.Threading.DispatcherPriority' because it is not a delegate type
Upvotes: 0
Views: 1997
Reputation: 3508
There are quite a few different method calls for BeginInvoke that differ depending upon which framework you are using. Take a look at http://msdn.microsoft.com/en-us/library/ms604730(v=vs.100).aspx and http://msdn.microsoft.com/en-us/library/ms604730(v=vs.90).aspx, or whatever version of the .NET framework you are using for more information.
Try this for .NET 3.5 and 4 compatibility; this should fix both your first and second problem; the clue to the second error you are experiencing is in the error message; the method that you are using is expecting a DispatcherPriority
with no object parameters and you're passing it a Delegate, which is actually required as the second argument.
Thread thread = new Thread(new ThreadStart(() =>
{
vm.Dispatcher = Dispatcher.CurrentDispatcher;
ev.Set();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, new MethodInvoker(() =>
{
SplashForm splashForm = new SplashForm(imageStream, imageSize)
{
DataContext = vm
};
splashForm.Show();
}));
Dispatcher.Run();
}));
See MethodInvoker vs Action for Control.BeginInvoke for why MethodInvoker is a more efficient choice
Upvotes: 2
Reputation: 12654
You can try replacing delegate{...}
with delegate(){...}
. That way compiler will know that you want an overload for action without parameters.
Upvotes: 9