微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关闭表单后释放已用内存

如何解决关闭表单后释放已用内存

我有包含 DevExpress 选项卡式视图的功能区表单。

enter image description here

当我在任务管理器中启动程序时,我获得了 118.6 MB 的已用内存
现在,当我在选项卡式视图中打开新表单 "FrmWeldingProduction"

enter image description here


当我打开和关闭表单 "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",Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"FK_idPartShip"),"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,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,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);


            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",Convert.ToDateTime(firstLastTime.LastDate.EditValue,cultureInfo))
                    .ConfigureAwait(true);
                firstLastTime.Close();

                report.ShowRibbonPreviewDialog();
            } else
            {
                report.DataSource = await welding.RepWeldingProductionShiftTimeSumm(
                    Convert.ToDateTime(firstLastTime.FirstDate.EditValue,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"),Convert.ToDateTime(e.Value))
                .ConfigureAwait(true);
            XtraMessageBox.Show(
                Resources.operationAccomplishedSuccessfully,Resources.Confirmation,MessageBoxButtons.OK,MessageBoxIcon.information);
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。