Microsoft 开发人员网络 > 论坛主页 > Visual C# General > Please help me optimizing my app.
提出问题提出问题
 

已答复Please help me optimizing my app.

  • 2009年11月3日 10:02Abe119 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    hi, everybody

    I'm recently working on a windows form application for a automation test system.

    The app imports a ActiveX control for automation tests, which provides some events like "BeforeTestBegin","AfterTestDone"...
    On the form, there is a dataGridView. I need to add a new row of measurements to dataGridView in "AfterTestDone" event handler.
    Now my problem is that because the tests are running very fast, which is pretty much what we expected. Then after each TestDone, adding a new dataviewgrid row takes more time(compare to each test run), which low down the tests running speed.

    I was thinking about starting a new thread to keep tests running other than waiting for adding a new row finished. But I failed because It starts a new thread after each TestDone which causes conflict with the current row number.

    Here is my original snippet:

    private void axTestExecSL1_AfterTestDone(object sender, AxHPTestExecSL.DTestExecSLEvents_AfterTestDoneEvent e)
    {
                    string[] row = new string[5];
                    row[0] = UutPosId;
                    row[1] = TestName;
                    row[2] = LowLimit;
                    row[3] = HighLimit;
                    row[4] = LastMeasuredValue;
             
                    dataGridView.Rows.Add(row);
    }

    The measurement updates after each test done, which means If I create a new thread I should pass the measurements to the new thread. Can someone help me with that? I have no idea how to optimize the speed. Using thread? or other options?


    Kind Regards,







答案

全部回复

  • 2009年11月3日 10:19JohnWein 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码

    Assuming that updating the DGV is slowing things down, you can allow the AfterTestDone event to complete using a timer.  Try this before you implement threading. 

    Timer tmr = new Timer();
        private void Form1_Load(object sender, EventArgs e)
        {
          tmr.Interval = 1;  //Minimun, but not actually 1 ms. 
          tmr.Tick += tmr_Tick;
        }
        private void axTestExecSL1_AfterTestDone(object sender, AxHPTestExecSL.DTestExecSLEvents_AfterTestDoneEvent e)
        {
          string[] row = new string[5];
          row[0] = UutPosId;
          row[1] = TestName;
          row[2] = LowLimit;
          row[3] = HighLimit;
          row[4] = LastMeasuredValue;
          tmr.Tag = row;
          tmr.Start();
        }
        void tmr_Tick(object sender, EventArgs e)
        {      
          tmr.Stop();
         string[] row = (string[])tmr.Tag; 
          dataGridView.Rows.Add(row);
        }
    
    
    
    • 已编辑JohnWein 2009年11月3日 14:42
    • 已编辑JohnWein 2009年11月3日 14:35Corrected tmr_Tick code. Added stop.
    •  
  • 2009年11月3日 13:43Abe119 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    hi JohnWein ,

    Thanks for the reply.

    I've test it in my app. It still slows tests down. And I have a idea about change dataGridView to listbox.
    Hopefully it can speed up.

    Regards,
  • 2009年11月3日 13:58Tergiver 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Use virtual mode on the DGV. Since the DGV does nothing for rows outside what's being displayed, using virtual mode will result in there being zero impact after the first N rows.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.virtualmode.aspx
    • 已标记为答案Abe119 2009年11月4日 14:33
    •  
  • 2009年11月3日 14:43JohnWein 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Try it with the tmr_Stop(); statement.  Tergiver's suggestion is excellent.
  • 2009年11月4日 14:32Abe119 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks guys!
    Appreciated!