本人写了一个线程小工具..想每隔一个小时往文件里面写东西.可是他只能在每隔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);          
    }
}请各位大虾帮帮忙吧..我真的是没办法啦.

解决方案 »

  1.   

    我很纳闷的是这种问题为什么不用Timer控件来解决?哪位大牛来解答下?
      

  2.   

    public static void start()
        {
            //一小时线程
            int AutoActReportNetTime = 60* 60000;
            gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, AutoActReportNetTime, 0);
        }
    改成
      

  3.   

    public static void start()
      {
      //一小时线程
      int AutoActReportNetTime = 60* 60000;
      gtimer = new System.Threading.Timer(new System.Threading.TimerCallback(CallReportNet), null, AutoActReportNetTime, AutoActReportNetTime);
      }
    改成
      

  4.   

                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;
            }
        }注释这两句试试
      

  5.   

    呵呵..谢谢..楼上的两位..我按你们的方法去调试了..同样的还是bky..  哎..
      

  6.   

        public static void start()
        {
            //一小时线程
            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()的调用去掉,要不占有没有释放,方法就不执行了
      

  7.   

    呵呵...谢谢各路大虾..这个原因已经找到了.因为写的这个东西一般都没人去碰他的..
       在应用程序池里面有个"空闲超时" 他里面默认的是20分钟..我设置的时间超过20分钟他就不写啦..
       我现在把"空闲超时"这个设置给去掉了.就可以啦.
    步骤如下:1.先查看你当前网站的应用程序池的名字 如:"ASP.NET V2.0"
              2.在应用程序池中找到对应的名字.右击"属性" "性能" "空闲超时" 把这个选项去掉就可以啦.呵呵..
      

  8.   

    弄个timer 每过1小时启动一个线程去写文件 怎么样?