Monthly Archives: November 2014

ASP.NET C# Send email in background thread

System.Threading.Thread thread = new System.Threading.Thread(delegate()

thread.IsBackground = true;

private static void SMTPSend(MailMessage message)
            SmtpClient smtp = new SmtpClient();

On the presentation layer you do not need any AJAX or JavaScript calling code from the backend
Just plain ASP.NET button

<asp:Button ID="btnSend" runat="server" Text="Send Email" onclick="btnSend_Click" />

Code behind

protected void btnSend_Click(object sender, EventArgs e)
    catch (Exception ex)
        litResult.Text = ex.ToString();

AWS EC2 – Get running instances only

AWS SDK has Console Sample project where you can see code populating number of your running Instances.

DescribeInstancesResponse ec2Response = ec2.DescribeInstances(request);
int numInstances = 0;
numInstances = ec2Response.Reservations.Count;
Console.WriteLine("You have " + numInstances + " Amazon EC2 instance(s) running.");

What you actually get is number of all your Instances, no matter if they are running or not.
If you want to get only running ones, query Reservations collection filtering by their State

DescribeInstancesRequest instanceRequest = new DescribeInstancesRequest();
List<Reservation> reservations = ec2.DescribeInstances(instanceRequest).Reservations;
List<Instance> runningInstances = reservations.Where(r => r.Instances.Any(i => i.State.Name == "running")).Select(r => r.Instances[0]).ToList();
numInstances = runningInstances.Count;