C# ThreadStart和ParameterizedThreadStart之间的调用不明确
我试图编译以下代码:C# ThreadStart和ParameterizedThreadStart之间的调用不明确,c#,wpf,multithreading,C#,Wpf,Multithreading,我试图编译以下代码: public class SplashScreenManager { private static readonly object mutex = new object(); public static ISplashScreen CreateSplashScreen(Stream imageStream, Size imageSize) { object obj; Monitor.Enter(obj = Splash
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;
}
}
并得到了错误信息:
以下方法或属性之间的调用不明确:
“System.Threading.Thread.ThreadSystem.Threading.ThreadStart”和
'System.Threading.Thread.ThreadSystem.Threading.ParameterizedThreadStart'
编辑1:
我更正了代码并得到错误2:
无法将匿名方法转换为类型“System.Windows.Threading.DispatcherPriority”,因为它不是委托类型您可以尝试将委托{…}替换为委托{…}。这样,编译器就会知道您需要一个没有参数的操作重载。对于BeginInvoke有许多不同的方法调用,这取决于您使用的框架。有关更多信息,请查看和或使用的.NET framework的任何版本 尝试一下.NET3.5和4的兼容性;这将解决第一个和第二个问题;您遇到的第二个错误的线索在错误消息中;您正在使用的方法需要一个不带对象参数的DispatcherPriority,并向它传递一个委托,该委托实际上是第二个参数所必需的
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();
}));
请参见为什么MethodInvoker是一个更有效的选择如果您没有将任何参数传递给委托,请使用BeginInvoke with single parameter-delegate,并删除新对象[0]@alex,我们没有足够的信息知道这是否可行。不同的框架版本具有不同的行为。NET 3.5没有接受单个委托作为参数的BeginInvoke方法。看一看-也许superjet100可以为我们提供他的框架verison?@chrisw69是的,它是.NET 3.5看到我为修复.NET 3.5和.NET 4的代码提供的答案了吗