How C # regularly executes programs (for example, 12:00 every night)

Source: Internet
Author: User
Tags datetime implement tostring
Program | timing | execution

How C # regularly executes programs (for example, 12:00 every night)
--------------------------------------------------------------------------------

Similar to the Timer.schedule timetask inside Java
Thank you

--------------------------------------------------------------------------------

C # also has timer~~
System.timer
or a System.Theading.Timer.
Specific usage can be queried under MSDN

--------------------------------------------------------------------------------

Use timer
System.Timers.Timer Atimer = new System.Timers.Timer ();
Public Rechargefrm ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
Components=null;
autotime.selectedindex=0;
Atimer.elapsed+=new Elapsedeventhandler (ontimedevent);
Create a new Timer with Interval set to 3 seconds.
//
TODO: Add any constructor code after the InitializeComponent call
//
}

--------------------------------------------------------------------------------

Only raise the event the Interval elapses.
Atimer.autoreset = true;
atimer.enabled = Flag;
Atimer.interval=int32.parse (AutoTime.SelectedItem.ToString ()) *300;//9s
Atimer.interval=int32.parse (AutoTime.SelectedItem.ToString ()) *1000*60;//30m
Console.WriteLine ("Press \ ' q\ ' to quit the sample.");
while (Console.read ()!= ' Q ');

--------------------------------------------------------------------------------

C # also has a timer, you can also use thread, or Windows scheduled tasks
Private Timer _timer;
private int _interval=30000;
_timer = new timer ();
_timer. Enabled = true;
_timer. Interval = _interval;
_timer. Elapsed + = new Elapsedeventhandler (timer_elapsed);
private void Timer_elapsed (object sender, Elapsedeventargs e)
{
Todo Something
}

--------------------------------------------------------------------------------

The easiest way to do this is to do a scheduled task that is scheduled to be delivered to Windows.
or implement a Windows service

--------------------------------------------------------------------------------

Windows ' Scheduled tasks are a good way to do it!

--------------------------------------------------------------------------------

So how do you specify 12:00 every night?
Or at night 12:00 for the first time, and then every 3 hours to execute

--------------------------------------------------------------------------------

I need to specify the time of first program execution.

--------------------------------------------------------------------------------

The above methods are all delayed when it happened, the main building is "on time" implementation,
Up a bit.

--------------------------------------------------------------------------------

Do you want to use a scheduled task? Write your own words to write Windows services.

--------------------------------------------------------------------------------

or timer control is better.

--------------------------------------------------------------------------------

Ohyear () understand what I mean.
Scheduled task is not feasible at all, because my application is asp.net, and it is on the virtual host
Timer control can only implement timed execution, can not specify the time to start execution, such as starting at 12:00

--------------------------------------------------------------------------------

Stringcurrtime=system.datetime.now.toshorttimestring ();
String s= "12:00";
if (currtime==s)
{//Program execution code
}

--------------------------------------------------------------------------------

Xjpeng (Sea Breeze) to change:
Private Timer _timer;
private int _interval=30000;
_timer = new timer ();
_timer. Enabled = true;
_timer. Interval = _interval;
_timer. Elapsed + = new Elapsedeventhandler (timer_elapsed);
private void Timer_elapsed (object sender, Elapsedeventargs e)
{
Todo Something
}
Write the following in the Todo something:
Stringcurrtime=system.datetime.now.toshorttimestring ();
String s= "12:00";
if (currtime==s)
{//Program execution code
}
This should be okay?

--------------------------------------------------------------------------------

Up

--------------------------------------------------------------------------------

Mark

--------------------------------------------------------------------------------

The easiest way to do this is to do a scheduled task that is scheduled to be delivered to Windows.
or implement a Windows service
----------------------------------------------------------
How to implement the Windows Servic?
my email is:lalac@163.com
Please contacts me if you know it!
Thank

--------------------------------------------------------------------------------

Learn

--------------------------------------------------------------------------------

The way to get time can be implemented
What if the C # program is closed now? How to get the time

--------------------------------------------------------------------------------

You can only join the mission.

--------------------------------------------------------------------------------

Egxsun ():
It's doable, and the program always looks a little bit earthy.
It seems that the. NET class library does not provide Java-like methods

--------------------------------------------------------------------------------

Concern!

--------------------------------------------------------------------------------

Concern...
The same question:
If the machine program is closed, can system.time still get it?

--------------------------------------------------------------------------------

Up

--------------------------------------------------------------------------------

ASP.net can't do it. The IIS side cannot take the initiative to do anything. Or you can write a scheduled task on your own machine every day to request that virtual host, let it perform the tasks written in advance.
The so-called timing 12 point is generally just with a timer, a few seconds to ask the system to 12 o ' clock ah? To do this.

--------------------------------------------------------------------------------

www.moblog.net.cn/sunshine.com This Software timer task function and is to call your DLL directly with reflection without compiling the program

--------------------------------------------------------------------------------

Up

--------------------------------------------------------------------------------

Msdn

--------------------------------------------------------------------------------

Directly with C # own timer class can be achieved completely? gt;>n B ya  xiong mei 刂 screen play, and is very complex control. No problem at all.

--------------------------------------------------------------------------------

Call external command I know, with the AT command
Do you add a/to the specific usage? Look at the Help!

--------------------------------------------------------------------------------

Virtual host? asp.net, but not.

--------------------------------------------------------------------------------

Concern

--------------------------------------------------------------------------------

or start a service.

--------------------------------------------------------------------------------

I have been using the Windows Planning task to automate the operation of the billing system! The effect is very good!

--------------------------------------------------------------------------------

Gz

--------------------------------------------------------------------------------

Control Panel-〉 Task Schedule-〉 Add Task-〉 Select your program, set the time

--------------------------------------------------------------------------------

Learn

--------------------------------------------------------------------------------

Using Windows own Stuff

--------------------------------------------------------------------------------

Windows Service
Windows schedule
SQL DTS Job
...

--------------------------------------------------------------------------------

The easiest way to write the program statement you want to execute is in an event (like Button_onclick), and then call that event with a timer's tick (you'll have to assign time to the timer's Interval property, which is calculated in milliseconds). With the timer's enabled
To control whether or not you're doing your thing.
I do not know whether to help you ~ I was so used ~ hehe ~

--------------------------------------------------------------------------------

Write to the planning task.


I am in the use of C#.net development BS structure program encountered a problem, how to be able to query the database without opening the page and perform a specific operation, through the online query and test to obtain the following code

Change the Global.asax.cs file as follows

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Web;
Using System.Web.SessionState;
Using System.Timers;
Using System.Data;
Using System.Data.SqlClient;

Namespace Sms_joke
{
<summary>
A summary description of Global.
</summary>
public class Global:System.Web.HttpApplication
{
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.IContainer components = null;

Public Global ()
{
InitializeComponent ();
}

protected void Application_Start (Object sender, EventArgs e)
{
SetTimer ();
}

protected void Session_Start (Object sender, EventArgs e)
{

}

protected void Application_BeginRequest (Object sender, EventArgs e)
{

}

protected void Application_EndRequest (Object sender, EventArgs e)
{

}

protected void Application_AuthenticateRequest (Object sender, EventArgs e)
{

}

protected void Application_Error (Object sender, EventArgs e)
{

}

protected void Session_End (Object sender, EventArgs e)
{

}

protected void Application_End (Object sender, EventArgs e)
{

}
public static void SetTimer ()
{
System.Timers.Timer Atimer = new System.Timers.Timer ();

atimer.elapsed + = new Elapsedeventhandler (OnTimer);

Atimer.interval = 5000;
Atimer.enabled = true;
}

public static void OnTimer (Object source, Elapsedeventargs E)
{
System.Data.SqlClient.SqlConnection SqlConnection1;
System.Data.SqlClient.SqlCommand SqlCommand1;
String customsetting = system.configuration.configurationsettings.appsettings["Sqlstr"];
SqlConnection1 = new SqlConnection ();
sqlconnection1.connectionstring = customsetting;
Sqlconnection1.open ();
String str = "UPDATE TABLE1 SET js = js + 1";

SqlCommand1 = new SqlCommand (str,sqlconnection1);
Sqlcommand1.executenonquery ();

Sqlconnection1.close ();

}
Code generated #region the Web forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
this.components = new System.ComponentModel.Container ();
}
#endregion
}
}



Http://www.zhinest.com/document/web/200572092418.htm
Http://www.rsblog.net/user1/3/archives/2005/265.html
Http://blog.joycode.com/percyboy/archive/2004/08/21/31240.aspx
Http://blog.joycode.com/yaodong/articles/25845.aspx



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.