2016-08-05 13 views
0

在我的C#Widows窗體上,即使在退出後我也想使用Process對象信息,但我得到異常「系統進程已退出,因此請求信息不可用「保存處理對象信息以便在其退出後使用

我到目前爲止所嘗試的是將它保存爲一個var,並用我的ListView項標記它,但它仍會拋出相同的異常。

//ListView Parameters (omitted redundant ones) 
Process currentProcess = Process.GetProcessById(Convert.ToInt32(processStringID); 
newListViewItem.Tag = currentProcess; 
listView.Items.Add(newListViewItem); 

我有選擇的指數的情況下更改,因此當用戶點擊的ListView項目,它應該顯示有關已標記,即使它已經退出該項目的過程信息。

private void listView_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      try 
      { 

      Process processItem = (Process)listView.SelectedItems[0].Tag; 

      //Sample of getting process information (Error happens here) 
      MessageBox.Show(processItem.Name + processItem.VersionInfo); 

      } 

      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

Tldr;我需要一種方法來保存整個Process對象,以便即使流程已經退出,我也可以獲取它的信息。對於如何實現這一點我很樂意。請協助我,因爲我目前對編程的理解無法解決任何問題。

回答

0

儲存於一個Session

要存儲數據:

Session["process"] = processItem; 

從會話中提取數據:

var process = (Process)Session["process"]; // Don't forget to cast back to it's original type 

數據是可用的,即使你導航到其他頁面,除非你手動刪除它。

更新:

由於問題並不清楚在第一。

創建使用靜態類

public static class GlobalVar 
{ 
    /// <summary> 
    /// Global variable that is constant. 
    /// </summary> 
    public const string GlobalString = "Important Text"; 

    /// <summary> 
    /// Static value protected by access routine. 
    /// </summary> 
    static int _globalValue; 

    /// <summary> 
    /// Access routine for global variable. 
    /// </summary> 
    public static int GlobalValue 
    { 
    get 
    { 
     return _globalValue; 
    } 
    set 
    { 
     _globalValue = value; 
    } 
    } 

    /// <summary> 
    /// Global static field. 
    /// </summary> 
    public static bool GlobalBoolean; 
} 

一個全局變量看到這個職位:http://www.dotnetperls.com/global-variable

+0

會議?爲了澄清,我的程序是一個C#Windows窗體程序,而不是一個Web應用程序。對不起,如果我以前沒有讓自己清楚。 – Wally

相關問題