2

C# を使用して、サーバーに着信する .tif ファイルを処理する Windows サービスを構築しました。サービスは正常に動作しますが、例外がスローされ、必要に応じてファイルの処理が続行されます。問題は、このエラーが発生していることです:

System.IO.IOException: 別のプロセスによって使用されているため、プロセスはファイルにアクセスできません。System.IO._ Error.WinIOError (Int32 errorCode、文字列の多分フルパス) で System.IO. System.IO.File.Move (文字列 sourceFileName、文字列 destFileName) での _Error.WinIOError()

例外をキャッチし、エラーを検証し、「ファイルはまだ処理中です」というメッセージを表示する必要があると思います。私の 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();
    }
}
4

1 に答える 1

0

処理中にタイマーを停止し、完了したら再び開始します。

Timer の代替としてBackgroundWorkerも見てください。

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();
      }
于 2013-04-10T20:01:22.417 に答える