开发者

Windows form takes time to respond

I'm building a pretty big Winforms application. Everything worked fine, but in the past two days I'm have problems with times. For example, if I am running a loop that opens 8 tabs and creates a webbrowser in each, it takes it some time. The UI is kind of unresponsive while the function is running, but the big problem isn't in the creating.

I have a button that responsible for removing all the things I don't need from the UI(i.e. resetting it to the normal state). It just takes a huge time when there are about 10 tabs open. I measured the time and the time it takes the code to execute is about 1-1.2 seconds, but the time it takes to the UI to get responsive and preform everything I did is a开发者_如何学C lot more, about 10 seconds. here is a code example:

private void killGUI()
{
  DateTime a = DateTime.Now;
  TimeSpan b;
  this.SuspendLayout();
  //tabPages.RemoveAll(TabPage);
  tabPages.Clear();
  if (tabControl1.TabPages.Count > 1)
  {
      //MessageBox.Show("" + tabControl1.TabPages.Count);
      //DateTime a = DateTime.Now;
      /*while (tabControl1.TabPages.Count != 1)
      {
          //int i = 1;
          foreach (TabPage tab in tabControl1.TabPages)
          {
              if (tab.Name != "helpPanel")
              {
                  tabControl1.TabPages.Remove(tab);
                  tab.Dispose();
              }

          }
      }*/
      while (tabControl1.TabPages.Count > 1)
      {
        Application.DoEvents();
        TabPage t = tabControl1.TabPages[1];
        tabControl1.TabPages.RemoveAt(1);
        t.Dispose();
      }
      //TimeSpan v = DateTime.Now.Subtract(a);
      //MessageBox.Show(""+v.Milliseconds);
  }
  ///////
  b = DateTime.Now.Subtract(a);
  MessageBox.Show("REMOVING ALL TABS:" + a.Millisecond);
  a = DateTime.Now;
  ////////
  questions.ElementAt(0).richy.Dispose();
  questions.ElementAt(0).createNewCom.Dispose();
  //questions.ElementAt(questions.Count - 1).Name.Dispose();
  for (int i = 0; i < questions.ElementAt(questions.Count - 1).comments.Count; i++)
  {
    Application.DoEvents();
    if (questions.ElementAt(0).comments.ElementAt(i).texty != null)
        questions.ElementAt(0).comments.ElementAt(i).texty.Dispose();
    if (questions.ElementAt(0).comments.ElementAt(i).cButton != null)
        questions.ElementAt(0).comments.ElementAt(i).cButton.Dispose();
    Application.DoEvents();
  }
  /////
  b = DateTime.Now.Subtract(a);
  MessageBox.Show("REMOVING THIS QUESTION:" + a.Millisecond);
  a = DateTime.Now;
  /////
  panel1.Visible = false;
  while (panel2.Controls.Count != 0)
  {
    Application.DoEvents();
    panel2.Controls.RemoveAt(0);
  }

  panel2.Visible = false;
  backButton.Visible = false;
  forwardButton.Visible = false;
  //placePanel.Dispose();
  //urgencyPanel.Dispose();
  //categoriesPanel.Controls.Clear();
  //categoriesPanel.Dispose();
  //((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("placePanel", false)[0]).Dispose();
  ((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("categoriesPanel", false)[0]).Dispose();
  ((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("urgencyPanel", false)[0]).Dispose();
  ((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("placePanel", false)[0]).Dispose();

  //Controls[] con=tabControl1.Controls.Find("HelpPanel",false);
  newQuestionTextBox.Clear();
  browsers.Clear();
  panels.Clear();
  buttons.Clear();
  questions.RemoveAt(0);
  finalTuid = "";
  this.ResumeLayout();
  foreach (Control cl in helpPanel.Controls)
  {
    Application.DoEvents();
    if (cl.Name == "categoriesPanel" || cl.Name == "urgencyPanel" || cl.Name == "placePanel")
    {
      //WTF that shouldnt happen-i cant get this.
      //MessageBox.Show("!!!");
      cl.Dispose();
    }
  }
  foreach (Control cl in helpPanel.Controls)
  {
    Application.DoEvents();
    if (cl.Name == "categoriesPanel" || cl.Name == "urgencyPanel" || cl.Name == "placePanel")
    {
      //FFFFFFFFFFFUUUUUUUUUUUUUUUUUUU
      //MessageBox.Show("!!!!!!!!");
      cl.Dispose();
    }
  }
  /////
  b = DateTime.Now.Subtract(a);
  MessageBox.Show("ALL ELSE:" + a.Millisecond);
  ///////
  this.ResumeLayout();
}

also another probelm is if u can see:

((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("categoriesPanel", false)[0]).Dispose();
((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("urgencyPanel", false)[0]).Dispose();
((Panel)((TabPage)tabControl1.Controls.Find("helpPanel", false)[0]).Controls.Find("placePanel", false)[0]).Dispose();

This should remove three panels from the main panel, but it just doesn't work. I don't know why, but only after running the two loops below it removes the panels and just one loop isn't enough. thanks so much in advance :)


Is there a reason why you don't have a reference to the help panel or the 3 other controls you want to dispose that exist in that panel? It seems really inefficient to have to iterate over the list of controls in your GUI (you said you had lots of tabs & controls) looking for these 3 specific controls. At bare minimum, you should cache the result of the helpPanel search as the next two lines are repeating the same search process that just occurred. The following loops can be solved with the same solution.

Wait, those two loops are iterating over helpPanel.Controls. So you already have a reference to the helpPanel and just not using it in the earlier steps?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜