File Transfer (3)-about Timer

Source: Internet
Author: User

File Transfer is designed to run Windows Services in the background. In the configuration file, you need to define a timer to check whether the specified time has arrived, in this way. net has several class libraries with timer definitions. For details, refer to msdn.

I used space system. timer In timers can set a time interval to execute events defined by elapsedeventhandler at intervals. Due to unstable network conditions, disconnection may occur from time to time, therefore, we need to define a time period. During this time period, we will check whether there are files to be uploaded at a certain time. However, due to the uncertainty of network transmission, the interval between checks is a problem, because the system. timer is multi-threaded, So if not processed, it will lead to resource competition. For example, the interval of timer is defined as 360000, that is, check whether a file needs to be uploaded or downloaded every six minutes. If the elapsedeventhandler event triggered by the previous timer is not completed, and the elapsedeventhandler event is triggered again, a problem occurs, you need to apply a lock through system. implement interlocked in threading.
Execute event processing when the value of the locavar variable is 0.ProgramCorrespondingCodeWhen the code is executed

If ( 0   = Interlocked. Exchange ( Ref Lockvar, 1 ))

Reset the value of lockvar after execution.

// Release lock
Interlocked. Exchange ( Ref Lockvar, 0 );

 

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.