2015-10-19 35 views
2

我怎樣才能得到當發生例外情況時中斷用戶未處理要像廣告中那樣可靠地工作https://archive.is/090KL如何獲得'突然發生用戶未處理的異常'以便像廣告一樣工作?

這裏是它的工作,另外它的失敗的例子:

public partial class MainForm : Form 
{ 
    public MainForm() 
    { 
     InitializeComponent(); 

      LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,100,100),Color.Blue, Color.White,angle:0); 
      brush.WrapMode = WrapMode.Clamp; // Causes Unhandled exception alert, offering break 
    } 

    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     { 
      LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 100, 100), Color.Blue, Color.White, angle: 0); 
      brush.WrapMode = WrapMode.Clamp; // Fails to break 
     } 
    } 
} 

有這個計劃沒有用戶異常處理程序。這個例子是平臺目標x86並在Windows 7下運行。

+1

Crystal ball表示您正在使用Windows 7.查看[本帖](http://stackoverflow.com/a/4934010/17034)中的解決方法。 –

+0

我正在使用Windows 7信息添加。感謝您的解決方法。唯一適用的http://i.imgur.com/2gKYkjP.png的作品是http://i.imgur.com/JAWKLQq.png ...,但當然,這也是以捕獲用戶處理的異常爲代價的。 – ChrisJJ

+0

嗯,很難看出在引發異常時讓調試器停止的問題。這幾乎不是「唯一適用的」,消除抖動強制並將您的機器升級到Win10是非常不錯的選擇。 –

回答

1

如果我修改Main()靜態線程,它會導致引發異常,在Windows 7上使用Visual Studio 2012和您的代碼示例。

/// <summary> 
/// The main entry point for the application. 
/// </summary> 
[STAThread] 
static void Main() 
{ 
    //Added this line and corresponding method 
    Application.ThreadException += Application_ThreadException; 

    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Form1()); 
} 

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{ 
} 
相關問題