c#.net 怎么定时执行代码

dreamzyl 2009-05-30 10:41:58
.net(C#)怎么定时执行代码
...全文
744 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kid_wang 2009-06-02
  • 打赏
  • 举报
回复
你在数据库代码里写,不一定非要在C#里写啊。
坤哥zzz 2009-06-02
  • 打赏
  • 举报
回复
直接用ajax里的timer控件就挺简单,设置好interval(时间间隔),在Tick事件里写你要执行的代码,最好是放到updatepanel里
chen_ya_ping 2009-06-02
  • 打赏
  • 举报
回复
在window Application 中添加一个Timer控件设置它的 this.timer1.Enabled = true;(启动这个Timer,默认是false)
this.timer1.Interval = 3000;(3秒钟执行一次) Timer.Click事件

代码如下:
private void timer1_Tick(object sender, EventArgs e)
{
ShowTextBoxValue();
}

private void ShowTextBoxValue()
{
this.textBox1.Text = (Convert.ToInt32(this.textBox1.Text) + 1).ToString();
}

3秒钟TextBox1中的值就会自动+1
moshangmoxia 2009-05-30
  • 打赏
  • 举报
回复
4楼的都把msdn上的源码都copy给你了。还没明白吗?
dreamzyl 2009-05-30
  • 打赏
  • 举报
回复
我是想根据从数据库中提取的上班时间,到上班时间后判断是否有员工迟到,如果有的话发送短信提示他,到下班时间后,判断今天的任务是否完成,如果没有完成也发送短信提示,请各位高手赐教!
yzwmg 2009-05-30
  • 打赏
  • 举报
回复
你是要这个程序一直运行着吗?
如果是这样的话,那么给把时钟写在gloab文件中了
void Application_Start(object sender, EventArgs e)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000 * 60;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{//在这里写你要运行的程序
}
SATAN1122 2009-05-30
  • 打赏
  • 举报
回复
没看明白楼上的和LZ的问题有什么关系··
周公 2009-05-30
  • 打赏
  • 举报
回复
一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托或 ParameterizedThreadStart 委托指定由线程执行的程序代码。使用 ParameterizedThreadStart 委托可以将数据传递到线程过程。

在线程存在期间,它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级,但不能保证操作系统会接受该优先级。

GetHashCode 提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。

注意:
操作系统 ThreadId 和托管线程没有固定关系,这是因为非托管宿主能控制托管与非托管线程之间的关系。特别是,复杂的宿主可以使用 CLR Hosting API 针对相同的操作系统线程调度很多托管线程,或者在不同的操作系统线程之间移动托管线程。


一旦启动线程,便不必保留对 Thread 对象的引用。线程将继续执行,直到该线程过程完成。
下面的代码示例说明简单的线程处理功能。

using System;
using System.Threading;

// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}

public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));

// Start ThreadProc. Note that on a uniprocessor, the new
// thread does not get any processor time until the main thread
// is preempted or yields. Uncomment the Thread.Sleep that
// follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);

for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}

Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
ME_Rz 2009-05-30
  • 打赏
  • 举报
回复
有的
dreamzyl 2009-05-30
  • 打赏
  • 举报
回复
麻烦给出具体的实例,拜托~
luckylongge 2009-05-30
  • 打赏
  • 举报
回复
好像有个Timer控件
dreamzyl 2009-05-30
  • 打赏
  • 举报
回复
因为我的程序需要定时执行许多方法,既要判断有没有员工旷工和迟到,还要判断今天有没有客户要来,并且如果是星期一的话,要把每个员工的本周计划定时发送给部门主管,那么,一个线程能够同时处理这么多任务吗?请赐教!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧