Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer not working for delay?

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
I'm messing around with OOP concepts and I've found something I really don't understand about the Timer.
Can someone tell me why when I try to run the stopMove method the program freezes?

Thanks,
Harold

class MoveImageWithStop : MoveImage
{
public MoveImageWithStop()
{
this.BoolMove = true;
}

private bool boolMove;
public bool BoolMove
{
get
{
return boolMove;
}

set
{
boolMove = value;
}
}

public void MoveRight(PictureBox picture, int sleep)
{
System.Timers.Timer time = new System.Timers.Timer(300);
while (this.BoolMove == true)
{
base.MoveRight(picture);

time.Start();
}
time.Stop();
}

public void MoveLeft(PictureBox picture, int sleep)
{
System.Timers.Timer time = new System.Timers.Timer(300);
while (this.BoolMove == true)
{
base.MoveLeft(picture);

time.Start();
}
time.Stop();
}

public void stopMove()
{
this.BoolMove = false;
}

}
 
I would better use 1 timer that has an tick event but with diferent moving directions.
And this
Code:
System.Timers.Timer time = new System.Timers.Timer(300);
looks like an invalide timer declaration to me, the constructor expects as parameter an container object not an integer value

Code:
class MoveImageWithStop : MoveImage
{
    private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    private bool direction=false;//means right
    public MoveImageWithStop()
    {        
        this.BoolMove = true;
        //add the event procedure
        this.timer.Tick += new System.EventHandler(this.timer_Tick);
        this.timer.Interval=300//300 miliseconds
        this.timer.Enabled=true;//start the timer
    }
    private void timer_Tick(object sender, System.EventArgs e)
    {
      if(!BoolMove) this.timer.Enabled=false;//disable timer
      if(!direction)
      {
          MoveRight(...)
      }
      else
      {
          MoveLeft(...);
      }
    }
    private bool boolMove;
    public bool BoolMove
    {
        get
        {
            return boolMove;
        }

        set
        {
            boolMove = value;
        }
    }

    public void MoveRight(PictureBox picture, int sleep)
    {
     //move right
    }

    public void MoveLeft(PictureBox picture, int sleep)
    {
     //move left
    }

    public void stopMove()
    {        
        this.BoolMove = false;        
    }

}
This should do it.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top