sharat naik Ответов: 0

Как решить ошибку создания ошибки дескриптора окна?


Система.ComponentModel.Win32Exception (0x80004005): ошибка создания дескриптора окна.
в системе.Окна.Формы.Родное окно.CreateHandle(CreateParams cp)
в системе.Окна.Формы.Контроль.Метода createhandle()
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl(Boolean fIgnoreVisible)
в системе.Окна.Формы.Контроль.CreateControl()
в системе.Окна.Формы.Контроль.OnVisibleChanged(EventArgs e)
в системе.Окна.Формы.ScrollableControl.OnVisibleChanged(EventArgs e)
в системе.Окна.Формы.Контроль.OnParentVisibleChanged(EventArgs e)
в системе.Окна.Формы.Контроль.OnVisibleChanged(EventArgs e)
в системе.Окна.Формы.ScrollableControl.OnVisibleChanged(EventArgs e)
в системе.Окна.Forms.Form.OnVisibleChanged(EventArgs e)
в системе.Окна.Формы.Контроль.SetVisibleCore(логическое значение)
в системе.Окна.Forms.Form.SetVisibleCore(логическое значение)
в системе.Окна.Формы.Контроль.Покажите()


<pre>19-03-2019 20:13:15 	System.OutOfMemoryException: Out of memory.
   at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
   at System.Drawing.Font.GetHeight()
   at System.Drawing.Font.get_Height()
   at System.Windows.Forms.DataGridViewRow..ctor()
   at System.Windows.Forms.DataGridViewRow.Clone()
   at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32 index)
   at System.Windows.Forms.DataGridView.OnClearingRows()
   at System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)
   at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()
   at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.Show()
   at TEXTILE_EXTNL_DISPLAY.MainForm.<Active_Timer_Tick>d__22.MoveNext()


Что я уже пробовал:

private void MainForm_Load(object sender, EventArgs e)
     {
         //Resolution.CResolution ChangeRes = new Resolution.CResolution(FixHeight, FixWidth);
         //  this.MinimumSize = new Size(this.Width, this.Height);
         //  this.MaximumSize = new Size(this.Width, this.Height);
     }

     public MainForm()
     {
         //Screen Srn = Screen.PrimaryScreen;
         //tempHeight = Srn.Bounds.Width;
         //tempWidth = Srn.Bounds.Height;
         InitializeComponent();
         Active_Timer.Interval = 100;
         Active_Timer.Tick += new EventHandler(Active_Timer_Tick);
         Active_Timer.Start();
         this.WindowState = FormWindowState.Maximized;
         this.Location = new Point(0, 0);
         this.Size = Screen.PrimaryScreen.WorkingArea.Size;
     }


         [DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
         CharSet.Ansi, SetLastError = true)]

         private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int
         maximumWorkingSetSize);

         public static void FlushMemory()
         {
             GC.Collect();
             GC.WaitForPendingFinalizers();
             if (Environment.OSVersion.Platform == PlatformID.Win32NT)
             {
                 SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
             }
         }

     private async void Active_Timer_Tick(object sender, EventArgs e)
     {
         try
         {
             bool IsOpen = false;

             if ((DateTime.Now.Minute == 3) || (DateTime.Now.Minute == 33) || (DateTime.Now.Minute == 4) || (DateTime.Now.Minute == 34))
             {
                 FlushMemory();
                 ELMEASURE frm1 = new ELMEASURE();
                 ELMEASURE1 frm2 = new ELMEASURE1();
                 #region ADD Screens
                 if ((DateTime.Now.Minute == 3) || (DateTime.Now.Minute == 33))
                 {
                     Active_Timer.Stop();
                     frm1.Location = new Point(0, 0);
                     frm1.Size = Screen.PrimaryScreen.WorkingArea.Size;
                     frm1.StartPosition = FormStartPosition.CenterScreen;
                     frm1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                     frm1.Dock = DockStyle.Fill;
                     frm1.Show();
                     await Task.Delay(60000);
                     frm1.Close();
                 }
                 if ((DateTime.Now.Minute == 4) || (DateTime.Now.Minute == 34))
                 {
                     frm2.Location = new Point(0, 0);
                     frm2.Size = Screen.PrimaryScreen.WorkingArea.Size;
                     frm2.StartPosition = FormStartPosition.CenterScreen;
                     frm2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                     frm2.Dock = DockStyle.Fill;
                     frm2.Show();
                     await Task.Delay(60000);
                     frm2.Close();
                 }
                 #endregion
                 MainForm mainFrm = new MainForm();
                 mainFrm.ShowDialog();
             }
             else
             {
                 //GC.Collect();
                 //GC.WaitForPendingFinalizers();
                 //GC.Collect();
                 //GC.WaitForPendingFinalizers();
                 Active_Timer.Stop();
                 this.Hide();
                 SqlConnection con = new SqlConnection(connection);
                 con.Open();
                 SqlCommand cmd = new SqlCommand("Select id,Form_Name,Type,DeviceID,DepartmentName,Status from Textile.dbo.Display_External_Selected where  Status='True'", con);
                 dt_Forms = new DataTable();
                 SqlDataAdapter adp = new SqlDataAdapter(cmd);
                 adp.Fill(dt_Forms);

                 arrDeviceID = new ArrayList();
                 int deviceCount = 0;
                 int ID;

                 foreach (DataRow row in dt_Forms.Rows)
                 {
                     Globals.StrShift = _AllData.CurrentShift();
                     Frm_Name = row["Form_Name"].ToString();
                     DeviceID = row["DeviceID"].ToString();
                     DepartmentName = row["DepartmentName"].ToString();
                     DeptType = row["Type"].ToString();

                     deviceCount = DeviceID.Split(',').Length;

                     Globals.StrDeviceCount = deviceCount;
                     Globals.StrDeviceID = DeviceID;
                     Globals.StrDeptName = DepartmentName;
                     Globals.StrDeptType = DeptType;
                     string[] strs = DeviceID.Split(',');
                     for (int i = 0; i < strs.Length; i++)
                     {
                         Globals.ArrDeviceID.Add(strs[i]);
                     }
                     ID = Convert.ToInt16(row["id"].ToString());
                     var str_array = Frm_Name.Split(',');
                     for (var i = 0; i < str_array.Length; i++)
                     {
                         Active_Timer.Stop();

                         conn = new SqlConnection(connection);
                         conn.Open();
                         cmdd = new SqlCommand("select Form_Active from  Textile.dbo.Display_Parameters where Form_No=" + str_array[i] + "", conn);
                         reader = cmdd.ExecuteReader();
                         if (reader.HasRows)
                         {
                             while (reader.Read())
                             {
                                 Active_Frm_Name = (string)reader["Form_Active"];
                                 IsOpen = true;
                                 string formTypeFullName = string.Format("{0}.{1}", this.GetType().Namespace, Active_Frm_Name);
                                 Type type = Type.GetType(formTypeFullName, true);
                                 frmShow = (Form)Activator.CreateInstance(type);
                                 frmShow.Location = new Point(0, 0);
                                 frmShow.Size = Screen.PrimaryScreen.WorkingArea.Size;
                                 frmShow.StartPosition = FormStartPosition.CenterScreen;
                                 frmShow.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                                 frmShow.Dock = DockStyle.Fill;
                                 FormCollection fc = Application.OpenForms;
                                 if (IsOpen == true)
                                 {
                                     foreach (Form frm in fc)
                                     {
                                         frm.Hide();
                                     }
                                     FlushMemory();
                                     frmShow.Show();
                                     IsOpen = false;
                                 }
                                 if (IsOpen == false)
                                 {
                                     int timeDelay = 12000;
                                     await Task.Delay(timeDelay);
                                 }
                                 Active_Timer.Enabled = false;
                                 Active_Timer.Start();
                                 //Active_Timer.Interval = 150;
                                 frmShow.Close();
                             }
                             reader.Close();
                         }
                     }

                 }
                 con.Close();
             }
         }
         catch (Exception Ex)
         {
             Logger onLogger = new Logger();
             onLogger.WriteErrorLog(Ex.ToString());
         }
         finally
         {
             frmShow.Dispose();
         }
     }

Richard MacCutchan

19-03-2019 20:13:15 System.OutOfMemoryException: Out of memory.
Вам нужно выяснить, какая часть вашего приложения потребляет всю память или ресурсы windows.

sharat naik

как любая логика, пожалуйста, подсказывает мне.

Richard MacCutchan

Извините, я не понимаю, что вы имеете в виду. Проблема четко сформулирована в сообщении об ошибке. Единственный способ исправить это - исследовать, что заставляет ваше приложение использовать всю память.

Gerry Schmitz

Код выглядит так, что делает глупые вещи с (многими) формами в цикле.

johannesnestler

so you create unlimited new forms in a loop and used up all your memory... I don't even bother to think about what you are trying to do here, as Gerry Schmitz said "silly stuff". So you thought structuring your code is not needed... We all were there - just learn from it. Give your Code a structure that is easy to follow and you will avoid such pitfalls. E.g split the timer_tick in more reasonable chuncks, make shure your code runs in your interval (100ms really?), secure it against "overtaking", think About what you do with the forms and how you instantiate them (better reuse?) and so on. I'm pretty sure your Problem will just disappear - or just "fix" the memory consumtion and run in the next problem due to bad structure next week… just my 2c

0 Ответов