Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I have a console application from where I have to send mails and have to retrieve statistics about mail from third party at every 5 minutes. As I have to send more number of mails and it may take 4-5 hours, i can not wait to get it over and than start getting statistics. So I have to apply threading for sending mail and for retrieving statistics every 5 minutes..
I have made a class file for it

How can I use timer here and set it to trigger after every 5minutes
Posted
Updated 17-Mar-14 21:02pm
v2

See MSDN Threading Tutorial[^]

You should create two threads, one for statistics and one for sending emails.
In your code you should have:

public class SomeForm : Form
{
private Thread _sendEmailsThread;
private Thread _getStatisiticsThread;


protected void SomeUIEventMethod(...)
{

if(_sendEmailThread == null || _sendEmailThread.IsAlive == false)
{
_sendEmailsThread= new Thread(new ThreadStart(SendEmails));
_sendEmailThread.Start();
}

if(_getStatisiticsThread== null || _getStatisiticsThread.IsAlive == false)
{
_getStatisiticsThread= new Thread(new ThreadStart(GetStatisitics));
_getStatisiticsThread.Start();
}

}

private void SendEmails()
{
//... your code for sending emails
}


private void GetStatisitics()
{
//... your code for getting statistics
}

protected void SomeForm_FormClosing(...)
{
if (_sendEmailThread!= null && _sendEmailThread.IsAlive)
{
        _sendEmailThread.Abort();
}
if (_getStatisiticsThread!= null && _getStatisiticsThread.IsAlive)
{
        _getStatisiticsThread.Abort();

}
//...

}


}
 
Share this answer
 
v2
Comments
Ami_Modi 14-Mar-14 3:12am    
Hi Raul. Thanks for reply. Here I want to know that why do we have to check _sendEmailThread == null?
Raul Iloc 14-Mar-14 3:15am    
This condition test if is the first time the UI event is used.
Supposing that you will start the thread on button click (could be also other UI event) then you should test if the thread was already started or not!
Ami_Modi 14-Mar-14 3:18am    
Okay.. Thank you
Raul Iloc 14-Mar-14 3:43am    
It will be nice from your side to "Accept" my solution if it helps you!
Ami_Modi 14-Mar-14 4:52am    
I am applying it.. If it works correctly will definitely do it.
You can use background worker component provide by .net framework for creating threads and thus you can create a class which sends out emails in a diffrent thread while you continue with your execution.

An example of sort,

<br />
Public Class Email<br />
    <br />
    Private EmailWorker As New ComponentModel.BackgroundWorker<br />
    <br />
    Public Sub SendEmailsAsync()        <br />
        EmailWorker.WorkerReportsProgress = True<br />
        AddHandler EmailWorker.DoWork, AddressOf EmailWorker_DoWork<br />
        AddHandler EmailWorker.ProgressChanged, AddressOf EmailWorker_ProgressChanged<br />
        AddHandler EmailWorker.RunWorkerCompleted, AddressOf EmailWorker_Completed<br />
        EmailWorker.RunWorkerAsync()<br />
    End Sub<br />
<br />
 Private Sub EmailWorker_Completed(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs)<br />
        ' Anything you wish to do after all the emails are sent <br />
    End Sub<br />
<br />
    Private Sub EmailWorker_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs)<br />
        ' Progress i.e if you wish to have any logging after each email or somthing similar <br />
    End Sub<br />
<br />
    Private Sub EmailWorker_DoWork(ByVal sender As Object, ByVal e As EventArgs)<br />
      ' Write Your Code to Send Emails <br />
              EmailWorker.ReportProgress(1,string message )<br />
    End Sub<br />
<br />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900