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

本文关键字:文件 处理 消息 捕获异常 在哪里 验证 不确定 | 更新日期: 2023-09-27 18:08:34

使用c#构建了一个Windows服务来处理传入服务器的。tif文件。该服务工作正常,但会抛出异常,然后继续按需要处理文件。问题是我得到这个错误:

先。IOException:进程不能访问该文件,因为它正在被另一个进程使用。在System.IO._ 错误。WinIOError() at System.IO._Error.WinIOError() at System.IO. file .移动(字符串sourceFileName,字符串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();
    }
}

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

在处理过程中停止Timer,完成后重新启动。

还可以看看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();
      }