为什么在控制台执行可以,而写到Global.asax不行??
我写了两个定时执行任务的计时器,为什么在控制台是都可以正常执行而,到了Global.asax就不行了呢 ,源代码如;
Global文件里的:
控制台程序两个都可以正常执行
帮忙看看怎么回事?
Global文件里的:
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
//这个计时器可以正常执行
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 1000;
myTimer.AutoReset = true;
myTimer.Enabled = true;
//下面这个线程执行不成功,只能执行一次
AutoResetEvent autoEvent = new AutoResetEvent(true);
TimerCallback timerDelegate = new TimerCallback(RequstMyPage);
System.Threading.Timer startTimer = new System.Threading.Timer(timerDelegate,autoEvent,0,1000*60*3);
}
private static void RequstMyPage(object state)
{
string url = "http://localhost:4015/ATPSystem/Default.aspx";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
//指定日志文件的目录
string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "SystemLogRequst";
string fileLogName = "SoftPrj_CN_" + DateTime.Now.ToLongDateString() + "_log.txt";
//定义文件信息对象
FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
//创建只写文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter strwriter = new StreamWriter(fs);
strwriter.BaseStream.Seek(0, SeekOrigin.End);
//写入错误发生时间
strwriter.WriteLine("请求页面发生时间: " + DateTime.Now.ToString());
//写入日志内容并换行
//strwriter.WriteLine("错误内容: " + message);
strwriter.WriteLine("错误内容: ");
//写入间隔符
strwriter.WriteLine("---------------------------------------------");
strwriter.WriteLine();
//清空缓冲区内容,并把缓冲区内容写入基础流
strwriter.Flush();
//关闭写数据流
strwriter.Close();
fs.Close();
}
}
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
//间隔时间执行某动作
//指定日志文件的目录
string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "SystemLog";
string fileLogName = "SoftPrj_CN_" + DateTime.Now.ToLongDateString() + "_log.txt";
//定义文件信息对象
FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
//创建只写文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter strwriter = new StreamWriter(fs);
strwriter.BaseStream.Seek(0, SeekOrigin.End);
//写入错误发生时间
strwriter.WriteLine("定时检查发生时间: " + DateTime.Now.ToString());
//写入日志内容并换行
//strwriter.WriteLine("错误内容: " + message);
strwriter.WriteLine("错误内容: ");
//写入间隔符
strwriter.WriteLine("---------------------------------------------");
strwriter.WriteLine();
//清空缓冲区内容,并把缓冲区内容写入基础流
strwriter.Flush();
//关闭写数据流
strwriter.Close();
fs.Close();
}
}
</script>
控制台程序两个都可以正常执行
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Timers;
using System.Threading;
namespace TimerTest
{
class Program
{
private static System.Timers.Timer aTimer;
static void Main(string[] args)
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = true;
aTimer.Enabled = true;
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
TimerCallback timerDelegate =
new TimerCallback(statusChecker.CheckStatus);
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
System.Threading.Timer stateTimer =new System.Threading.Timer(timerDelegate, autoEvent, 0, 5000);
Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine(System.DateTime.Now);
Console.WriteLine("Time Thread going");
}
}
class StatusChecker
{
int invokeCount, maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
public void CheckStatus(Object state)
{
Console.WriteLine( DateTime.Now.ToString("h:mm:ss")+"another Theard");
}
}
}
帮忙看看怎么回事?
...全文
相关推荐
Global.asax 文件是什么 有什么作用
**应用级别的事件处理**:通过在Global.asax 文件中编写事件处理代码,可以在特定的应用程序生命周期事件发生时执行相应的逻辑。
Global.asax 文件是什么
件驱动的编程模型使得 Global.asax 文件成为管理 ASP.NET 应用程序全局行为的关键组件。在深入了解这些事件之前,我们先来理解 Global.asax 文件的基本作用。
.net全局定时定期执行某些操作在Global.asax中具体实现
通过配置Global.asax文件,可以实现全局定时定期执行某些操作。首先,Global.asax文件是一个可以在*** Web应用程序中找到的可选文件。它包含了应用程序和会话的事件处理程序。
ASP.net全局程序文件Global.asax用法分析
Global.asax文件通常放在应用程序的根目录下,且一个应用程序只能有一个Global.asax文件。以下详细介绍了Global.asax文件中几个常见选项的含义及用法:1.
Global.asax_作用
Global.asax 文件的作用:Global.asax 文件是可选的,但它提供了许多有用的功能。开发人员使用位于特定 Web 应用程序根目录下的 Global.asax 文件来创作逻辑。


