2015-04-26 46 views
0

我得到我周圍的C#緩慢而穩步地方式笑在此代碼:訪問後臺工作

// create an instance of the main form 
    public formMain _formMain; 
    public void btnDynaDotCheck_Click(object sender, EventArgs e) 
    { 
     if (_formMain.bgWorker.IsBusy != true) 
     { 
      this.btnDynaDotCheck.Enabled = false; 
      _formMain.bgWorker.RunWorkerAsync("dr_begin_dd_check"); 
     } 
     else 
     { 
      _formMain.returnMessage("Please wait untill the current task is finished..."); 
      return; 
     } 
    } 

我試圖從anotherForm訪問後臺工作在formMain.cs的.cs存在VS沒有錯誤,但運行時我得到

和「附加信息「類型的‘System.NullReferenceException’ 發生未處理的異常」:未將對象引用設置爲 的一個實例目的。」

在此行中:

if (_formMain.bgWorker.IsBusy != true) 

,所以我沒有真正得到訪問在這種情況下,是嗎?

+0

我想'_formMain == null'因爲你沒有實例化。 – stefankmitph

+0

我同意stefankmitph,你的代碼已經聲明_forMain,但沒有實例化你的代碼嘗試操作的那個對象的實例。 – JohnH

+0

mainform被實例化,但_formMain不是指那個實例。沒有必要實例化一個已經有一個mainform的新實例。將該實例傳遞給anotherForm的構造函數。 –

回答

0

使用依賴注入注入到你的MainForm參考到otherone

anotherForm _anotherForm = new anotherForm(this); 
_anotherForm.Show(); 

假設你的MainForm的內創建從代碼anotherform,this實際上是指主表。 在anotherFrom的構造函數做到這一點:

public anotherForm(MainForm formMain){ 
    _formMain = formMain; 
} 

這是迄今爲止最優雅的方式來解決這個問題。因爲它清楚地表明瞭從一種形式到另一種形式的依賴性,並使設計意圖清晰。

使用家長也很好,但前提是mainform實際上是其他表單的父級。

去通過應用程序對象將工作,但應用程序對象是一個全局的,你隱藏你的依賴關係的方式。

0
_formMain = Application.OpenForms["formMain"]; 

添加此代碼按鈕單擊並嘗試。

0

當從anotherForm訪問_formMain

我認爲anotherForm被實例化,並呼籲從_formMain這樣的:

anotherForm _anotherForm = new anotherForm(); 
_anotherForm.Show(); 

現在有幾種方法可以從_anotherForm訪問_formMain但我認爲最簡單的是設置作爲_anotherForm的父母的_formMain

_anotherForm.Parent = this; // insert before _anotherForm.Show() 

這樣你可以得到它握在_anotherForm這樣

public void btnDynaDotCheck_Click(object sender, EventArgs e) 
{ 
    ... 
    formMain _formMain = this.Parent as formMain; 
    if(_formMain != null) 
    { 
     ... // do whatever ever you have to do 
    } 
} 

但要小心......讓你在BackgroundWorker需要_formMain公共方法可以調用和返回你的BackgroundWorker

希望這會有所幫助!在MainForm的代碼的地方執行以下操作:

0

感謝您的幫助球員:)

我現在有:

// create an instance of the formMain 
    formMain _formMain = new formMain(); 
    public void btnDynaDotCheck_Click(object sender, EventArgs e) 
    { 
     if (_formMain.bgWorker.IsBusy != true) 
     { 
      this.btnDynaDotCheck.Enabled = false; 
      _formMain.bgWorker.RunWorkerAsync("dr_begin_dd_check"); 
     } 
     else 
     { 
      _formMain.returnMessage("Please wait untill the current task is finished..."); 
      return; 
     } 
    } 

其中一期工程:)它獲得通過的主要形式有:

public void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     // action to string format 
     string action = e.Argument as string; 
     if (action == "dr_begin_dd_check") 
     { 
        BeginInvoke((Action)(() => 
        { 
         statusLabel.Text = "Access the bgw..."; 
        } 
        )); 

     } // dr_begin_dd_check 

現在我越來越表單中的錯誤主題:

Invoke或BeginInvoke無法在co上調用直到窗口句柄被創建。

我不知道錯誤在哪裏,我的上面的代碼或實際上在formMain部分,或者我應該打開一個新的問題? :)

歡呼傢伙

格雷厄姆

+0

不要創建第二個主窗體。按照我的回答所述,將現有表單傳遞給另一個表單的構造函數。 –

+0

非常感謝你們的工作完美:) – graham23s

+0

Upvote helpfull答案並請標記答案!這是感謝的正確方法;-) –