Mesauring paint on a winforms panel
-
Hello, How can I measure the time that controls are being painted on a panel ? Thanks, berlus
If I understand you correctly you have a Form, a Panel on that Form, and some Controls on the Panel. The Controls get painted whenever there is a need to, and you would like to measure how long it takes to paint them. Correct? I could explain how to get the measurement done (or what to change so it can be done), however I'd rather tell you that you asking this tells me (1) it is slow, and (2) you should fix that. Event handlers, including the Paint handler, should execute in a snap, never keeping the GUI thread busy (or waiting) for more than say 20 milliseconds, otherwise the GUI experience would be bad (e.g. a window covering most of your form, then disappearing, would take too long for everything to settle; or you dragging the Form by its title would not follow your mouse smoothly). I suggest you tell more about your current situation: which controls, showing what kind of data (and where it comes from), etc. Plus a detailed symptom description. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Nil Volentibus Arduum
-
If I understand you correctly you have a Form, a Panel on that Form, and some Controls on the Panel. The Controls get painted whenever there is a need to, and you would like to measure how long it takes to paint them. Correct? I could explain how to get the measurement done (or what to change so it can be done), however I'd rather tell you that you asking this tells me (1) it is slow, and (2) you should fix that. Event handlers, including the Paint handler, should execute in a snap, never keeping the GUI thread busy (or waiting) for more than say 20 milliseconds, otherwise the GUI experience would be bad (e.g. a window covering most of your form, then disappearing, would take too long for everything to settle; or you dragging the Form by its title would not follow your mouse smoothly). I suggest you tell more about your current situation: which controls, showing what kind of data (and where it comes from), etc. Plus a detailed symptom description. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Nil Volentibus Arduum
Thanks for your answer. I'm developing a GDI+ (can't use direct draw or open GL) framework, and i'm trying to decide between two alternatives to draw a certain symbol. In order to decide which method to use, i'm creating a PanelWithStopwatch (derived from Panel), and drawing on this panel the desired symbol. I want to use the stopwathc to measure the time that each panel was painted. I hope I have clarified the need for the performance test. Thanks, Berlus
-
Thanks for your answer. I'm developing a GDI+ (can't use direct draw or open GL) framework, and i'm trying to decide between two alternatives to draw a certain symbol. In order to decide which method to use, i'm creating a PanelWithStopwatch (derived from Panel), and drawing on this panel the desired symbol. I want to use the stopwathc to measure the time that each panel was painted. I hope I have clarified the need for the performance test. Thanks, Berlus
I understood you had Controls on the Panel, now you say it is a symbol, so I'm still confused. If there are no listing controls involved (ListBox, TreeView, DataGridView) and no databases, I doubt it will take more than say one millisecond (unless you are messing with huge images, transparency, ...). Anyway, you could implement one way of painting things, then have a button click handler that contains:
start stopwatch
for (onethousand times) Panel.Refresh
stop stopwatchThis should repaint your panel all the time, whatever it contains. And then you could repeat it with your alternative way of painting things. When in doubt it really repaints every time, change it a bit, e.g. perform a TranslateTransform. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Nil Volentibus Arduum
-
I understood you had Controls on the Panel, now you say it is a symbol, so I'm still confused. If there are no listing controls involved (ListBox, TreeView, DataGridView) and no databases, I doubt it will take more than say one millisecond (unless you are messing with huge images, transparency, ...). Anyway, you could implement one way of painting things, then have a button click handler that contains:
start stopwatch
for (onethousand times) Panel.Refresh
stop stopwatchThis should repaint your panel all the time, whatever it contains. And then you could repeat it with your alternative way of painting things. When in doubt it really repaints every time, change it a bit, e.g. perform a TranslateTransform. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Nil Volentibus Arduum
Thanks, By symbol I mean a GDI+ drawings (such as pen, circle, etc). Do you have any suggestions on how to implement a panel with a textbox, that upon invalidation and repainting, that text box will be updated with the amount of time it took the panel to repaint itself ? Thanks, Berlus
-
Thanks, By symbol I mean a GDI+ drawings (such as pen, circle, etc). Do you have any suggestions on how to implement a panel with a textbox, that upon invalidation and repainting, that text box will be updated with the amount of time it took the panel to repaint itself ? Thanks, Berlus
Hi, you need to be more accurate when explaining things; you first said Control, then symbol, now drawing; and a pen is not a drawing. I explained how to do it. Here is working code:
public void RunTimingTest() { Form f=new Form(); Button btn=new Button(); btn.Text="Start"; btn.Click+=new EventHandler(btn\_Click); btn.Bounds=new Rectangle(20, 20, 200, 40); f.Controls.Add(btn); lbl=new System.Windows.Forms.Label(); lbl.Bounds=new Rectangle(20, 80, 400, 30); f.Controls.Add(lbl); p=new MyPanel(); p.Bounds=new Rectangle(20, 150, 200, 100); p.BackColor=Color.Yellow; f.Controls.Add(p); f.Show(); } void btn\_Click(object sender, EventArgs e) { p.Count=0; lbl.Text=""; Stopwatch sw=new Stopwatch(); sw.Start(); int n=100\*r.Next(1, 10); for (int i=0; i<n; i++) { p.Text="MyPanel "+i.ToString(); p.Refresh(); } sw.Stop(); lbl.Text="Did "+p.Count+" paints in "+sw.ElapsedMilliseconds+" msec"; } public class MyPanel : Panel { public int Count; public string Text; protected override void OnPaint(PaintEventArgs e) { Count++; base.OnPaint(e); e.Graphics.DrawString(Text, Font, Brushes.Black, new Point(20, 20)); } }
The spoon feeding ends here. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Nil Volentibus Arduum
-
Hi, you need to be more accurate when explaining things; you first said Control, then symbol, now drawing; and a pen is not a drawing. I explained how to do it. Here is working code:
public void RunTimingTest() { Form f=new Form(); Button btn=new Button(); btn.Text="Start"; btn.Click+=new EventHandler(btn\_Click); btn.Bounds=new Rectangle(20, 20, 200, 40); f.Controls.Add(btn); lbl=new System.Windows.Forms.Label(); lbl.Bounds=new Rectangle(20, 80, 400, 30); f.Controls.Add(lbl); p=new MyPanel(); p.Bounds=new Rectangle(20, 150, 200, 100); p.BackColor=Color.Yellow; f.Controls.Add(p); f.Show(); } void btn\_Click(object sender, EventArgs e) { p.Count=0; lbl.Text=""; Stopwatch sw=new Stopwatch(); sw.Start(); int n=100\*r.Next(1, 10); for (int i=0; i<n; i++) { p.Text="MyPanel "+i.ToString(); p.Refresh(); } sw.Stop(); lbl.Text="Did "+p.Count+" paints in "+sw.ElapsedMilliseconds+" msec"; } public class MyPanel : Panel { public int Count; public string Text; protected override void OnPaint(PaintEventArgs e) { Count++; base.OnPaint(e); e.Graphics.DrawString(Text, Font, Brushes.Black, new Point(20, 20)); } }
The spoon feeding ends here. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Nil Volentibus Arduum
-
Thanks for your answer, I will try to explaing myself better next time. I hope I won't vommit from your spoon , :).... Thanks, Berlus
Did you solve your problem? What is the outcome? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.