最大的網(wǎng)站源碼資源下載站,
c#如何定時(shí)執(zhí)行程序(例如每天晚上12:00)
--------------------------------------------------------------------------------
類似于java里面的timer.schedule timetask
謝謝
--------------------------------------------------------------------------------
c#也有timer~~
system.timer
或者是 system.theading.timer
具體用法可以查詢下msdn
--------------------------------------------------------------------------------
使用timer
system.timers.timer atimer = new system.timers.timer();
public rechargefrm()
{
//
// windows 窗體設(shè)計(jì)器支持所必需的
//
initializecomponent();
components=null;
autotime.selectedindex=0;
atimer.elapsed+=new elapsedeventhandler(ontimedevent);
// create a new timer with interval set to 3 seconds.
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
--------------------------------------------------------------------------------
// only raise the event the first time 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#也有timer,另外也可以用thread,或者windows的計(jì)劃任務(wù)
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
}
--------------------------------------------------------------------------------
最簡(jiǎn)便的方法就是將定時(shí)執(zhí)行交給windows的計(jì)劃任務(wù)來(lái)完成;
或者實(shí)現(xiàn)一個(gè)windows服務(wù)
--------------------------------------------------------------------------------
windows的計(jì)劃任務(wù)是個(gè)不錯(cuò)的方法!
--------------------------------------------------------------------------------
那如何指定每天晚上12:00執(zhí)行?
或者說(shuō)晚上12:00第一次執(zhí)行,然后每3小時(shí)執(zhí)行一次
--------------------------------------------------------------------------------
我現(xiàn)在需要指定 第一次程序執(zhí)行的具體時(shí)間
--------------------------------------------------------------------------------
上面的方法都是延遲在什么時(shí)候發(fā)生的,樓主要的是”按時(shí)“執(zhí)行的,
up一下。
--------------------------------------------------------------------------------
用計(jì)劃任務(wù)不就可以啦?自己寫的話要寫windows服務(wù)。
--------------------------------------------------------------------------------
還是timer控制比較好吧.
--------------------------------------------------------------------------------
ohyear()理解我的意思了
計(jì)劃任務(wù)根本不可行,因?yàn)槲业膽?yīng)用是asp.net,而且是放在虛擬主機(jī)上
timer控制只能實(shí)現(xiàn)定時(shí)執(zhí)行,不能指定具體開始執(zhí)行的時(shí)間,例如晚上12:00開始執(zhí)行
--------------------------------------------------------------------------------
stringcurrtime=system.datetime.now.toshorttimestring();
string s="12:00";
if( currtime==s)
{ //程序執(zhí)行代碼
}
--------------------------------------------------------------------------------
xjpeng(海風(fēng)) 的改一下:
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
}
在todo something那里寫下面的:
stringcurrtime=system.datetime.now.toshorttimestring();
string s="12:00";
if( currtime==s)
{ //程序執(zhí)行代碼
}
這樣應(yīng)該可以了吧?
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
mark
--------------------------------------------------------------------------------
最簡(jiǎn)便的方法就是將定時(shí)執(zhí)行交給windows的計(jì)劃任務(wù)來(lái)完成;
或者實(shí)現(xiàn)一個(gè)windows服務(wù)
----------------------------------------------------------
how to implement the windows servic?
my email is: [email protected]
please contact me if you know it!
thank you
--------------------------------------------------------------------------------
學(xué)習(xí)
--------------------------------------------------------------------------------
得到時(shí)間的辦法可以執(zhí)行
要是此時(shí)c#程序已經(jīng)關(guān)閉了呢?怎么得到時(shí)間
--------------------------------------------------------------------------------
還是只能加入計(jì)劃任務(wù)吧
--------------------------------------------------------------------------------
egxsun():
這樣子可行是可行,程序看上去總是有點(diǎn)土
看來(lái).net的類庫(kù)里面沒提供類似于java的方法
--------------------------------------------------------------------------------
關(guān)注!
--------------------------------------------------------------------------------
關(guān)注。。。
同樣的問題:
如果機(jī)器程序都關(guān)閉了,system.time還能取得嗎?
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
asp.net做不了。iis端不能主動(dòng)做任何事的。要不你就在自己機(jī)器上寫個(gè)計(jì)劃任務(wù)每天定時(shí)去請(qǐng)求那個(gè)虛擬主機(jī)一下,讓它執(zhí)行事先寫好的任務(wù)。
所謂定時(shí)12點(diǎn)一般也不過(guò)是用timer,隔個(gè)幾秒鐘去問一下系統(tǒng)到?jīng)]到12點(diǎn)啊?到了就執(zhí)行這種。
--------------------------------------------------------------------------------
www.moblog.net.cn/sunshine.com這個(gè)軟件定時(shí)任務(wù) 功能 而且是 不用編譯入程序 直接用反射調(diào)用你的dll
--------------------------------------------------------------------------------
up
--------------------------------------------------------------------------------
msdn
--------------------------------------------------------------------------------
直接用c#自己的timer類就完全可以實(shí)現(xiàn)的?gt;>n乙丫芎玫目刂屏耍?nbsp;而且是很復(fù)雜的控制。沒有問題的。
--------------------------------------------------------------------------------
調(diào)用外部命令我知道~用at 命令
具體用法你加個(gè)/? 看下幫助!
--------------------------------------------------------------------------------
虛擬主機(jī)嗎?asp.net可不行
--------------------------------------------------------------------------------
關(guān)注
--------------------------------------------------------------------------------
還是做個(gè)啟動(dòng)服務(wù)
--------------------------------------------------------------------------------
我一直用windows計(jì)劃任務(wù)進(jìn)行自動(dòng)記費(fèi)系統(tǒng)的運(yùn)行!效果很好!
--------------------------------------------------------------------------------
gz
--------------------------------------------------------------------------------
控制面板-〉任務(wù)計(jì)劃-〉添加任務(wù)-〉選擇你的程序,設(shè)置時(shí)間
--------------------------------------------------------------------------------
學(xué)習(xí)
--------------------------------------------------------------------------------
使用windows自己的東西
--------------------------------------------------------------------------------
windows service
windows schedule
sql dts job
...
--------------------------------------------------------------------------------
最簡(jiǎn)單的方法~把你要執(zhí)行的程序語(yǔ)句寫在一個(gè)事件里面(就象button_onclick),然后用timer的tick調(diào)用那個(gè)事件就可以了~(當(dāng)然你要給timer的interval屬性賦上時(shí)間它是以毫秒計(jì)算的)用timer的enabled
來(lái)控制你是否執(zhí)行你的事件~
不知是否對(duì)你有幫助~我就是這么用的~呵呵~
--------------------------------------------------------------------------------
寫到計(jì)劃任務(wù)里面去嘛
我在用c#.net開發(fā)bs結(jié)構(gòu)程序中遇到一個(gè)問題,怎樣能夠不打開頁(yè)面的情況下查詢數(shù)據(jù)庫(kù)并執(zhí)行特定的操作,經(jīng)過(guò)網(wǎng)上查詢和試驗(yàn)得出如下代碼
更改 global.asax.cs 文件如下
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>
/// global 的摘要說(shuō)明。
/// </summary>
public class global : system.web.httpapplication
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </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();
}
#region web 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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
新聞熱點(diǎn)
疑難解答
圖片精選