0

I am writing an MDI application in C# that contains two child forms which are referenced through DLL (stand alone projects added to references). One child form (call it form1) is performing animation based on graphic objects using a timer. This project (child form1) works fine until the other child form (call it form2) is loaded. What happens is that animation on the first form (form1) stops, but when I close this second child form (form2) animation continues with no problem.

I use this code in form2:

private void FlyingBeeForm_Paint(object sender, PaintEventArgs e) {
    Graphics g = e.Graphics; 
    DrawImages(g); 
    System.Threading.Thread.Sleep(50); 
    this.Invalidate(); 
}
dfeuer
  • 48,079
  • 5
  • 63
  • 167
  • Well, what's special about the 2nd form? All we know it is a fish. If you have no clue then comment out chunks of code in the 2nd form. – Hans Passant May 08 '12 at 14:34
  • It's also some animation,but with no timer. What that form2(fish) does is when you click mouse ,fishes move to area where clicked – falcon2303 May 08 '12 at 14:42
  • Well, you almost definitely need to use a timer to animate the fish. If you loop then the timer in the 1st form stops working. – Hans Passant May 08 '12 at 14:45
  • both projects(form1 and form2) are based on paint event (which occurs when control needs repainting) – falcon2303 May 08 '12 at 14:46
  • Post code, don't talk about it. The snippet you have now is useless to diagnose the problem. We don't need to see how the fish swim, just what keeps them going. – Hans Passant May 08 '12 at 14:49
  • It's bees not fishes; but how to make a new line?!? 'private void FlyingBeeForm_Paint(object sender, PaintEventArgs e) ' { Graphics g = e.Graphics; DrawImages(g); System.Threading.Thread.Sleep(50); this.Invalidate(); } – falcon2303 May 08 '12 at 15:02
  • Wow.It's actualy worked.I am new here,do I need to do something now when I got solution? – falcon2303 May 08 '12 at 15:12
  • when i click and move a window of this other form(form2) then animation speeds up in the (form1) – falcon2303 May 08 '12 at 15:14

1 Answers1

1

The timer is kept going by a notification delivered by Windows when the interval has expired. It is however a very low priority notification, you'll only get it when no other work needs to be done.

You now should see the problem with your Paint event handler. For one, you prevent any work from getting done by sleeping constantly. Then you ensure there's always work to be done by calling Invalidate(). So Windows generates a new paint event and never gets around to the state where a timer event can be delivered.

You must remove the Sleep and Invalidate call. And use a 50 msec timer instead. Just call Invalidate() in its Tick event handler.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • but new problem appears when, I click and move a window of this other form(form2) then animation speeds up in the (form1) and vice versa – falcon2303 May 08 '12 at 15:17
  • I trust that this got solved as well when you fixed the Paint event handler. Start a new question if that wasn't the case. – Hans Passant May 08 '12 at 15:26