forum

Home / DeveloperSection / Forums / Backgroundworker stops twice

Backgroundworker stops twice

marcel ethan 1733 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;
}

wpf wpf 
Updated on 24-Sep-2013

Can you answer this question?


Answer

1 Answers

Liked By