0

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

Superjet100
  • 129
  • 1
  • 11

2 Answers2

9

You can try replacing delegate{...} with delegate(){...} . That way compiler will know that you want an overload for action without parameters.

alex
  • 12,464
  • 3
  • 46
  • 67
  • You are not passing any parameters to the delegate, so use BeginInvoke with single parameter - delegate, and remove the `,new object[0]` – alex Apr 24 '13 at 08:34
  • @alex, we don't have enough information to know that this will work. Different framework versions have different behavior; .NET 3.5 does not have a BeginInvoke method that accepts a single delegate as a parameter. Have a look at http://msdn.microsoft.com/en-us/library/ms604730(v=vs.90).aspx - Perhaps superjet100 can provide us with his framework verison? – ed_me Apr 24 '13 at 08:56
  • @chrisw69 Yes, it is .NET 3.5 – Superjet100 Apr 24 '13 at 09:19
2

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

Community
  • 1
  • 1
ed_me
  • 3,338
  • 2
  • 24
  • 34