C# Timer Interval doesn't work -


as written in title, in timer interval seems off. timer should take time "datetimepicker", convert seconds & change interval time set on datetimepicker. afterwards should post tweet on twitter, though doesn't work. keeps spamming posts on , over.

            private void intervalchoose_valuechanged(object sender, eventargs e) //datetimepicker     {         postinterval.interval = (intervalchoose.value.hour * 3600) + (intervalchoose.value.minute * 60) + intervalchoose.value.second;         savetimerinterval = postinterval.interval; //savetimerinterval set 0 @ beginning         messagebox.show("current interval in seconds: " + postinterval.interval.tostring());     }      private void button1_click(object sender, eventargs e)     {         if (button1.text == "start bot") //starts program (works)         {             intervalchoose.enabled = false;             messagebox.show(savetimerinterval.tostring());             postinterval.interval = savetimerinterval; //sets interval (once again, safecall)             postinterval.start(); //starts posting.             button1.text = "stop bot";         }     private void postinterval_tick(object sender, eventargs e)     {         var service = new twitterservice("", "", "", ""); //auth keys         var twitterstatus = service.sendtweet(new sendtweetoptions() { status = "hi" + counter });         if (twitterstatus != null)         {             messagebox.show("your message:\n\n\"" + "hi" + counter + "\"\n\n has been tweeted.", "success!", messageboxbuttons.ok);             counter++;          }         else         {             //messagebox.show("an error occured while bot running. please contact developer.", "error!", messageboxbuttons.ok, messageboxicon.exclamation);         }     label2.text = "next post in:\n" + (postinterval.interval / 3600) + " hour(s) " + ((postinterval.interval / 3600) / 60) + " minute(s) " + (((postinterval.interval / 3600) / 60) / 60) + " second(s)";     } 

result:

picture 1 picture 2

as seen in pictures, label won't refresh, nor new set time being used. randomly posted text in random time. in advance help.

as others mentioned, interval property expects milliseconds, not seconds. in case, don't need make conversions hand. datetimepicker returns datetime value. can time portion of datetime timespan using datetime.timeofday property. timespan's properties return various time parts, or total duration measured in specific unit. example, timespan.totalmilliseconds returns current time value milliseconds.

all need write is:

postinterval.interval = (int)intervalchoose.value.timeofday.totalmilliseconds; 

that takes care of invalid interval problem , works if want eg send message every 10 minutes or every 1 hour.

if want send message @ set time can't use system.windows.forms.timer because doesn't allow set specific start time or start delay. in case you'd have use system.threading.timer


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -