不确定在哪里捕获异常,验证和消息'文件仍处理'

时间:2013-04-10 15:41:44

标签: c# .net exception-handling windows-services

使用C#构建Windows服务来处理进入服务器的.tif文件。该服务工作正常但抛出异常然后继续根据需要处理该文件。问题是我收到了这个错误:

  

System.IO.IOException:进程无法访问该文件,因为该文件正由另一个进程使用。在System.IO ._ Error.WinIOError(Int32 errorCode,String maybeFullPath)at System.IO。 _Error.WinIOError()at System.IO.File.Move(String sourceFileName,String destFileName)

我相信我需要捕获异常,验证错误,并显示消息“文件仍在处理”但是,我不知道在哪里?我的timer1设置为10秒,这应该足够的时间来处理文件。

我的代码:

protected void timer1Elapsed(object sender, EventArgs e)
    {
        try
        {
            if (mainProg == null)
            {
                mainProg = new ProcessFilesFromSource(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            }
            mainProg.ScanArchiverMain();          
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("ScanArchiver Error", ex.ToString());
            EventLog.WriteEntry("ScanArchiver Online");
        }            
      }
    protected override void OnStop()
    {
        EventLog.WriteEntry("ScanArchiver Offline");
        timer1.Enabled = false;
        timer1.Dispose();
    }
}

1 个答案:

答案 0 :(得分:0)

处理时停止计时器,完成后再次启动计时器。

另请参阅BackgroundWorker作为Timer的替代方案。

protected void timer1Elapsed(object sender, EventArgs e)
    {
        timer1.Stop();
        try
        {

            if (mainProg == null)
            {
                mainProg = new ProcessFilesFromSource(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            }
            mainProg.ScanArchiverMain();          
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("ScanArchiver Error", ex.ToString());
            EventLog.WriteEntry("ScanArchiver Online");
        }            
        timer1.Start();
      }