I have a for loop which loops through and sends an email each loop. Right now im using thread.sleep() but I want the user to still be able to interact with the program, just del
You can spin off a new thread to send the emails so you don't lock the UI thread.
Try shifting the code to background Thread.
You need to put the method of sending mail in separated thread to keep your GUI interactive .
Thread newThread = new Thread(new ThreadStart(SendMail));
newThread.Start();
public void SendMail()
{
//Send mails here
}
Are you running the loop on the UI thread? If so, just use Task.Factory.StartNew to run your loop in a different thread. If you need to delay the email sending at that time, put a Thread.Sleep before you actually begin looping.
It will look something like this:
private void OnButtonClick(object sender, EventArgs e)
{
//This code happens on the UI thread
Task.Factory.StartNew(SendEmails);
}
private void SendEmails()
{
Thread.Sleep(500);
foreach(var email in emailAddresses) {
SendEmail(email);
}
}