1

タイマー本体でメールを送信する必要があります.C#アプリケーションでは、タイマー間隔は2秒です

try
{
    string[] filePaths = Directory.GetFiles(@"D:\ISS_Homewrok\");
    foreach (string filePath in filePaths)
    {
        SendEmail(filePath);
        File.Delete(filePath);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

この例外は、ファイルの削除時にスローされます

    System.IO.IOException: The process cannot access the file 'D:\ISS_Homewrok\KeyBoardMovements1.txt' because it is being used by another process.
 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Delete(String path)
   at ISS_Homework.Form1.timer_tick(Object sender, ElapsedEventArgs e)

SendEmail メソッドは次のとおりです。

private void SendEmail(string p)
    {
        SmtpClient smtp;
        //Detailed Method
        MailAddress mailfrom = new MailAddress("samahnizam@gmail.com");
        MailAddress mailto = new MailAddress("rubaabuoturab@gmail.com");
        MailMessage newmsg = new MailMessage(mailfrom, mailto);
        newmsg.Subject = "Tracker";
        //For File Attachment, more file can also be attached
        try
        {
            Attachment att = new Attachment(p);
            newmsg.Attachments.Add(att);
            smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("XXXXX", "XXXXX");
            smtp.EnableSsl = true;
            smtp.Send(newmsg);
        }
        catch (Exception ex)
        {
        }
    }

編集: タイマーの間隔を1分にしましたが、例外はまだスローされています! 助けてください。

4

3 に答える 3

0

タイマー機能を試すことができます

timer.Stop();
try
{
    string[] filePaths = Directory.GetFiles(@"D:\ISS_Homewrok\");
    foreach (string filePath in filePaths)
    {
        SendEmail(filePath);
        File.Delete(filePath);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}
timer.Start();
于 2012-11-05T14:41:36.360 に答える
0

What kind of timer are you using? There are four different types of Timer in the .Net framework, and some of them spawn new threads to handle the Elapsed/Tick events.

In this case, I suspect that it is taking longer to send all of the emails than the interval between timer ticks. If you are using one of the threading timers, then your file is being read by multiple timer threads at the same time. See the documentation at MSDN:

The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread, so the event-handling method might run on one thread at the same time that a call to the Stop method runs on another thread. This might result in the Elapsed event being raised after the Stop method is called. The code example in the next section shows one way to work around this race condition.

What I would do is set a flag at the beginning of your routine to indicate that the process is currently running and to exit if that is the case:

if (_isRunning)
    return;

try
{
    _isRunning = true;
    string[] filePaths = Directory.GetFiles(@"D:\ISS_Homewrok\");
    foreach (string filePath in filePaths)
    {
        SendEmail(filePath);
        File.Delete(filePath);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}
finally
{  _isRunning = false; }
于 2012-11-05T14:48:00.097 に答える
0

古いリクエストの処理中に新しいメールを送信しようとしていると思います。両方のプロセスがファイルにアクセスしようとすると、クラッシュします。解決策: 関数を backgroundworker.dowork プロシージャでラップします。タイマーが起動するたびに、backgroundworker.isbusy メソッドをチェックして、古いプロセスが完了したかどうかを確認できます。そうでない場合は、さらに 2 秒待ちます。

Vb.netでプログラミングしているため、コードはありません

于 2012-11-05T14:47:35.337 に答える