CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Posts
    1

    Question problem with timer help pls

    im trying to build a game of monopoly i have a problem with moving the players on board the func that calls for the playermover that has the timer continues before timer has a chance to end unless i put a message box that waits for user input....i tried thread.sleep(); but it wont work with it...
    Code:
     //call for playerMover class
    playerMover.movePlayer(steps, DataBase.players[currentPlayerIndexInArr], currentPlayerIndexInArr,waiting);
    Code:
     public void movePlayer( int steps,Player pl, int playersIndexInArr,toWait waiting)
            {
                toWait Waiting=waiting;
                currentPlayer = pl;
                setIndex(currentPlayer.getIndex());
                setSteps(steps);//why need this?
                setCurrentPlayerIndexInArr(playersIndexInArr);//this too
                counter = 0;
                Timer timer = new Timer();
                timer.Start();
                timer.Interval = 500;
                timer2 = timer;
                timer.Tick+=new EventHandler(timer_Tick);
    
                
              // with this box it works like it should
                MessageBox.Show(" ");
    
               
            }
    
            private void timer_Tick(object sender, EventArgs e)
            {
        
                if (counter < steps)
                {
                    if (indexInLocationsArr == DataBase.pointsArr.Length - 1)
                        indexInLocationsArr = 0;
                    else
                        indexInLocationsArr++;
    
                    board.setLocationToPlayerButton(DataBase.pointsArr[indexInLocationsArr], currentPlayerIndexInArr);
                    currentPlayer.setIndex(indexInLocationsArr);
    
                    if (indexInLocationsArr == 0)
                    {
                        //getSalary function
                        MessageBox.Show(currentPlayer.getName() + " get salary 200$");
                        currentPlayer.addCash(DataBase.salary);
                    }
                    counter++;
    
    
                }
                timer2.Stop();
                   
                   
            }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: problem with timer help pls

    I have to ask why you are using a timer here in the first place.

    A Windows.Forms.Timer acts as an asynchronous callback mechanism. The Tick callback is in fact executed on the main (UI) thread, so you essentially have a simple way to execute a block of code on an interval.

    However, you need to understand what it means to execute said code asynchronously. You are simply blocking the UI thread with your messagebox, so of course that blocks the Tick event. If that is giving you the desired behavior then you simply don't need a timer.

    You are also creating a new Timer for each call to the method, which is unnecessary and again goes against the purpose of using a timer in the first place. Are you trying to use this as a way to animate something? If it is not working as you say then we need more information. What is "not working" and how should it work?
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured