0

I have the following code:

public async void startCountdown()
{
    do
    {               
        label9.Text = frsL.ToString();

        frsL -= frsLStep;

        g = tabPage1.CreateGraphics();
        g.DrawLine(pen1, 5, 7, (int)frsL + 5, 7);

        await Task.Delay(1000);

    } while (timeToSeconds());
}

I'm trying to refresh a progress line every second, but the line isn't changing. I used label9 just to check if frsL value is changing and it is, but the line isn't redrawn and it stays at it's starting length.

nevets
  • 4,631
  • 24
  • 40
ivanpop
  • 27
  • 2
  • 6
  • What does `timeToSeconds()` do? Can you add code for it? – LightBulb Aug 31 '14 at 10:08
  • Also, you're doing `g = tabPage1.CreateGraphics();` inside a loop which may be a cause for your problem because you're recreating the graphics context in every step of the loop. That part should be outside the loop. – LightBulb Aug 31 '14 at 10:10
  • timeToSeconds() just returns true while the program is running and false when it's finished. – ivanpop Aug 31 '14 at 10:11
  • 1
    Try calling `Application.DoEvents();`. An even better approach would be to execute the UI-related code in the UI thread. This can be done by using the `Invoke` method of the form or control, or by using a `Timer`. By the way, when you're using `Graphics` it is best to override the `OnPaint` method and perform your painting logic there and only there. – SimpleVar Aug 31 '14 at 10:12
  • Moving g = tabPage1.CreateGraphics(); outside the loop doesn't change anything. – ivanpop Aug 31 '14 at 10:17
  • 2
    If you're going for `Application.DoEvents()`, please read [this answer](http://stackoverflow.com/a/5183623/1698987) first, and probably [this blog](http://blog.codinghorror.com/is-doevents-evil/) as well. – Noctis Aug 31 '14 at 10:18
  • Is your progress bar supposed to go backwards (decrease) or forwards (increase)? `frsL` is being decreased here so it seems like you're drawing smaller line over the bigger one which is not going to be visible. – LightBulb Aug 31 '14 at 10:24
  • Why not using a `ProgressBar`? First rule of programming: do not reinvent the wheel. – nvoigt Aug 31 '14 at 10:27
  • I tried to put 'Application.DoEvents()' after 'g.DrawLine' but still nothing happens. I see change in the line when I switch tabs. When I go to tabPage2 and go back to tabPage1, the line's length is drawn shorter. Maybe I have to destroy the old line before drawing the new one? The line is decreasing. – ivanpop Aug 31 '14 at 10:27
  • @LightBulb has a good point. You should really just override the `OnPaint` method to *add* your own painting. Save the needed data for the `OnPaint` logic as members of the class (you kinda have to), update the data in the async loop and paint according to the data in the `OnPaint`. Note that you might have to call `Invalidate` as well to force the repaint event. – SimpleVar Aug 31 '14 at 10:30

2 Answers2

0

Yeah, the problem was that the line is decreasing and I have to remove the old line before drawing the new one. Inserting 'g.Clear(Color.White);' before 'g.DrawLine' fixed the problem. The bad thing is that sometimes the whole line flashes.

ivanpop
  • 27
  • 2
  • 6
  • Instead of clearing, simply only paint a second line for the rest of the way. Also turn on doublebuffering! Also do consider Yorye Nathan's recommendation! – TaW Aug 31 '14 at 11:05
-1

Hi I suggest you to put a timer in your form , set it's interval on 1000 and paste your code in it's tick function.

good luck!