本人写了一个线程小工具..想每隔一个小时往文件里面写东西.可是他只能在每隔20分钟以下才能写入文件.我现在对这个问题是丈二和尚呀..
代码如下:InterfaceClass.cs 文件public interface InterfaceClass
{
void Work();
}Work_UpdateVersion.cs 文件public class Work_UpdateVersion : InterfaceClass
{
public void Work()
{
GetBarInfo.GetXml();//每小时写文件方法
}
}ListWorkThreading.cs 这个主要是那个线程循环的一些方法.超别人的.我也看不懂.public static class ListWorkThreading
{
private static List<InterfaceClass> worklist = new List<InterfaceClass>();
private static Semaphore worklistSemaphore = new Semaphore(0, int.MaxValue);
private static System.Threading.Thread workThread;
private static bool keepWork = false;
static ListWorkThreading()
{
workThread = new Thread(new ThreadStart(WorkThread));
workThread.IsBackground = true;
workThread.Priority = ThreadPriority.Lowest;
}
static public void Start()
{
keepWork = true;
workThread.Start();
}
static public void Stop()
{
keepWork = false;
worklistSemaphore.Release();
workThread.Join();
}
static public void AddWork(InterfaceClass w)
{
worklistSemaphore.Release();
lock (worklist)
{
worklist.Add(w);
}
}
static private InterfaceClass GetWork()
{
worklistSemaphore.WaitOne();
if (worklist.Count == 0) return null;
lock (worklist)
{
InterfaceClass one = worklist[0];
worklist.RemoveAt(0);
return one;
}
}
private static void WorkThread()
{
while (keepWork)
{
InterfaceClass one = GetWork();
if (keepWork == false) return;
if (one == null) continue;
one.Work();
}
}
}WorkThreading.cs 这个是设置时间的。public static class WorkThreading
{
static System.Threading.Timer gtimer;
public static void CallReportNet(object a)
{
ListWorkThreading.AddWork(new Work_UpdateVersion());
}
public static void start()
{
//一小时线程
int AutoActReportNetTime = 60* 60000;
gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, 0, AutoActReportNetTime);
}
}最后一个文件是我写入文件的方法.GetBarInfo.cspublic class GetBarInfo
{
public GetBarInfo()
{
}
/// <summary>
/// 向文件写入内容并根据bool append参数决定是改写还是追加 append=false则改写
/// </summary>
public static void WriteFile(string file, string fileContent, bool append)
{
FileInfo info = new FileInfo(file);
if (!Directory.Exists(info.DirectoryName))
{
Directory.CreateDirectory(info.DirectoryName);
}
StreamWriter writer = new StreamWriter(file, append, Encoding.GetEncoding("utf-8"));
try
{
try
{
writer.Write(fileContent);
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
}
finally
{
writer.Flush();
writer.Close();
}
}
public static void GetXml()
{
WriteFile(@"D:\www\Log\aa.txt", DateTime.Now.ToString() + "\r\n" , true);
}
}请各位大虾帮帮忙吧..我真的是没办法啦.
代码如下:InterfaceClass.cs 文件public interface InterfaceClass
{
void Work();
}Work_UpdateVersion.cs 文件public class Work_UpdateVersion : InterfaceClass
{
public void Work()
{
GetBarInfo.GetXml();//每小时写文件方法
}
}ListWorkThreading.cs 这个主要是那个线程循环的一些方法.超别人的.我也看不懂.public static class ListWorkThreading
{
private static List<InterfaceClass> worklist = new List<InterfaceClass>();
private static Semaphore worklistSemaphore = new Semaphore(0, int.MaxValue);
private static System.Threading.Thread workThread;
private static bool keepWork = false;
static ListWorkThreading()
{
workThread = new Thread(new ThreadStart(WorkThread));
workThread.IsBackground = true;
workThread.Priority = ThreadPriority.Lowest;
}
static public void Start()
{
keepWork = true;
workThread.Start();
}
static public void Stop()
{
keepWork = false;
worklistSemaphore.Release();
workThread.Join();
}
static public void AddWork(InterfaceClass w)
{
worklistSemaphore.Release();
lock (worklist)
{
worklist.Add(w);
}
}
static private InterfaceClass GetWork()
{
worklistSemaphore.WaitOne();
if (worklist.Count == 0) return null;
lock (worklist)
{
InterfaceClass one = worklist[0];
worklist.RemoveAt(0);
return one;
}
}
private static void WorkThread()
{
while (keepWork)
{
InterfaceClass one = GetWork();
if (keepWork == false) return;
if (one == null) continue;
one.Work();
}
}
}WorkThreading.cs 这个是设置时间的。public static class WorkThreading
{
static System.Threading.Timer gtimer;
public static void CallReportNet(object a)
{
ListWorkThreading.AddWork(new Work_UpdateVersion());
}
public static void start()
{
//一小时线程
int AutoActReportNetTime = 60* 60000;
gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, 0, AutoActReportNetTime);
}
}最后一个文件是我写入文件的方法.GetBarInfo.cspublic class GetBarInfo
{
public GetBarInfo()
{
}
/// <summary>
/// 向文件写入内容并根据bool append参数决定是改写还是追加 append=false则改写
/// </summary>
public static void WriteFile(string file, string fileContent, bool append)
{
FileInfo info = new FileInfo(file);
if (!Directory.Exists(info.DirectoryName))
{
Directory.CreateDirectory(info.DirectoryName);
}
StreamWriter writer = new StreamWriter(file, append, Encoding.GetEncoding("utf-8"));
try
{
try
{
writer.Write(fileContent);
}
catch (Exception exception)
{
throw new FileNotFoundException(exception.ToString());
}
}
finally
{
writer.Flush();
writer.Close();
}
}
public static void GetXml()
{
WriteFile(@"D:\www\Log\aa.txt", DateTime.Now.ToString() + "\r\n" , true);
}
}请各位大虾帮帮忙吧..我真的是没办法啦.
解决方案 »
- 程序点击关闭退出后,发现进程过10多秒之后才完全退出?
- 接触vb.net有一段时间。我想问我选错没有?
- winform中动态添加控件后,如何让他消失?
- 请问用sqlconnection连接sqlserver能比用oledbconnection有多大的性能提高
- .net中有轻量级的表达式引擎和脚本引擎吗?
- Photoshop里用钢笔勾出轮廓路径,怎么可以用编程做到?
- C#调用ACCESS数据库打包问题
- winform 中做出百度搜索一样的效果
- 使用adapter对dataset进行更新数据库的问题
- 关于将数据保存成excel xls格式的问题
- C#随机数问题
- 蜘蛛抓取网页内容时候出现乱码,有办法解决么
{
//一小时线程
int AutoActReportNetTime = 60* 60000;
gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, AutoActReportNetTime, 0);
}
改成
{
//一小时线程
int AutoActReportNetTime = 60* 60000;
gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, AutoActReportNetTime, AutoActReportNetTime);
}
改成
{
//worklistSemaphore.Release();
lock (worklist)
{
worklist.Add(w);
}
}
static private InterfaceClass GetWork()
{
//worklistSemaphore.WaitOne();
if (worklist.Count == 0) return null;
lock (worklist)
{
InterfaceClass one = worklist[0];
worklist.RemoveAt(0);
return one;
}
}注释这两句试试
{
//一小时线程
int AutoActReportNetTime = 60* 60000;
gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, 0, AutoActReportNetTime);
ListWorkThreading.Start();//把线程队列启动起来
}
private static void WorkThread()
{
Thread.Sleep(100);//要不cpu太高
while (keepWork)
{
InterfaceClass one = GetWork();
if (keepWork == false) return;
if (one == null) continue;
one.Work();
}
}
还有就是提到的把哪两个WaitOne()和Release()的调用去掉,要不占有没有释放,方法就不执行了
在应用程序池里面有个"空闲超时" 他里面默认的是20分钟..我设置的时间超过20分钟他就不写啦..
我现在把"空闲超时"这个设置给去掉了.就可以啦.
步骤如下:1.先查看你当前网站的应用程序池的名字 如:"ASP.NET V2.0"
2.在应用程序池中找到对应的名字.右击"属性" "性能" "空闲超时" 把这个选项去掉就可以啦.呵呵..