Thelostcircuit

I have a Problem, I have a timer that runs and a event has to be run within this timer. The event is triggered by a button. My question is how do i get the brogram to "ask" if the specific timer is still running The event can be run before or after the timer is ended but it will be maked as early or late.

Any help would be cool

Thanks
Thelostcircuit


Re: Visual C# Express Edition Timer

jrboddie

Try reading the Enabled property of the timer. It should be true if running and false if stopped.





Re: Visual C# Express Edition Timer

OmegaMan

To add to jrboddie said...When the timer is finished dispose of the object and set its reference to null and check for that condition. The timer object should be properly disposed of when done regardless, unless the need to restart the timer is needed.





Re: Visual C# Express Edition Timer

Thelostcircuit

Ok really what I'm trying to do is tell if someone did something late. I have a time that gets set and that is when a button is supposed to be pressed. but I'm giving the person 5 minutes to press the button before it will be marked late. Is it better to use a timer or would it be better to set a another addminutes thing and get the time that it would be late and ask the question is now after LateTime. I don't know if it can be done that way or not.

DateTime PressButton = DateTime.Now;
PressButton = myTime.AddHours(1)

DateTime LateButton = PressButton;
LateButton = LateButton.AddMinutes(5)

if now = > LateButton make as late

sort of thing






Re: Visual C# Express Edition Timer

jrboddie

I think your approach of comparing the DateTime of the button push to the expected time should work. You don't need to use the timer at all.





Re: Visual C# Express Edition Timer

Thelostcircuit

The only reason i though of the if statement way was i thought that the timers take up CPU power so i need to run like 5 instances at once it might get slowed.




Re: Visual C# Express Edition Timer

jrboddie

It seems to me that if you only need to check the time when the first event happens and when the button is eventually pushed, you do not need to use the timer.

However, if there is a possibility that the button does not get pushed or you want to report to the user that the elapsed time has passed without any input, then you will need to use a timer.





Re: Visual C# Express Edition Timer

Thelostcircuit

Ok, I tried this

DateTime Ftime = new DateTime.Now();

if (Ftime > Flol )
{
label14.Text = "late";
}
else (Ftime < Flol );
{
label14.Text = "ontime";
}


Flol is a time using the addminutes

Error 1 The name 'Flol' does not exist in the current context

Flol is created be another button press




Re: Visual C# Express Edition Timer

jrboddie

This just means that Flol is not scoped correctly.

Define Flol above the button push event.

Like this:



DateTime Flol;
private void button1_Click(object sender, EventArgs e)
{
Flol = DateTime.Now;
Flol = Flol.AddSeconds(5);
label1.Text = "Waiting";
}

private void button2_Click(object sender, EventArgs e)
{
DateTime pTime = DateTime.Now;

if (pTime > Flol)
{
label1.Text = "Late";
}
else
{
label1.Text = "OK";
}
}






Re: Visual C# Express Edition Timer

Thelostcircuit

I tried the code that you put there and it worked just like i want mine to. but i did your changes to my code and it tells me that

Warning 1 Field 'RollerTumble.Form1.Flol' is never assigned to, and will always have its default value

DateTime Flol = Flo.AddMinutes(1);

I would think that that was it's assignment I have to apologize im new at Visual Studio and C#

Thanks
Jon




Re: Visual C# Express Edition Timer

jrboddie

Jon,

No problem.  Not seeing your code, I am guessing here but I suspect that your statement:

DateTime Flol = Flo.AddMinutes(1);

is inside the button push event handler.  This makes that instance of the variable "Flol" only visible to that method.  It is said to be "local" to that routine.  If you also placed the declaration

DateTime Flol;

above the button push event handler (as in my example), that makes a variable "Flol" which will be visible to both button push methods.  This is the variable you want to use.  Since this "Flol" is never given a value, you get the warning.

Change the line inside the button push event:

DateTime Flol = Flo.AddMinutes(1); 

to simply: 

Flol = Flo.AddMinutes(1);

By the way, do you really have "Flo.AddMinutes(1)" and not "Flol.AddMinutes(1)"

In C#, variables have to be declared before use, int, double, string DateTime etc.  Where they are declared is important.

Hope this works.  If not, cut and paste the code into a post, and we can sort it out.





Re: Visual C# Express Edition Timer

Thelostcircuit

jrboddie,

That worked out perfectly after I did the same thing with the ftime variable. I didn't know you didn't have to have the datetime thing in front of it after it was created.

Thankyou
Jon