ASP. NET regularly calls WebService to run background code through global. asax and timer Timer

Source: Internet
Author: User

Effect:
In the application_start method of Global. asax of the website
Add a timer to regularly call WebService
A method of this WebService is responsible
Add data to a table in the database

Steps:
1. Create a website through
2. Join global. asax
3. Add WebService edit and reference
4. Edit global. asax
5. Save the running website to view the effect
==================================
1. Create a website through

2. Join global. asax
-----------------
The default content is as follows:
<% @ Application language = "C #" %>

Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts
}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.
}

</SCRIPT>
3. Add WebService edit and reference

4. Edit global. asax as follows:
----------------------------
Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts
System. Timers. Timer mytimer = new system. Timers. Timer (60000 );
Mytimer. elapsed + = new system. Timers. elapsedeventhandler (ontimedevent );
Mytimer. interval = 60000;
Mytimer. Enabled = true;
}
Private Static void ontimedevent (Object source, system. Timers. elapsedeventargs E)
{
Localhost. WebService A = new localhost. WebService ();
String S = A. helloworld ();
}

5. Save the running website to view the effect

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.