Timer not working properly? C#

my timer isnt working properly. Could someone tell me whats wrong?

public void Timer() { DateTime targetDate = DateTime.Now; if (DateTime.Now > targetDate) { int Now = DateTime.Now.Hour; string dtNow = Now.ToString(); targetDate = DateTime.Now.AddMinutes(1); int targetdate = targetDate.Hour; string TD = targetdate.ToString(); if (Now == targetdate){ Vote(); } } }

It should run the vote function when the target date is equal to the time Now.

Thanks, Oliver

-------------Problems Reply------------

I don't mean to be rude, but I think you are confusing what you have done, which is to create a method by the name of "Timer" with the concept of creating an object of Type Timer (as in System.Timers.Timer) which you might then create a delegate for the tick event to test the current time and invoke the vote method...

In short, the problem is you are way off course...

The time clock of your PC is very slow compared to the CPU clock frequency which is responsible for the execution speed of your code. This means that a lot of code can be executed until the time clock makes its next tick.


Also, I do not understand your logic. If you want to vote every minute, you should do something like this

private DateTime targetDate = DateTime.Now.AddMinutes(1);

public void Timer()
DateTime now = DateTime.Now;
if (now > targetDate) {
targetDate = now.AddMinutes(1);

However, using a System.Timers.Timer as Cos Callis suggests, seems more appropriate.

Category:c# Views:0 Time:2012-02-26

Related post

  • How can I get Unicode characters to display properly for the tooltip for the IMG ALT in IE7? 2008-08-14

    I've got some Japanese in the ALT attribute, but the tooltip is showing me the ugly block characters in the tooltip. The rest of the content on the page renders correctly. So far, it seems to be limited to the tooltips. --------------Solutions-------

  • Why Doesn't My Cron Job Work Properly? 2008-08-16

    I have a cron job on an Ubuntu Hardy VPS that only half works and I can't work out why. The job is a Ruby script that uses mysqldump to back up a MySQL database used by a Rails application, which is then gzipped and uploaded to a remote server using

  • How do you properly use namespaces in C++? 2008-09-03

    I come from a Java background, where packages are used, not namespaces. I'm used to putting classes that work together to form a complete object into packages, and then reusing them later from that package. But now I'm working in C++. How do you use

  • How can I get markdown to format this code properly? 2008-09-03

    Here is some code I could not get to format properly in markdown, this is straight C code, pasted into the text box with the '4 spaces' format to denote code: #define PRINT(x, format, ...) \ if ( x ) { \ if ( debug_fd != NULL ) { \ fprintf(debug_fd,

  • How to properly link your a custom css file in sharepoint 2008-09-10

    I've created a custom list, and made some changes to the way the CQWP renders it on a page by modifying ItemStyle.xsl. However, I'd like to use some custom css classes and therefore I'd like to link to my own custom .css file from the head tag of the

  • Under what circumstances does Internet Explorer fail to properly unload an ActiveX control? 2008-09-15

    I'm running into a perplexing problem with an ActiveX control I'm writing - sometimes, Internet Explorer appears to fail to properly unload the control on process shutdown. This results in the control instance's destructor not being called. The contr

  • How to properly handle exceptions when performing file io 2008-09-17

    Often I find myself interacting with files in some way but after writing the code I'm always uncertain how rubust it actually is. The problem is that I'm not entirely sure how file related operations can fail and, therefore, the best way to handle ex

  • Properly scoped transactions in Stored Procs 2008-09-18

    Suppose I have a stored procedure that manages its own transaction CREATE PROCEDURE theProc AS BEGIN BEGIN TRANSACTION -- do some stuff IF @ThereIsAProblem ROLLBACK TRANSACTION ELSE COMMIT TRANSACTION END If I call this proc from an existing transact

  • Weird MSC 8.0 error: "The value of ESP was not properly saved across a function call-" 2008-09-27

    We recently attempted to break apart some of our Visual Studio projects into libraries, and everything seemed to compile and build fine in a test project with one of the library projects as a dependency. However, attempting to run the application gav

  • Asp.Net MVC: How do I get Html.ActionLink to render integer values properly? 2008-09-28

    I have an asp.net mvc application with a route similar to: routes.MapRoute("Blog", "{controller}/{action}/{year}/{month}/{day}/{friendlyName}", new { controller = "Blog", action = "Index", id = "", friendlyName="" }, new { controller = @"[^\.]*", yea

  • Capistrano not restarting Mongrel clusters properly 2008-09-30

    I have a cluster of three mongrels running under nginx, and I deploy the app using Capistrano 2.4.3. When I "cap deploy" when there is a running system, the behavior is: The app is deployed. The code is successfully updated. In the cap deploy output,

  • How to properly implement a shared cache in ColdFusion? 2008-10-01

    I have built a CFC designed to serve as a dynamic, aging cache intended for almost everything worth caching. LDAP queries, function results, arrays, ojects, you name it. Whatever takes time or resources to calculate and is needed more than once. I'd

  • How to properly clean up Excel interop objects 2008-10-01

    I'm using the Excel interop in C# (ApplicationClass) and have placed the following code in my finally clause: while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet) != 0) { } excelSheet = null; GC.Collect(); GC.WaitForPendingFinal

  • How to properly use Struts ActionForms, Value Objects, and Entities? 2008-10-03

    I've inherited a large Java app that uses Struts, Spring, and Hibernate. The classes and interfaces I deal with daily are: Struts Actions, Struts ActionForms, Value Objects, Service Interfaces and Implementations, DAO Interfaces and Implementations,

  • Tab index not working properly 2008-10-08

    I have some controls in the tablelayoutpanel but the tab index for the control are not working properly. --------------Solutions------------- I don't know what your specific issue is because the question is a bit vague, but be aware that container co

  • How do I make my mouse in a VMWare Server virtual machine work properly? 2008-10-10

    My mouse, in my virtual server virtual machine is all jittery, and not working smoothly. Outside the virtual machine it is fine. My mouse is set to use the VMWare Pointing Device, which it says is working properly. I've tried the sample rate in 100 a

  • Is Donut Caching working properly with ASP.NET MVC? 2008-10-17

    And by donut caching, I'm refering to Scott Guthrie's blog post which is awesome. Does anyone know if that works properly? --------------Solutions------------- Yes, you can donut cache by following the steps in this blog post. Note that this techniqu

  • How do you properly use WideCharToMultiByte 2008-10-19

    I've read the documentation here: http://msdn.microsoft.com/en-us/library/ms776420(VS.85).aspx I'm stuck on this parameter: lpMultiByteStr [out] Pointer to a buffer that receives the converted string. I'm not quite sure how to properly initialize the

  • PHP not running properly 2008-10-27

    I have been having some problems trying to get my PHP running. When I try and run any scripts they appear in the source and do not run properly. This is the htaccess file: # Use PHP5 as default AddHandler application/x-httpd-php5 .php AddType x-mapp-

  • Hiding DIV does not work properly in Opera 2008-11-05

    My code works great in IE8, Firefox and Safari. But it doesn't work properly in Opera. What happens is that the DIV is hidden but the space occupied by the DIV remains to appear in my web-page. <div style=" z-index:-1;height :380; width:760; posit

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.158 (s). 11 q(s)