创建一个在后台运行的Windows服务程序,该程序启动时,创建并启功一个线程。该线程启动后,产生
一个定时器,定时器会每隔10秒定时对数据库中的库存表进行扫描,如果某物料或商品的当前数量小于
或等于安全库存量,则将该物料的相关报警信息写入日志文件中。我发现无论如何改变库存表中的信息,login.txt日志文件没有任何记录,为什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Timers;
using System.Data.SqlClient;
 
using System.IO;
namespace experiment8_1
{
   
    public partial class Service1 : ServiceBase
    {
        private System.Timers.Timer timer;
        private Thread scanThread;
        private SqlConnection conn;
        private SqlCommand comm;
        private FileStream fs;
        public Service1()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            scanThread = new Thread(new ThreadStart(ScanMaterial));
            scanThread.Start();
        }
        private void ScanMaterial()
        {
            if (timer == null)
            {
                timer = new System.Timers.Timer();
                timer.Interval = 10000;
                timer.Enabled = true;
                timer.Elapsed += new ElapsedEventHandler(OnTimer);            }
            else
            {
                timer.Start();
            }
        
        
        }
        private void OnTimer(Object source, ElapsedEventArgs e)
        {
            string strConn = "Server=.;Database=studentManagent;Integrated Security=sspi;";
            conn = new SqlConnection(strConn);
            string sql = "select safeQuan,onlineQuan,materialName from material";
            comm = new SqlCommand(sql, conn);
            try
            {
                fs = new FileStream(@"c:\liu.txt", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.GetInt32(1) <= dr.GetInt32(0))
                    {
                        sw.BaseStream.Seek(0, SeekOrigin.End);
                        sw.WriteLine(string.Format("警告提示:日期:{0}", DateTime.Now.ToString()));
                        sw.WriteLine(string.Format("请及时补充{0},安全库存是{1},在线数量是{2}。", dr[2].ToString(), dr[0].ToString(), dr[1].ToString()));
                    }
                }
                sw.Flush();
                sw.Close();            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally 
            {
                conn.Close();
                fs.Close();
            }
        
        }
        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            if (timer != null)
                timer.Close();
            if(scanThread!=null)
                scanThread.Abort();
        }
    }
}

解决方案 »

  1.   


    只是数据库表的图片,里面有一些记录,希望能显示出来
    这里的login.txt就是liu.txt
    服务已安装,注册了
      

  2.   

    再次看了看,这里确实是java/java se发错区了吧?同志,出门右拐
      

  3.   


      if (timer == null)
                {
                    timer = new System.Timers.Timer();
                    timer.Interval = 10000;
                    timer.Enabled = true;
                    timer.Elapsed += new ElapsedEventHandler(OnTimer);
                  //如果为null的时候,这边怎么不再进行  timer.Start();   ???
                }
                else
                {
                    timer.Start();
                }
       catch (Exception ex)
                {
                    throw ex;
                  //强烈建议这边也写日志到 txt文件里面去,windows服务你是没那么多精力去看服务是否被停止            }
    //另外建议 这类作业程序使用第三方组件,更稳定更准确
    //Quartz.net 自己搜索下
      

  4.   

                    while (dr.Read())
                    {
                        if (dr.GetInt32(1) <= dr.GetInt32(0))
    //上面这一句,不是应该改为第三列和第四列比较吗?
      

  5.   

    select safeQuan,onlineQuan,materialName from material
    查询的 safeQuan,onlineQuan是一,二列
    五楼的我不怎么懂
      

  6.   

    以后Windows不建议用Timer,这个不靠谱,你的线程已经执行完毕了,怎么可能还能计时呢?
    我给你改改吧。
    记得以后要加dr.Close();using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading;
    using System.Timers;
    using System.Data.SqlClient;using System.IO;
    namespace experiment8_1
    {    public partial class Service1 : ServiceBase, IDisposable
        {
            private Thread scanThread;
            private SqlConnection conn;
            private SqlCommand comm;
            private FileStream fs;
            private bool m_Running = false;        public Service1()
            {
                InitializeComponent();
            }        protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。
                scanThread = new Thread(new ThreadStart(ScanMaterial));
                scanThread.IsBackground = true;
                m_Running = true;
                scanThread.Start();
            }
            private void ScanMaterial()
            {
                while (m_Running)
                {
                    try
                    {
                        string strConn = "Server=.;Database=studentManagent;Integrated Security=sspi;";
                        conn = new SqlConnection(strConn);
                        string sql = "select safeQuan,onlineQuan,materialName from material";
                        comm = new SqlCommand(sql, conn);
                        try
                        {
                            fs = new FileStream(@"c:\liu.txt", FileMode.Create, FileAccess.Write);
                            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                            conn.Open();
                            SqlDataReader dr = comm.ExecuteReader();
                            while (dr.Read())
                            {
                                if (dr.GetInt32(1) <= dr.GetInt32(0))
                                {
                                    sw.BaseStream.Seek(0, SeekOrigin.End);
                                    sw.WriteLine(string.Format("警告提示:日期:{0}", DateTime.Now.ToString()));
                                    sw.WriteLine(string.Format("请及时补充{0},安全库存是{1},在线数量是{2}。", dr[2].ToString(), dr[0].ToString(), dr[1].ToString()));
                                }
                            }
                            dr.Close();
                            sw.Flush();
                            sw.Close();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            conn.Close();
                            fs.Close();
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        if (!m_Running)
                        {
                            break;
                        }
                        Thread.Sleep(60 * 1000);
                    }
                }
            }        protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                m_Running = false;
                scanThread.Join();
            }
        }
    }
      

  7.   

    是Windows Service不建议用Timer,少打了Service,呵呵
      

  8.   

    按加林仙人的代码
    catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            conn.Close();
                            fs.Close();
                        }错误 1 控制不能离开 finally 子句主体 F:\作业\WinForm\experiment8-1\experiment8-1\Service1.cs 83 25 experiment8-1
    自己改了一下,还是没有出来。
      

  9.   

    finally
                    {
                        if (!m_Running)
                        {
                            break;
                        }
                        Thread.Sleep(60 * 1000);
                    }