WPF Dispatcher.Invoke ThreadAbortException [英] WPF Dispatcher.Invoke ThreadAbortException

查看:78
本文介绍了WPF Dispatcher.Invoke ThreadAbortException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序是使用Windows 7 32位计算机在VS2008上的WPF中设计的,并已部署在Windows XP上.经过测试,一切正常.

最近,需要对外观和业务逻辑进行一些更改.这次我使用VS2010并进行了必要的更改.经过测试并在Windows XP上部署,一切正常.但是,当部署在32位Windows 7上时,该应用程序将崩溃.

查看日志后,我将其范围缩小到了这段代码:

I had an application designed in WPF on VS2008 using Windows 7 32-bit machine and deployed on Windows XP. Tested and everything worked fine.

Recently, some cosmetic and business logic changes were required. This time I used VS2010 and made the required changes. Tested and deployed on Windows XP everything works fine. However when deployed on Windows 7 32-bit, the application crashes.

After looking at the logs, I narrowed it down to this probably piece of code :

protected virtual void SomeEventHandlerMethod()
        {
            try
            {
                System.Windows.Application.Current.Dispatcher.Invoke(
                      DispatcherPriority.Normal,
                      new Action<Data>(FollowMethod), new dData(SubTypeData));
            }
            catch (Exception ex)
            {
                Logger.ErrorException(ex.Message, ex);
            }
        }



以下是引发的异常的堆栈跟踪:

System.Threading.ThreadAbortException:线程正在中止.

在System.Threading.WaitHandle.WaitOneNative(SafeWaitHandle waitHandle,UInt32毫秒超时,布尔值hasThreadAffinity,布尔值exitContext)

在System.Threading.WaitHandle.WaitOne处(Int64超时,布尔值exitContext)

在System.Threading.WaitHandle.WaitOne处(TimeSpan超时,布尔值exitContext)

在System.Windows.Threading.DispatcherOperation.DispatcherOperationEvent.WaitOne()

在System.Windows.Threading.DispatcherOperation.Wait(TimeSpan超时)

在System.Windows.Threading.Dispatcher.InvokeImpl中(DispatcherPriority优先级,TimeSpan超时,Delegate方法,对象参数,布尔值isSingleParameter)

在System.Windows.Threading.Dispatcher.Invoke处(DispatcherPriority优先级,Delegate方法,对象arg)

方法的完全限定名称SomeEventHandlerMethod()


我无法弄清楚在Windows 7上运行时应用程序崩溃的原因.我必须弄清楚这一点. HELP!



Following is the stack trace of the exception thrown :

System.Threading.ThreadAbortException: Thread was being aborted.

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(TimeSpan timeout, Boolean exitContext)

at System.Windows.Threading.DispatcherOperation.DispatcherOperationEvent.WaitOne()

at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout)

at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)

at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)

at Fully Qualified name of the method SomeEventHandlerMethod()


I am not able to figure out the reason of the application crash when running on Windows 7. I MUST figure this out. HELP!

推荐答案

您未在此处提供相关代码.所以,我给你个主意. ThreadAbortException异常是与错误情况完全不同.此异常应该捕获在使用Thread.Abort中止的任何线程的堆栈的最顶端,否则可能会中止.该异常用于执行线程的致命操作,进行适当的清理.

使用中止和此异常的技术是线程终止的技术之一.它旨在以零开销提供终止,而无需线程方面的任何协作成本.在某些终止条件不确定的应用程序中,至关重要的一点是,一个线程需要被另一个线程终止.这种机制非常可靠,但前提是您非常了解线程发生了什么,非常了解机制并认真编写线程代码 .

—SA
You did not provide relevant code here. So, I''ll just give you an idea. The exception ThreadAbortException is nothing like an error condition. This exception should be caught on the very top of stack of any thread which is aborted using Thread.Abort or it there is a possibility of abort. The exception is used to perform the post-mortal action of a thread, proper clean-up.

The technique using abort and this exception is one of the techniques of thread termination. It is designed to provide termination with zero overhead, without any cost of cooperation from the thread side. It is critically important in some applications where the condition of termination is uncertain, to a thread needs to be terminated by another thread. This mechanism is very reliable, but only if you know very well what happens to your thread, understand the mechanisms very well and write the thread code with great care.

—SA


这篇关于WPF Dispatcher.Invoke ThreadAbortException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆