Users Pricing

forum

Home Forums Backgroundworker stops twice – MindStick

Backgroundworker stops twice

marcel ethan 2108 24 Sep 2013

I have a problem with background worker. I don't know how exactly to describe it. Actually its a game and with the background worker ever x milisecs i update the progress bar and check if anyone has lost/won or the time is up. If someome has win the game ends. If both players have lost/time is up the game goes to the next round. The ploblem occurs when both players have lost. The method NextRound in the SetTime method, runs twice. Here is the code:

 void bw_ProgressChanged( object sender, ProgressChangedEventArgs e )
    {
        this.SetTime(e.ProgressPercentage);
    }
    void bw_DoWork( object sender, DoWorkEventArgs e )
    {
        Thread.Sleep(3000);
        BackgroundWorker worker = sender as BackgroundWorker;
        int tick=ProgLib.maxTime*10;
        for( int i = 1; i <= 100; i++ )
        {
            if( ( worker.CancellationPending == true ) )
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                Thread.Sleep(tick);
                worker.ReportProgress(i);
            }
        }
    }
        private void SetTime( double k )
    {
        this.time.Bar1.Value=k;
        this.time.Bar2.Value=k;
}

1 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md