Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
System.ComponentModel.Win32Exception (0x80004005): Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
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()


<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()


What I have tried:

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();
         }
     }
Posted
Comments
Richard MacCutchan 20-Mar-19 5:28am    
19-03-2019 20:13:15 System.OutOfMemoryException: Out of memory.
You need to find out which part of your application is consuming all the memory, or windows resources.
sharat naik 20-Mar-19 7:24am    
how any logic please suggest me.
Richard MacCutchan 20-Mar-19 8:22am    
Sorry, I do not understand what you mean. The problem is clearly stated by the error message. The only way to fix it is to investigate what is causing your application to use up all the memory.
[no name] 20-Mar-19 10:49am    
The code looks to be doing silly stuff with (many) Forms in a loop.
johannesnestler 20-Mar-19 11:46am    
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900