简体   繁体   English

线程在委托中被中止

[英]Thread was being aborted on a delegate

I am getting this type of error on the below function:I have also handled it with ThreadException but still getting such an error: 我在以下函数上遇到这种类型的错误:我也用ThreadException处理过它,但仍然收到这样的错误:

private void tmrOneSec_Tick(object sender, EventArgs e)
{
        tsSpendTime = tsSpendTime.Add(new TimeSpan(0, 0, 1));
        tsRemTime = tsTotalTime.Subtract(tsSpendTime);
        if (tsRemTime.Ticks > 0)
            clsCommonFunc.MultiThreadSetText(txtTimeRem, clsCommonFunc.GetFormattedTime(tsRemTime));
}


public static void MultiThreadSetText(TextBox TxtBox, string Text)
{

        if (TxtBox.InvokeRequired)
        {
            TxtBox.Invoke((MethodInvoker)delegate
            {
                MultiThreadSetText(TxtBox, Text);
            });
        }
        else
        {
            TxtBox.Text = Text;
            TxtBox.Refresh();
        }
}   

And the error is this: 错误是这样的:

Source :: mscorlib
Error :: 6/5/2012 8:51:28 AM
Error Description : Thread was being aborted.


Stack Trace:    at System.Threading.WaitHandle.WaitOneNative(SafeWaitHandle waitHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext)
at System.Threading.WaitHandle.WaitOne(Int32 millisecondsTimeout, Boolean exitContext)
at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at SE5.clsCommonFunc.MultiThreadSetText(TextBox TxtBox, String Text)

I am not able to recognize the exact problem. 我无法识别出确切的问题。

You get the exception waiting for the return from the Invoke call. 您会收到异常,等待Invoke调用返回。

Try changing 尝试改变

TxtBox.Invoke((MethodInvoker)delegate
{
    MultiThreadSetText(TxtBox, Text);
});

to

TxtBox.BeginInvoke((MethodInvoker)delegate
{
    MultiThreadSetText(TxtBox, Text);
});

Try changing your delegate usage like so: 尝试像这样更改您的代理使用:

private delegate void MultiThreadSetTextDelegate(TextBox TxtBox, string Text);

public static void MultiThreadSetText(TextBox TxtBox, string Text)
{

        if (TxtBox.InvokeRequired)
        {
            TxtBox.Invoke(new MultiThreadSetTextDelegate(MultiThreadSetText), TxtBox, Text);
        }
        else
        {
            TxtBox.Text = Text;
            TxtBox.Refresh();
        }
} 

Try that, if you still have the error let me know. 如果仍然有错误,请尝试让我知道。 But this is how I invoke my delegates and I have no troubles at all! 但这就是我调用我的代表的方式,我一点也不麻烦! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM