Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2008

Tip: Looking for answers? Try searching our database.

How to stop a paint event handler function by another event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wasishincar - 14 Jan 2008 04:14 GMT
Hi there,

I'm working on a stress test tool for drawing. After start button
clicked, it fires Form_Paint(xxx) event handler to continuous drawing
shapes on screen. Problem is once there is a while loop in the event
handler, so once entering the loop, no other event can capture from
application.

I tried to use a thread but it causes another problem that e.Graphics
will be out of scope, and raise an exception.

Below is my brief of my code. Could any one tell me how to make solve
this problem? Thanks.

private bool bTesting;

private void drawPage(Graphics g)
{
   // do something draw...
}

private void Form_Paint(Object sender, PaintEventArgs e)
{
  // skip

  while(this.bTesting)
  {
      drawPage(e.Graphics);
  }
}
Peter Duniho - 14 Jan 2008 04:26 GMT
> I'm working on a stress test tool for drawing. After start button
> clicked, it fires Form_Paint(xxx) event handler to continuous drawing
[quoted text clipped - 7 lines]
> Below is my brief of my code. Could any one tell me how to make solve
> this problem? Thanks.

Yes.  Don't use that code.

Your Paint event handler must do exactly one thing: paint the control  
_once_ and return immediately.  It's very bad to do anything else, and  
it's simply awful to have a loop that draws repeatedly.

This is a common error for people unfamiliar with the Windows API.  Here's  
one of the most recent threads, of many, in the newsgroups I follow  
addressing this very question:  
<http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_t
hread/thread/bfb53115470ebc76/f8704ea42d90042c
>

Pete
wasishincar - 14 Jan 2008 08:43 GMT
On Jan 14, 12:26 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

> > I'm working on a stress test tool for drawing. After start button
> > clicked, it fires Form_Paint(xxx) event handler to continuous drawing
[quoted text clipped - 20 lines]
>
> Pete

Thanks a lot, Pete.

After read Bob Powell's GDI+ guide, I have more sense about painting.

Now, I move the loop to a thread, but it failed to call Invalidate()
to update form. I copy the detail exception log below
System.NotSupportedException was unhandled
 Message="Chinese words" <- means something like "System can't
display error message due to no resource component" , sorry for my bad
translation.
 StackTrace:
   於 Microsoft.AGL.Common.MISC.HandleAr()
   於 System.Windows.Forms.Control.Invalidate()
   於 DisplayTest.FormDisplayStress.StartTest()
Could you give me some suggestion on thread. Thanks in advance.

My second code:
private bool bTesting;

private void buttonStart_Click(object sender, EventArgs e)
{
   Thread thread = new Thread(new ThreadStart(this.StartTest));
   thread.Start();
}

public void StartTest()
{
   while(this.bTesting)
   {
       this.getSomeShapeConfiguration();
       this.Invalidate(); // <-
   }
}

private void drawPage(Graphics g)
{
   // do something draw...
}

private void Form_Paint(Object sender, PaintEventArgs e)
{
  // skip
  drawPage(e.Graphics);
}
Peter Duniho - 14 Jan 2008 20:19 GMT
> After read Bob Powell's GDI+ guide, I have more sense about painting.
>
[quoted text clipped - 9 lines]
>     於 DisplayTest.FormDisplayStress.StartTest()
> Could you give me some suggestion on thread. Thanks in advance.

I see that Bob's site could be a little more beginner-friendly (and he's  
still got at least one bug in one of his samples, that I reported to him  
but he hasn't fixed).  But, here's an article that sort of addresses this  
question:
http://www.bobpowell.net/animation.htm

If you read through it, you'll see he's using the  
System.Windows.Forms.Timer class instead of a Thread.  In his case, he  
wants to draw things at specific intervals, and the Forms.Timer class  
specifically ensures that the code executed at those intervals is run on  
the correct thread.

In your code, you have the problem that the code calling Invalidate() is  
on the wrong thread, thus the exception.  Depending on how your  
application's animation is supposed to work, you can either modify your  
application so that instead of using a Thread, you just call  
"getSomeShapeConfiguration()" from a Tick event handler for a Forms.Timer  
object, or you can fix the StartTest() method so that it deals with the  
cross-thread call correctly.

As an example of the latter:

public void StartTest()
{
    while(bTesting)
    {
        getSomeShapeConfiguration();
        Invoke((MethodInvoker)delegate { Invalidate(); });
    }
}

Using the Invoke() method causes the anonymous method to be executed on  
the GUI thread, avoiding the exception.

Pete
wasishincar - 15 Jan 2008 05:49 GMT
On Jan 15, 4:19 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

> > After read Bob Powell's GDI+ guide, I have more sense about painting.
>
[quoted text clipped - 45 lines]
>
> Pete

Great thanks. I finally use Timer to get this test tool done. : )

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.