![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 16
Rep Power: 0
![]() |
Timer on Form
I have made a lot of progress today with the help of members here and i thank the whole community for such a great forum. Before i go to sleep i thought I would post the last problem I ran into that i could not solve.
I have added a timer on a form. I declare a class scoped variable timeFlag public static byte timeFlag = 0; I set TimerFlag to 0, set the interval to 2mS enable the timer and do a while loop with a GC.keepAlive(timer1); The timeFlag is not incrementing I need 2mS delay between frames minimum, I will put the enable code AFTER the block is sent so i can execute code that calculates the 510 byte array but I am just trying to get the timer to work. During debugging, is there a tick clock variable i can look at like a program counter or hardware timer? I am use to assembler and 8 bit AVR programming. timeFlag = 0;
timer1.Interval = 2;
timer1.Enabled = true;
while (timeFlag <1)
{
GC.KeepAlive(timer1);
}
try
{
serialPort1.Write(mybyte, 0, 510);
}
...morecodei thought this code would get triggered when the 2 mS timer ran out then the while loop would fall through. The timer code private void timer1_Tick(object sender, EventArgs e)
{
timeFlag=1;
} |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, you don't show all your code, so we can't see your construction of the timer or your construction of the event handler. Have you had a look here?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
As DaWei clearly states, more code would be helpful in order to diagnose issues confidently, however one thing I have brought up here once or twice before, I believe, is that Timer.Enabled does not control the state of an Timer object - you must call Timer.Start() in order for it to start ticking.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2007
Posts: 16
Rep Power: 0
![]() |
This is one of those posts that will really accellerate my ability to learn how to use this language. Just knowing where to look for help for each class.
I thought I posted all the code. I am using visual studio c# 2005 gui where I place the Timer on the form using the form editor. The code for the timer Tick is what looks to be what gets executed once per Ms when the timer is enabled and increments an internal tick counter then number of Ms you set in the Interval. So a 2 mS delay I set Interval to 2 and start the timer, the code in the timer1_Tick i assume will execute when 2 Ms has passed and make the variable timeFlag=1 as my code shows. The properties do not show for example AuroReset as the msdn .NET framework center does and I know i need to set that as well. Trying to use timer1.AutoReset = true; gives error System.Windows.Forms.Timer does not contain a definition for Autoreset When i read what the Start method does, it says it starts the timer by setting the Enabled to true. However after changing the code to use timer1.Start() does not start the timer. Obviously I have missed a very big concept on programming in C# and understanding this concept will indeed be more than just how to set a timer. Tell me what other code I need to post as the forms editor generated code other than what i can get using the lighting bolt while the timer icon is selected is the Tick method. |
|
|
|
|
|
#5 | |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Quote:
The actual problem is a simple misunderstanding of the purpose and proper use of a WinForms timer. A form/control operates via the passing of messages. One message may be an indication that a button has been pressed. Another may be a request to repaint. These messages are queued by the OS and processed in a loop. If you perform a long running operation in a button click handler, the form will go all-white and won't close. The messages to paint and close are waiting there, but execution will not return to the message loop until the event handler returns (it's called by the message loop). Your program doesn't drop what it's doing because something interesting happened. WinForms timers operate by asking the OS to post a timer message to your form at a regular interval. The timerFlag will never be set because you are wasting cycles waiting for it to change. A watched pot, and all that. Instead of setting a timer flag, why don't you just do what you need in the Tick handler? That's the intended use of it. If you need execution to pause for a certain amount of time, look at Thread.Sleep. This is probably undesirable for a GUI app. No messages will be processed during that time for the same reason as now, but it would actually work Sleep is useful when using multiple threads or working with a console app, however.If you need a higher precision timer (note that the winforms timer is limited to 55ms accuracy), and one that will fire even if your main thread is busy, there are two other timers (in System.Timers and System.Threading) which use worker threads. It's not a matter of processing a message when you have time -- it's that your program is executing somewhere else (effectively at the same time), but sleeps for the intervals you give. Dealing with multiple threads properly is a very advanced topic, so feel free to ask for pointers if this is how you choose to proceed.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
My apologies for expressing false information, obviously I was firmly under the impression my words were precise, for whatever reason.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Oct 2007
Posts: 16
Rep Power: 0
![]() |
Quote:
Yes I have the form that turns white when this is run. What you are saying is the timer really did run out but the main thread was unable to do anything about it because I never returned to the main thread so it never called the timer tick method. The reasoning behind this is i was under the impression that each program only got a slice of time, i dont have a concept how much time that is or how many instructions can be done on different processors. I see now I really am in control of my program but if i don't follow the rules windows will shut me down! I thought it would just go and run other code and return to where it left me. The initial form here was to work on how to actually open the serial port send data and close it. I think I have an idea now what I need to do to run a timer and I will do some experimenting. It is quite different giving up full processor control and I have not yet grasped the speed of the programs execution as I am use to 20mhz 8 bit processors, about 10 million instructions per second. With a 3Ghz machine I have not yet looked at how much it can do in 2mS. When i started working with the ATMEL chips it took me a while to grasp the time frames I was working with. Now on to timers my next learning solution name. Thanks for the time, I will move to threads when I get a better grasp on timers perhaps. |
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Oct 2007
Posts: 16
Rep Power: 0
![]() |
I put the output and close port in the timertick method. I guess it was running, LOL I filled my screen with error messages that would not stop so I had to kill the process in the task manager. (added timer1Stop()
to fix that.Good thing I had it set to 500mS instead of 2mS whew. I am puzzled though why I can not access some of the other features of the timer I am working with. Two properties are missing as my last post shows AutoReset and SynchronizingObject, Container ans site are missing but on the .NET framework page. I did use System.nanoTime() in some java code but maby that's a java specific command. I am unpuzzled now, i see there are timers that just work in windows or forms and that's the one I am using. Last edited by metron9; Oct 9th, 2007 at 2:46 AM. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Oct 2007
Posts: 16
Rep Power: 0
![]() |
Just FYI I tested the timer at 2mS and made a camtasia move of the screen in swf format. link is here. You can see the letter A (in binary code as signals) dance around. Not even close to 2mS
I will be investigating how Game programmers keep track of time, but that's it for today I could do this all night but then I would get fired for not showing up at work. http://www.acousticlights.com/Untitled.html |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Windows is not an RTOS. Each process is getting around 15 ms. per time slice.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| FFT DWTIB -> Optimization -> Choosing An Appropriate Run Time | Sane | Software Design and Algorithms | 7 | Dec 1st, 2006 11:40 AM |
| Show or hiding forms/modifying control properties ..from different a form.. | cloud- | C# | 4 | Nov 10th, 2006 11:51 AM |
| Form Submit Blues | MegaArcon | Python | 3 | Dec 14th, 2005 5:20 PM |
| .NET Timer Form closing issue | MBirchmeier | C# | 4 | Nov 21st, 2005 11:00 AM |
| entering data into excel from a form | glevine | Perl | 1 | Nov 18th, 2005 6:03 PM |