|
Well - you could do this without using a timer at all. Have a button on your form, which writes the time that the button was pressed to a member variable of type DateTime. Then have a second button that takes the time that the button was pressed away from DateTime.Now . That's how long it took to run.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Just a standard System.Windows.Forms.Timer sounds like what you need. Set the interval to 1 second, and have an incremental integer adding for every cycle the timer takes.
static System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
int iCurrentSeconds = 0;
private void NotificationConfig_Load(object sender, EventArgs e)
{
tmr.Tick += new EventHandler(TimerEventProcessor);
tmr.Interval = 1000;
tmr.Start();
}
private void TimerEventProcessor(object obj, EventArgs e)
{
iCurrentSeconds++;
Console.Writeline("Timer has been running for {0} seconds!", iCurrentSeconds);
}
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
Thank you both for your answers, I think this will solve the problem
--
Joplinazz
--
|
|
|
|
|
You bet, happy to help!
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
Why do you actually need to do this? It seems a lot more complicated than getting the difference between two datetime objects. It's certainly a lot more code - which while not always a bad thing, certainly doesn't adhere to the KISS principal.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete O`Hanlon wrote: Why do you actually need to do this? It seems a lot more complicated than getting the difference between two datetime objects. It's certainly a lot more code - which while not always a bad thing, certainly doesn't adhere to the KISS principal.
I understand what you are saying about the K.I.S.S. principal. From his short explanation he mentioned a while loop. I assumed that he was wanting to preform an operation multiple times within the timer, until a certain condition had been met. The most simple way to do that to me seemed to be a timer preforming the operations, and when the condition was met, do a timer.Stop().
Maybe I am missing something though, is it as efficent to do the above with a TimeSpan object?
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
Justin Perez wrote: I assumed that he was wanting to preform an operation multiple times within the timer, until a certain condition had been met. The most simple way to do that to me seemed to be a timer preforming the operations, and when the condition was met, do a timer.Stop().
Fair point. I read it as he wanted to get the difference between two points in time (and that was it), but you could well be right. Although my method would still work for that case because the only processing that occurs is at the button presses.
Justin Perez wrote: Maybe I am missing something though, is it as efficent to do the above with a TimeSpan object?
There's a bit of a gotcha with a TimeSpan in that you can't go over a day. Now I know it's unlikely, but it is vaguely possible that he started the process off and then left to go on holiday for a couple of weeks. When he gets back, if he's using the DateTime it will work - a TimeSpan and it'll crash.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete O`Hanlon wrote: There's a bit of a gotcha with a TimeSpan in that you can't go over a day. Now I know it's unlikely, but it is vaguely possible that he started the process off and then left to go on holiday for a couple of weeks. When he gets back, if he's using the DateTime it will work - a TimeSpan and it'll crash.
That's an interesting point you made. I am doing a couple bug fixes on an application right now. The application is an In/Out status application that keeps track of which employees are in the building. Lets say John Doe comes into the building at 9:45 PM on Monday October 8th. He leaves the building at 3:30 AM on Tuesday October 9th. I have 2 DateTime objects, and I try to do a TimeSpan between the 2. Are you saying that I will get an error when I try to get the span?
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
OK - slap me round the head with a wet kipper. I'm just plain wrong here. I've just had a quick look and the TimeSpan class does indeed allow you to use a greater range. So - saying this, I'd go with the TimeSpan object.
Thanks for making me go and have a look again. Worth a 5 for making me look an idiot.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete O`Hanlon wrote: OK - slap me round the head with a wet kipper. I'm just plain wrong here. I've just had a quick look and the TimeSpan class does indeed allow you to use a greater range. So - saying this, I'd go with the TimeSpan object.
Thanks for making me go and have a look again. Worth a 5 for making me look an idiot.
That's why I love codeproject. Bouncing around ideas with other developers really is great!
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
Justin Perez wrote: That's why I love codeproject.
It certainly is. It's the cut and thrust of the intellectual debate that goes on here.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
No kipper around, sorry.
A wet rain cheque will have to do for now.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
You may want to consider using Stopwatch [^] instead. (Requires .NET 3.0).
/ravi
|
|
|
|
|
I understand how to draw, for say a rectangle, but where do I draw it at, as in is there some event? Or...? Thanks to the same 4 or so people that help out. And anyone else, in advance.
|
|
|
|
|
All drawing should occur in your paint event. The reason is, Windows tells your form when to draw itself, for example if it was obscured by another window and then shown again. That's what the Paint event is for.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Yeah, thanks, I just found this good article that explains it well... In the paint event, you set up graphics, ad then (whatever your variable is).DrawWhatever (blah, blah, blah)
Thanks again!
|
|
|
|
|
Exactly. The other piece of the puzzle is, if you change something and want to force a redraw, you call Invalidate() to force a paint event.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
Can I suggest that you look at abstracting your drawing so that you don't end up with a huge paint routine? For instance, you want to draw a rectangle - then why not create a class that is responsible for managing the rectangle? The reason that I suggest this, is that it makes it easier to manage the drawing process if the components themselves are responsible for drawing themselves.
Typically, you would have a shape that inherits from some common class (call it ShapeBase for instance). Then you would add the shapes to a shape collection. When you call paint in your form, you would simply need to cycle through each shape in the collection and tell them to paint themself.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I've been reading this Dummies book for starters, (that's to the same people, they KNOW who they are...), but anyways, I'm trying to figure out some simple kind of collision detection for 2 squares, not per pixel. I've got some to work, but I need to know the correct way to do this, so if anyone would teach me a little on collision, I'd be very happy. Thanks in advance.
|
|
|
|
|
Collision of squares is easy. I believe the Rectangle class has a PtInRect method, just pass that the four corners of your other rectangle and see if any of them return true. A rectangle is easy because if any of the corners are inside the other rectange, then they have collided. Irregular shapes are more complex, I present one solution ( although I don't believe it's overly efficient ) in my series of articles on writing a game in C#.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I see what you are getting at, but could you give me a look at the code, just a simple snippet?
|
|
|
|
|
It's even easier than I thought:
Rectangle rc1 = new Rectangle(0, 0, 20, 20);
Rectangle rc2 = new Rectangle(19, 21, 5, 5);
if (rc2.IntersectsWith(rc1))
{
// match
}
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Are you serious?
|
|
|
|
|
Wait, but when I run the program, you can't see the rectangles. It's probably supposed to do that, but how do you use both collision and drawing? I get them seperate, but... Thnks for the help so far though.
|
|
|
|