Windows窗體應用程序常用的幾個類的屬性、方法以及事件


System.Diagnostics.Process#

屬性##

public bool EnableRaisingEvents { get; set; }//獲取或設置在進程終止時是否應激發 Exited 事件。

事件##

public event EventHandler Exited //在進程退出時發生。進程退出的時候,一般需要把Process對象釋放掉[比如一個Windows應用程序里面另外打開其他的exe,其他exe關閉后的處理]

System.AppDomain#

屬性##

public static AppDomain CurrentDomain { get; } //獲取當前 Thread 的當前應用程序域。

事件##

public event UnhandledExceptionEventHandler UnhandledException //當某個異常未被捕獲時出現。

System.Windows.Forms.Application#

詳細的信息還是去msdn上查看,特別是要看備注

屬性##

public static string CommonAppDataPath { get; } 可以把程序的日志寫在此路徑下,即便是用戶切換了Windows用戶,還是可以看到程序的以前的日志
//獲取所有用戶共享的應用程序數據的路徑。

public static string StartupPath { get; } //獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。

方法##

public static void Exit() //通知所有消息泵必須終止,並且在處理了消息以后關閉所有應用程序窗口。
public static void EnableVisualStyles() //啟用應用程序的可視樣式。 新建一個windows窗體應用程序的時候會調用

public static void SetCompatibleTextRenderingDefault(bool defaultValue) 新建windows窗體應用程序的時候會調用,傳遞的參數是false
//將某些控件上定義的 UseCompatibleTextRendering 屬性設置為應用程序范圍內的默認值。
defaultValue 用於新控件的默認值。
如果為 true,則支持 UseCompatibleTextRendering 的新控件使用基於 GDI+ 的 Graphics 類進行文本呈現;
如果為 false,則新控件使用基於 GDI 的 TextRenderer 類。

public static void Run(Form mainForm) 新建windows窗體應用程序的時候會調用,傳遞的參數是新建的一個Form主窗體
//在當前線程上開始運行標准應用程序消息循環,並使指定窗體可見。

public static void DoEvents() //處理當前在消息隊列中的所有 Windows 消息。

public static void Restart() The most common reason for calling Restart is to start a new version of the application that you have downloaded through ClickOnce using the Update or UpdateAsync method.(還是英文准確,機器翻譯的中文簡直是慘不忍睹)
//關閉應用程序並立即啟動一個新實例。 ClickOnce部署

事件##

public static event ThreadExceptionEventHandler ThreadException //在發生未捕獲線程異常時發生。 未捕獲的異常,通過這個事件處理來打印日志

總結:#

一般來說
System.AppDomain的CurrentDomain的UnhandledException事件是需要注冊的
System.Windows.Forms.Application的ThreadException事件也是需要注冊的
通過上面兩個事件來捕獲未知的異常

最后吐槽下,博客園的markdown編輯器,不支持兩個空格和enter鍵換行嗎?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM