Windows服务异常并停止我的服务

时间:2013-07-04 06:44:51

标签: c# windows exception service

我的服务从com端口读取并将信息保存在DB中。它在一段时间内完好无损,但有时会出现错误窗口以获取异常,如果我没有点击'no'但是它没有从com端口读取。当我检查事件查看器时,我在服务上看到一些异常号7034,7031.I记录我的代码中的每个位置,我使用try,catch。我的日志中没有ant catch文件,所以我无法理解是什么问题?

public partial class Service1 : ServiceBase
{
    string _fileName = @"c:\logSensor\log.ini";
    internal delegate void StringDelegate(string data);
    ArrayList lines = new ArrayList();
    BL.EnterDatas eData = new BL.EnterDatas();
    private class Line
    {
        public string Str;

        public Line(string str)
        {
            Str = str;
        }
    }

    public Service1()
    {
        InitializeComponent();
CommPort com = CommPort.Instance;
        com.Open();
        com.StatusChanged += OnStatusChanged;
        com.DataReceived += OnDataReceived;
        timer1.Enabled = true;
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(_fileName, true))
        {
            writer.WriteLine(PublicVariable.DateShamsi() + "  " + PublicVariable.Nowtime() +
            " . " + "Step1:Load ");
            writer.Flush();
        }
    }

    protected override void OnStart(string[] args)
    {

    }

    protected override void OnStop()
    {
    }

    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            timer1.Stop();
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(_fileName, true))
            {
                writer.WriteLine(PublicVariable.DateShamsi() + "  " + PublicVariable.Nowtime() + "timer1_Elapsed:Stop Timer ");
                writer.Flush();
            }
            ReadLog();
            StreamReader sr = new StreamReader(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\path.txt");
            string _logPath = sr.ReadLine();
            FileStream fs = new FileStream(_logPath, FileMode.Open);
            fs.SetLength(0);
            fs.Close();
        }
        catch (Exception ex)
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(_fileName, true))
            {
                writer.WriteLine(PublicVariable.DateShamsi() + "  " + PublicVariable.Nowtime() + "Catch:timer1_Elapsed " + ex.Message);
                writer.Flush();
            }
        }

        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(_fileName, true))
        {
            writer.WriteLine(PublicVariable.DateShamsi() + "  " + PublicVariable.Nowtime() + "final:timer1_Elapsed ");
            writer.Flush();
        }

        timer1.Start();
    }

    #region Functions...

... }

0 个答案:

没有答案