2016-04-23 104 views
0

當我使用ShowDialog()顯示錶單時,它阻止用戶界面和代碼,但我只需要阻止用戶界面不代碼。ShowDialog沒有阻止執行代碼,但阻止用戶界面

letturalog can3 = new letturalog(); 
        (new System.Threading.Thread(() => { 
         can3.ShowDialog(); 
        })).Start(); 

此模式不會阻止代碼和用戶界面。

所以我,想知道如果你能做到這一點

+0

如果您不希望阻止該代碼是在另一個線程,你已經做它。顯示對話框只會阻止它被調用的線程。 – Ian

+0

是的,但我寫了34.000行代碼..它不可能改變所有的showdialog ... – user3477026

+0

ShowDialog()通過禁用應用程序中的所有窗口,然後在循環中調用DoEvents(),直到DialogResult屬性被分配,然後重新啓用所有窗口。所以你得到相同的結果,減去「阻止代碼」,使用Show()並禁用所有窗口。 –

回答

3

如果你不想阻止代碼,那麼你要調用.Show

換句話說,你想:

can3.Show(this); 
this.Enabled = false; //disable the form so the UI is blocked 

//...do our stuff now that code is not blocked while the UI is blocked 

//All done processing; unblock the UI: 
this.Enabled = true; 

其實這就是ShowDialog所做的一切:禁用表單,然後重新啓用它。在僞代碼:

void ShowDialog(IWindowHandle Owner) 
{ 
    this.Show(Owner); 

    try 
    { 
     //Disable the owner form 
     EnableWindow(Owner, false); 

     repeat 
     { 
     Application.DoEvents(); 
     } 
     until (this.DialogResult != DialogResult.None); 
    } 
    finally 
    { 
     //Re-enable the UI! 
     EnableWindow(owner, true); 
    } 
} 

您可以竊取所有這些概念,並替換爲任何你想要的膽量:

void DoStuffWithTheThing() 
{ 
    can3.Show(); 

    try 
    { 
     //Disable the owner form 
     this.Enabled = false; 

     //todo: Solve the P=NP conjecture 
    } 
    finally 
    { 
     //Re-enable the UI! 
     this.Enabled = true; 
    } 
}