public delegate void testDelegate();
public testDelegate d1;
//public testDelegate d1=()=>{};//给个方法后却不会卡了
private void button1_Click(object sender, EventArgs e)
{
this.Invoke(d1, new object[] { });
}
,不知道为啥会卡住,感觉委托为null应该报个错,而不是一直卡在这,想知道原因
后来有位大佬(Aisa_Thanatos,需要可以关注他一下)说:Control.Invoke执行的委托不能为null的,内部判断会死循环的 这是Invoke的方法:
public object Invoke(Delegate method, params object[] args)
{
using (new Control.MultithreadSafeCallScope())
return this.FindMarshalingControl().MarshaledInvoke(this, method, args, true);
}
private object MarshaledInvoke(Control caller, Delegate method, object[] args, bool synchronous)
{
if (!this.IsHandleCreated)
throw new InvalidOperationException(SR.GetString("ErrorNoMarshalingThread"));
if ((Control.ActiveXImpl) this.Properties.GetObject(Control.PropActiveXImpl) != null)
IntSecurity.UnmanagedCode.Demand();
bool flag = false;
int lpdwProcessId;
if (SafeNativeMethods.GetWindowThreadProcessId(new HandleRef((object) this, this.Handle), out lpdwProcessId) == SafeNativeMethods.GetCurrentThreadId() && synchronous)
flag = true;
ExecutionContext executionContext = (ExecutionContext) null;
if (!flag)
executionContext = ExecutionContext.Capture();
Control.ThreadMethodEntry threadMethodEntry = new Control.ThreadMethodEntry(caller, this, method, args, synchronous, executionContext);
lock (this)
{
if (this.threadCallbackList == null)
this.threadCallbackList = new Queue();
}
lock (this.threadCallbackList)
{
if (Control.threadCallbackMessage == 0)
Control.threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_ThreadCallbackMessage");
this.threadCallbackList.Enqueue((object) threadMethodEntry);
}
if (flag)
this.InvokeMarshaledCallbacks();
else
UnsafeNativeMethods.PostMessage(new HandleRef((object) this, this.Handle), Control.threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
if (!synchronous)
return (object) threadMethodEntry;
if (!threadMethodEntry.IsCompleted)
this.WaitForWaitHandle(threadMethodEntry.AsyncWaitHandle);
if (threadMethodEntry.exception != null)
throw threadMethodEntry.exception;
return threadMethodEntry.retVal;
}
private void InvokeMarshaledCallbacks()
{
Control.ThreadMethodEntry tme = (Control.ThreadMethodEntry) null;
lock (this.threadCallbackList)
{
if (this.threadCallbackList.Count > 0)
tme = (Control.ThreadMethodEntry) this.threadCallbackList.Dequeue();
}
while (tme != null)
{
if (tme.method != null)
{
try
{
if (NativeWindow.WndProcShouldBeDebuggable)
{
if (!tme.synchronous)
{
this.InvokeMarshaledCallback(tme);
goto label_13;
}
}
try
{
this.InvokeMarshaledCallback(tme);
}
catch (Exception ex)
{
tme.exception = ex.GetBaseException();
}
}
finally
{
tme.Complete();
if (!NativeWindow.WndProcShouldBeDebuggable && tme.exception != null && !tme.synchronous)
Application.OnThreadException(tme.exception);
}
}
label_13:
lock (this.threadCallbackList)
tme = this.threadCallbackList.Count <= 0 ? (Control.ThreadMethodEntry) null : (Control.ThreadMethodEntry) this.threadCallbackList.Dequeue();
}
}