C # timed to perform an action

Source: Internet
Author: User
A client sends a paper to the server socket, however, the server limits the frequency of transmission, if only 1 times within 10 seconds, then the client should also be the corresponding restrictions, the initial idea is to save the last time in the configuration file sent, the current delivery and this last time to compare, Send or hibernate the appropriate time according to the situation.
For example, the server sends a frequency limit of 10 seconds, last time is 10:00:00, there are two situations:
(1) The current time is 10:00:03, then after 7 seconds to send;
(2) The current time is 10:02:00, send immediately.

App.config
<!--Send Frequency limit (sec)-->
<add key= "Msgtimelimit" value= "ten"/>
<!--last time last sent--> <add
" Lastmsgtime "value=" 2013-11-1 "/>
Test.cs
CancellationTokenSource CT;

     private void Btnok_click (object sender, EventArgs e) {btnok.enabled = false;
     Task T = new Task (() => do (CT));
     ct = new CancellationTokenSource ();
     T.start (); T.continuewith ((x) => {this.
             Safecall (() => {richtextbox1.appendtext ("task over \ r \ n");
         Btnok.enabled = true;
     });
 }); } private void Btncancel_click (object sender, EventArgs e) {Ct.
Cancel (); }///<summary>///get send remaining time///</summary>///<returns></returns> private int Getmsgrestsecond
    S () {int msgtimelimit = 0; Gets the interval time (seconds) int to limit.
    TryParse (Appsettings.getvalue ("Msgtimelimit"), out Msgtimelimit);
    if (Msgtimelimit = = 0) return 0;
    Last time string lastmsgtime = Appsettings.getvalue ("Lastmsgtime");
    DateTime dtlastmsgtime = Datetime.minvalue;
    Datetime.tryparse (Lastmsgtime, out dtlastmsgtime);      
    DateTime Dtnow = DateTime.Now; if (DtlasTmsgtime = = Datetime.minvalue | |                 
    Dtlastmsgtime >= Dtnow) return 0;
    TimeSpan ts = dtnow-dtlastmsgtime;
    int restseconds = 0; if (Msgtimelimit > TS. totalseconds) {restseconds = msgtimelimit-(int) ts.
        TotalSeconds; Restseconds = Restseconds < 0?
    0:restseconds;
return restseconds; }

Where
Appsettings.setvalue () and Appsettings.getvalue () methods are shown:
http://blog.csdn.net/gdjlc/article/details/ 8284799

Safecall is an extension method
public static void Safecall (this control CTRL, Action callback)
{
     if (ctrl. invokerequired)
        Ctrl. Invoke (callback);
    Else
        callback ();
}

Click on the "Confirm" button to perform the following results:

Sending 1th customer ...
Wait, pause 10 seconds
is sending 2nd customer ...
Wait, pause 10 seconds
is sending 3rd customer ...
Task ended

after 3 seconds, click on the "Confirm" button and press "Cancel" to perform the first action as follows:

Please wait, pause 7 seconds
Sending 1th customer ...
Wait, pause 10 seconds
is sending 2nd customer ...
Task 3 Cancel
Task end

After 5 seconds, click the "Confirm" button to perform the following results:

Please wait, pause 5 seconds
Sending 1th customer ...
Wait, pause 10 seconds
is sending 2nd customer ...
Wait, pause 10 seconds
is sending 3rd customer ...
Task End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.