簡體   English   中英

在C#Winforms中處置用戶控件的功能而不從BeginInvoke方法調用

[英]Dispose function of user control not calling from BeginInvoke Method in c# winforms

我有一個主窗體,並在此主窗體上添加一個用戶控件,如下所示。

objCustomer = new Customer();
objCustomer.Top = this.Top;
objCustomer.Left = this.Left;
this.BeginInvoke((MethodInvoker)delegate { this.Controls.Add(objCustomer); });

現在,在某些情況下,我必須卸載此控件並加載其他控件。

if (objCustomer != null)
{
this.Invoke((MethodInvoker)delegate { this.Controls.Remove(objCustomer); });
this.Invoke((MethodInvoker)delegate { objCustomer.Dispose(); });
}
objEmployee = new Employee();
objEmployee.Top = this.Top;
objEmployee.Left = this.Left;

this.BeginInvoke((MethodInvoker)delegate { this.Controls.Add(objEmployee); });

現在,在“客戶Dispose功能上,我有一些例程要求從其他系統注銷。

protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            Common.Log.LogEvent("Customer", "DisposedCall");
            LogOffServer();

            components.Dispose();

        }
        base.Dispose(disposing);
    }

我相信此Dispose事件沒有調用。

請幫我。

謝謝

假定最后一個Dispose()方法用於您的Form,如果沒有執行您的條件塊,這是因為Winform上的所有控件都在調用Form的Dispose()方法之前被處置了。 這意味着components != null為假(因為所有components都已被處置),並且條件的計算結果為假。

Winform事件生命周期

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM