Interlocked

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		List<string> addresses = GetEmailAddresses();
		var skipCount = 0;
		const int takeCount = 1;
		TimeSpan period = TimeSpan.FromSeconds(1);

		var timer = new System.Threading.Timer(e =>
		{
    		foreach (string address in addresses.Skip(skipCount).Take(takeCount))
    		{
        		SendEmailAddress(address);
    		}
			Interlocked.Add(ref skipCount, takeCount);
		}, null, TimeSpan.Zero, period);

		while (addresses.Count > Interlocked.CompareExchange(ref skipCount, int.MinValue, int.MinValue)) {
			Thread.Sleep(1000);
		}

		timer.Dispose();
		Console.WriteLine("Fine");
	}
	
	private static List<string> GetEmailAddresses()
	{
		return new List<string> { "a", "b", "c" };
	}
	
	private static void SendEmailAddress(string email)
	{
	}
}
;