首页 > 解决方案 > 关闭表单后释放使用的内存

问题描述

我有包含 DevExpress 选项卡式视图的功能区表单。 在此处输入图像描述 当我在任务管理器中启动程序时,我得到118.6 MB的已用内存
现在当我在选项卡式视图上 打开新表单“FrmWeldingProduction”时,当我打开和关闭表单“FrmWeldingProduction” 时,我得到这些结果(已用内存) 首先打开 274.4 MB 第一次关闭 272.1 MB 第二次打开 403.9 MB 第二次关闭 401.6 MB 第三次打开 564.9 MB 第三次关闭 562.2 MB 这是表格后面的代码在此处输入图像描述




public partial class FrmWeldingProduction : XtraForm, IDisposable
{
    readonly CLS_Welding welding = new();
    readonly CLS_User us = new();
    private RepOptionsFirstLastTime firstLastTime = new();
    private readonly CultureInfo cultureInfo = CultureInfo.CurrentCulture;
    private readonly string WeldingProduction = Application.StartupPath + "\\WeldingProduction.xml";
    private readonly string LayoutWeldingProduction = "LayoutWeldingProduction";
    private readonly int IDScreen = 19;
    public FrmWeldingProduction() { InitializeComponent(); }

    private async Task Permission()
    {
        using(DataTable Dt = await us.ApplayPermission(Program.UserID, IDScreen).ConfigureAwait(true))
        {
            if(Dt.Rows[0][1].ToString() == "False" || string.IsNullOrEmpty(Dt.Rows[0][1].ToString()))
            {
                repItemCreationDate.ReadOnly = true;
            }
            if(Dt.Rows[0][2].ToString() == "False" || string.IsNullOrEmpty(Dt.Rows[0][2].ToString()))
            {
                btnDelete.Enabled = false;
                btnDeleteNoir.Enabled = false;
            }
        }
    }

    private void FrmWeldingProduction_FormClosed(object sender, FormClosedEventArgs e)
    {
        workspaceManager1.CaptureWorkspace(LayoutWeldingProduction, true);
        workspaceManager1.SaveWorkspace(LayoutWeldingProduction, WeldingProduction, true);
        DisposeFrm();
    }

    public void DisposeFrm()
    {
        us.Dispose();
        firstLastTime.Dispose();
        this.Load -= FrmWeldingProduction_Load;
        gridView1.FocusedRowChanged -= gridView1_FocusedRowChanged;
        repItemCreationDate.DrawItem -= RepItemCreationDate_DrawItem;
        btnRefresh.Click -= btnRefresh_Click;
        this.KeyDown -= FrmWeldingProduction_KeyDown;
        btnProductionProject.ItemClick -= btnProductionProject_ItemClick;
        gridView1.CellValueChanged -= GridView1_CellValueChanged;
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private async void gridView1_FocusedRowChanged(
        object sender,
        DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        if(e.FocusedRowHandle > 0)
        {
            using(DataTable EmployeeWeldingByProduction = await welding.GetEmployeeWeldingByProduction(
                Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FK_ShiftTime"), cultureInfo),
                Convert.ToDateTime(
                    gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Date de fabrication"),
                    cultureInfo)
                    .ToString("MM/dd/yyyy", cultureInfo),
                Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FK_idPartShip"), cultureInfo),
                Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OrderNumber"), cultureInfo))
                .ConfigureAwait(true))
            {
                gridControl2.DataSource = EmployeeWeldingByProduction;
            }
        }
    }

    private async void btnRefresh_Click(object sender, EventArgs e)
    {
        using(DataTable WeldingProduction = await welding.GetWeldingProduction().ConfigureAwait(true))
        {
            gridControl1.DataSource = WeldingProduction;
        }

        using(DataTable WeldingPaintProduction = await welding.GetWeldingPaintProduction().ConfigureAwait(true))
        {
            gridControl3.DataSource = WeldingPaintProduction;
        }
        using(DataTable WeldingPaintFabRest = await welding.GetWeldingPaintFabRest().ConfigureAwait(true))
        {
            gridControl4.DataSource = WeldingPaintFabRest;
        }
        using(DataTable WeldingPaintStatusCHProject = await welding.GetWeldingPaintStatusCHProject()
            .ConfigureAwait(true))
        {
            gridControl5.DataSource = WeldingPaintStatusCHProject;
        }
        using(DataTable ProductionCharpent = await welding.GetProductionCharpent().ConfigureAwait(true))
        {
            gridControl6.DataSource = ProductionCharpent;
        }
        using(DataTable StatusSupportsBS = await welding.GetStatusSupportsBS().ConfigureAwait(true))
        {
            gridControl7.DataSource = StatusSupportsBS;
        }


        if(gridView1.FocusedRowHandle > 0)
        {
            using(DataTable EmployeeWeldingByProduction = await welding.GetEmployeeWeldingByProduction(
                Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FK_ShiftTime"), cultureInfo),
                Convert.ToDateTime(
                    gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Date de fabrication"),
                    cultureInfo)
                    .ToString("MM/dd/yyyy", cultureInfo),
                Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FK_idPartShip"), cultureInfo),
                Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OrderNumber"), cultureInfo))
                .ConfigureAwait(true))
            {
                gridControl2.DataSource = EmployeeWeldingByProduction;
            }
        }
    }

    private async void FrmWeldingProduction_Load(object sender, EventArgs e)
    {
        KeyPreview = true;
        btnRefresh_Click(sender, e);

        foreach(GridColumn columnDateEdit in gridView1.Columns)
            columnDateEdit.OptionsColumn.AllowEdit = false;
        gridView1.Columns["Date de fabrication"].OptionsColumn.AllowEdit = true;

        //pivotGridControl1.DataSource = await welding.GetProductionCharpent().ConfigureAwait(true);


        workspaceManager1.TargetControl = this;
        workspaceManager1.SaveTargetControlSettings = true;
        if(workspaceManager1.LoadWorkspace(LayoutWeldingProduction, WeldingProduction, true))
            workspaceManager1.ApplyWorkspace(LayoutWeldingProduction);

        await Permission().ConfigureAwait(true);
    }

    private bool IsHoliday(DateTime dt)
    {
        //the specified date is a Holiday
        if(dt.DayOfWeek == DayOfWeek.Friday)
            return true;
        //New Year's Day
        if(dt.Day == 1 && dt.Month == 1)
            return true;
        //Independence Day
        if(dt.Day == 5 && dt.Month == 7)
            return true;
        //VAlgerian War of Independence
        if(dt.Day == 1 && dt.Month == 11)
            return true;
        //Employees Day
        if(dt.Day == 1 && dt.Month == 5)
            return true;
        return false;
    }

    private void RepItemCreationDate_DrawItem(
        object sender,
        DevExpress.XtraEditors.Calendar.CustomDrawDayNumberCellEventArgs e)
    {
        if(e.View != DevExpress.XtraEditors.Controls.DateEditCalendarViewType.MonthInfo)
            return;
        //return if a given date is not a holiday
        //in this case the default drawing will be performed (e.Handled is false)
        if(!IsHoliday(e.Date))
            return;
        //highlight the selected and hot-tracked dates
        bool isHotTracked = e.State == DevExpress.Utils.Drawing.ObjectState.Hot;
        if(e.Selected || isHotTracked)
        {
            e.Graphics.FillRectangle(e.Style.GetBackBrush(e.Cache), e.Bounds);
        }
        //the brush for painting days
        Brush brush = (e.Inactive ? Brushes.LightPink : Brushes.Red);
        //specify formatting attributes for drawing text
        using(StringFormat strFormat = new StringFormat
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center
        })
        {
            //draw the day number
            e.Graphics.DrawString(e.Date.Day.ToString(), e.Style.Font, brush, e.Bounds, strFormat);
        }
        //no default drawing is required
        e.Handled = true;
    }

    private void FrmWeldingProduction_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            if(e.KeyCode == Keys.F5)
            {
                btnRefresh_Click(sender, e);
            }
        } catch
        {
        }
    }
    private void btnProductionProject_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        if(firstLastTime == null || firstLastTime.IsDisposed)
            firstLastTime = new RepOptionsFirstLastTime();
        firstLastTime.Show();
        firstLastTime.OK.Click += ProductionProject_Click;
    }

    private async void ProductionProject_Click(object sender, EventArgs e)
    {
        using(RepWeldingProductionSumm report = new())
        {
            // Create a new parameter. 
            Parameter param1 = new();
            Parameter param2 = new();
            Parameter param3 = new();
            // Specify required properties. 
            param1.Name = "FirstDatepara1";
            param1.Type = typeof(DateTime);
            param1.Visible = false;
            param1.Value = Convert.ToDateTime(firstLastTime.FirstDate.EditValue, cultureInfo)
                .ToString("dd/MM/yyyy", cultureInfo);

            param2.Name = "lastDatepara2";
            param2.Type = typeof(DateTime);
            param2.Visible = false;
            param2.Value = Convert.ToDateTime(firstLastTime.LastDate.EditValue, cultureInfo)
                .ToString("dd/MM/yyyy", cultureInfo);


            param3.Name = "shifttime";
            param3.Type = typeof(string);
            param3.Visible = false;
            param3.Value = firstLastTime.cmbShiftTime.Text;


            report.Parameters.Add(param1);
            report.Parameters.Add(param2);
            report.Parameters.Add(param3);
            if(firstLastTime.cmbShiftTime.Text == Resources.allDay ||
                string.IsNullOrEmpty(firstLastTime.cmbShiftTime.Text))
            {
                report.DataSource = await welding.RepWeldingProductionSumm(
                    Convert.ToDateTime(firstLastTime.FirstDate.EditValue, cultureInfo)
                        .ToString("MM/dd/yyyy", cultureInfo),
                    Convert.ToDateTime(firstLastTime.LastDate.EditValue, cultureInfo)
                        .ToString("MM/dd/yyyy", cultureInfo))
                    .ConfigureAwait(true);
                firstLastTime.Close();

                report.ShowRibbonPreviewDialog();
            } else
            {
                report.DataSource = await welding.RepWeldingProductionShiftTimeSumm(
                    Convert.ToDateTime(firstLastTime.FirstDate.EditValue, cultureInfo)
                        .ToString("MM/dd/yyyy", cultureInfo),
                    Convert.ToDateTime(firstLastTime.LastDate.EditValue, cultureInfo)
                        .ToString("MM/dd/yyyy", cultureInfo),
                    Convert.ToInt32(firstLastTime.cmbShiftTime.EditValue, cultureInfo))
                    .ConfigureAwait(true);
                firstLastTime.Close();
                report.ShowRibbonPreviewDialog();
            }
        }
    }

    private async void GridView1_CellValueChanged(
        object sender,
        DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
        await Permission().ConfigureAwait(true);
        if(e.Column.FieldName == "Date de fabrication")

        {
            await welding.UpdtaeCreationDateWeldingProduction(
                Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, "id"), cultureInfo),
                Convert.ToDateTime(e.Value))
                .ConfigureAwait(true);
            XtraMessageBox.Show(
                Resources.operationAccomplishedSuccessfully,
                Resources.Confirmation,
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }
    }
}

标签: c#winformsdispose

解决方案


推荐阅读