首页 文章

不知道在哪里捕获异常,验证和消息'file is still processing'

提问于
浏览
2

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

System.IO.IOException:进程无法访问该文件,因为该文件正由另一个进程使用 . System.IO.File.Move上的System.IO._Error.WinIOError()处的System.IO._Error.WinIOError(Int32 errorCode,String maybeFullPath)(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

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

    另请参阅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();
          }
    

相关问题