Asp.net timed Automatic Execution of console programs

Source: Internet
Author: User

The requirement is to execute the task of Automatically Generating static files every 20 minutes on a website. Because the website is placed in the purchased virtual space, there is no Server Management permission. Therefore, Windows Services and automatic tasks do not work. My idea is to use the system. Timers. Timer class to create a thread in the background during website running to regularly execute the console program that generates static files.

First, the console program is directed to the binfolder of the website, such as static.exe. Create a scheduled task in the application_start method of Global. asax. The global. asax code is as follows:

1 <% @ application language = "C #" %>
2
3 <% @ import namespace = "system. Diagnostics" %>
4 <% @ import namespace = "system. Timers" %>
5
6 <SCRIPT runat = "server">
7 timer;
8
9 void application_start (Object sender, eventargs E)
10 {// execute the console program once every minute
11 timer = new system. Timers. Timer (60000 );
12 timer. elapsed + = new elapsedeventhandler (execonsole );
13 timer. Start ();
14}
15
16 void application_end (Object sender, eventargs E)
17 {
18 if (timer! = NULL)
19 {
20 timer. Stop ();
21 timer. Close ();
22}
23
24}
25
26 void application_error (Object sender, eventargs E)
27 {
28 // code that runs when an unhandled error occurs
29
30}
31
32 void session_start (Object sender, eventargs E)
33 {
34 // code that runs when a new session starts
35
36}
37
38 void session_end (Object sender, eventargs E)
39 {
40 // the code that runs at the end of the session.
41 // Note: Only the sessionstate mode in the web. config file is set
42 // inproc, The session_end event will be triggered. If the session mode is set to StateServer
43 // or sqlserver, the event is not triggered.
44
45}
46
47
48 Private Static void execonsole (Object sender, system. Timers. elapsedeventargs E)
49 {
50 string Path = @ "D: \ aspnetautotask \ bin \ static.exe ";
51 string cmd = path + @ "D: \ aspnetautotask \";
52
53 Process = new system. Diagnostics. Process ();
54 processstartinfo startinfo = new processstartinfo ("cmd.exe ");
55 startinfo. useshellexecute = false;
56 startinfo. redirectstandardinput = true;
57 startinfo. redirectstandardoutput = true;
58 process. startinfo = startinfo;
59 process. Start ();
60
61 process. standardinput. writeline (CMD );
62 process. standardinput. writeline ("exit ");
63}
64
65 </SCRIPT>

The execonsoleis the role that executes the console program in asp.net. For more information, see "csung". Run the. exe file in a single process and obtain the output.

In this way, you can execute automatic tasks on the VM.

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.