-2

I have a file, to which data is saved a all the time.. I have a timer which runs once every 4 second My process looks like: File x.txt is open all the time. after 4 sec I want to stop the process and send this file by email. I want to repeat above steps all the time.

my code:

var timer = new System.Timers.Timer(4000);
        timer.Enabled = true;
        timer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
        {
            wrtiteToFile.Suspend();

            SendEmail();
            wrtiteToFile.Resume();

        };

  static void SendEmail()
    {
        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential =
            new NetworkCredential("", "");
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress("");
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.Port = ;
        smtpClient.Host = "";
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;
        smtpClient.EnableSsl = true;


        message.From = fromAddress;
        message.Subject = "";
        //Set IsBodyHtml to true means you can send HTML email.
        message.IsBodyHtml = true;
        message.Body = "";
        message.To.Add("");
        message.Attachments.Add(new Attachment(Application.StartupPath + @"\log.txt"));


        smtpClient.Send(message);
    }

The process cannot access the file 'C:\log.txt' because it is being used by another process. It's not working. What is the best manner in which I can do this? I tried lock but it doesn't work too. I must use .net framework 2.0

Tom
  • 51
  • 6

2 Answers2

2

You aren't disposing your resources. This way it is unsure when the resources (file locks) are released.

You should fix it with proper disposing of the resources. (Disclaimer, I haven't checked this)

static void SendEmail()
{
    using (SmtpClient smtpClient = new SmtpClient())
    using (MailMessage message = new MailMessage())
    using (Attachment attachment = new Attachment(Application.StartupPath + @"\log.txt"))
    {
        NetworkCredential basicCredential =
            new NetworkCredential("", "");

        MailAddress fromAddress = new MailAddress("");
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.Port = "";
        smtpClient.Host = "";
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;
        smtpClient.EnableSsl = true;

        message.From = fromAddress;
        message.Subject = "";
        //Set IsBodyHtml to true means you can send HTML email.
        message.IsBodyHtml = true;
        message.Body = "";
        message.To.Add("");
        message.Attachments.Add(attachment);


        smtpClient.Send(message);
    }
}

The using statement will implicit call the Dispose method of the disposables, and hence, releasing file handles and other kind of connections.

Stefan
  • 14,240
  • 9
  • 51
  • 69
0

Your SendMail method block file. Simplest way fix :)

var timer = new System.Timers.Timer(4000);
    timer.Enabled = true;
    timer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
    {
        timer.Enabled = false;
        wrtiteToFile.Suspend();
        SendEmail();
        wrtiteToFile.Resume();
        timer.Enabled = true;

    };
user1576474
  • 504
  • 1
  • 5
  • 10