C# control.invoke执行的委托是null的话,会卡死在这里

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();

      }

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值