2010-03-09 49 views
8

我的WPF測試應用程序(非常簡單,只有一個窗口)正在使用第三方受管dll(如X.dll)。這個託管的DLL使用一些非託管的DLL。 所以我們可以說我寫了一個小的wpf應用程序,它只是引用了X.dll。在窗口的構造函數中,我可以訪問X.dll中的某些東西(即在X.dll中的某些命名空間中)。在這樣做我不會捕捉任何異常,它似乎像事情正在按預期進行。但在返回控制到.NET運行時,我得到了應用程序類的「DispatcherUnhandledException」處理異常:「在算術運算溢出或下溢」「算術運算中溢出或下溢」WPF特定問題

System.ArithmeticException了未處理 消息 =「溢出或在算術運算中下溢。「
來源 = 「PresentationFramework」
堆棧跟蹤

System.Windows.Window.ValidateTopLeft(Double length)
System.Windows.Window.CoerceTop(DependencyObject d, Object value) System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean skipBaseValueChecks)
System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, OperationType operationType)
System.Windows.DependencyObject.CoerceValue(DependencyProperty dp) at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
System.Windows.Window.CreateSourceWindowImpl() at System.Windows.Window.SafeCreateWindow() at System.Windows.Window.ShowHelper(Object booleanBox)
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

幾點:

  • 這隻發生在WPF應用程序,而不是在WinForms應用程序。
  • 這不會被抓住試試看。只在應用程序的DispatcherUnhandledException
  • 這不會發生,如果我訪問窗口的'Loaded'事件中的X.dll的代碼,只發生在構造函數。

任何人都可以猜出問題嗎?

感謝, 米沙勒

+0

當你說「訪問」,你的意思是你傳遞的值作爲參數的東西,對不對?你通過了什麼? – Jay 2010-03-09 18:15:22

+0

如果我訪問/讀取整數XNameSpace.AStaticClass.StatInt(其中的XNamespace裏面X.dll命名空間),並將其存儲在另一個整數,我看到這一點。此外,我看到如果我把這個陳述放在try catch中,那麼它就不會被捕獲。它只會在System.Window.Application的DispatcherUnhandledException處理程序中被捕獲......這真的很奇怪。 – mishal153 2010-03-11 15:09:42

回答

5

我不知道的根本原因,但還沒有解決的辦法是在這裏:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a31f9c7a-0e15-4a09-a544-bec07f0f152c

似乎是一個流行的錯誤:)

感謝, 米沙勒

+0

僅僅爲了他人,鏈接中的建議解決方案更多地符合解決方法。這個問題本身似乎是WPF中的一個錯誤,儘管我不確定這個問題是否存在連接問題。 – jpierson 2014-05-01 01:52:35

+0

https://connect.microsoft.com/VisualStudio/feedback/details/382052/cryptic-arithmeticexception-in-system-double-equals-during-wpf-layout – jpierson 2014-05-01 01:58:55

10

繼@Mishhl的鏈接後,修復程序爲

public class FloatingPointReset 
{ 
    [DllImport("msvcr110.dll", CallingConvention = CallingConvention.Cdecl)] 
    public static extern int _fpreset(); 


    public static void Action() 
    { 
     // Reset the Floating Point (When called from External Application there was an Overflow exception) 
     _fpreset(); 
    } 
} 

它是由包含的DLL中的某些東西引起的,它將FP重置爲與WPF/C#/ Microsoft DLL不兼容的狀態。德爾福/ CPPB默認這樣做。

所以無論是在窗口構造函數或應用程序()構造函數只是做

FloatingPointReset.Action(); 

您可能需要更改以下引用msvcr的###任何版本。DLL

[DllImport("msvcr110.dll", CallingConvention = CallingConvention.Cdecl)] 

例如

[DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)] 
+0

我的建議是調用_fpreset不僅在構造函數,但與前在外部函數調用之後。從第三方COM對象庫調用函數時出現此問題。 – Denis 2015-12-11 04:37:25

+0

對我來說,我通過遵循我正在使用的外部庫來解決它,並且知道哪個msvcrXX。(它是msvcr100.dll),然後我調用** FloatingPointReset.Action(); **在調用** base.OnRender(drawingContext); **在我的UserControl之前。 – Dabbas 2017-04-11 17:13:00

0

如果它可以幫助,我有相同問題(這裏說明和 here)。我在這裏帶來這個可能的原因之一。

我使用了一個偉大的WPF用戶控件一個ElementHost的(C#,.net4.0,WinForm的,VS2012)。 一切正常大。在我的UserControl後面的代碼中,我使用了一個從另一個DLL中隱藏的類,用於存儲某些任務的進度。例如(這是貼近現實):

public class SI_MyProgressionHelper 
{ 
    public string gstr_OverallProgress_Description { get; set; } 

    public int gint_OverallProgress_ActualValue { get; set; } 
    public int gint_OverallProgress_MaximumValue { get; set; } 
} 

我當時就想「外掛」這個類在我的WPF進度/ textBox中,因爲它去彙報進展。然而(這是另一次對話),WPF需要將INotifyPropertyChanged應用於要插入WPF的每個屬性(以自動更新WPF中的控件)。這是我見過的最醜陋的事情之一,但它是它是什麼(我知道我必須錯過的東西..)

無論如何,遵守我的ProgressBar /文本框和我的SI_MyProgressionHelper類的WPF綁定mecanism,我已經修改了這個類(我重複,在另一個DLL中):

public class SI_MyProgressionHelper : INotifyPropertyChanged 
{ 
    private string _gstr_OverallProgress_Description = ""; 
    public string gstr_OverallProgress_Description { get { return _gstr_OverallProgress_Description; } set { _gstr_OverallProgress_Description = value; RaisePropertyChanged("gstr_OverallProgress_Description"); } } 

    private int _gint_OverallProgress_ActualValue = 0; 
    public int gint_OverallProgress_ActualValue { get { return _gint_OverallProgress_ActualValue; } set { _gint_OverallProgress_ActualValue = value; RaisePropertyChanged("gint_OverallProgress_ActualValue"); } } 

    private int _gint_OverallProgress_MaximumValue = 9999; 
    public int gint_OverallProgress_MaximumValue { get { return _gint_OverallProgress_MaximumValue; } set { _gint_OverallProgress_MaximumValue = value; RaisePropertyChanged("gint_OverallProgress_MaximumValue"); } } 


    protected virtual void RaisePropertyChanged(string propName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 

BAM!我UserContrl任何的XMLElement(高度,寬度等)的任何隨機整數屬性時發生錯誤(正是因爲這裏所說和其他參考我在上面說明)。

應用帶到這裏並糾正我的問題的解決方案: 在我的Program.cs的Main()方法:

[DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)] 
public static extern int _fpreset(); 

,然後在我的UserControl.xaml.cs,在構造函數中:

public UserControl1() 
{ 
    Program._fpreset(); 
    InitializeComponent(); 
} 

此外,這就是爲什麼我寫的評論的原因:通過對我的Helper類取出INotifyPropertyChanged的(和其他的東西有關),問題消失。 (但是我的WPF很傷心,因爲我的代碼對他來說太短了)。

希望這其它附加信息,可以幫助找人同樣的問題。

而且,我所有的法國同事,這裏是遇到了法國的錯誤:

伊爾爲y的歐盟未dépassement德capacitépositif歐négatif丹斯 L'操作arithmétique。

相關問題