Skip to content

Instantly share code, notes, and snippets.

@iraSenthil
iraSenthil / gist:930177
Created April 20, 2011 01:52
Share data between threads via instance method
class ShareDataInstanceVariable
{
string instanceString = "*";
public void Print(string threadMethodParam)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine(string.Format("ThreadID: {0} {1} {2}",
Thread.CurrentThread.ManagedThreadId, threadMethodParam, instanceString));
@iraSenthil
iraSenthil / gist:930189
Created April 20, 2011 01:58
Share data between threads via static field
static string ClassString = "*";
static void Main(string[] args)
{
Program program = new Program();
//Another thread
Thread newThread = new Thread(() => program.Print("+"));
newThread.Start();
//Main thread
@iraSenthil
iraSenthil / gist:930288
Created April 20, 2011 03:41
Thread Safety
static List<int> sourceList = new List<int>() { 1,2,3,4,5};
static List<int> destinationList = new List<int>();
static void Main(string[] args)
{
//Create new threads
Thread newThread1 = new Thread(Move);
Thread newThread2 = new Thread(Move);
//Start thread
@iraSenthil
iraSenthil / gist:930328
Created April 20, 2011 04:14
Different ways to create and run thread
//Method with no parameter - ThreadStart Delegate
Thread t = new Thread (new ThreadStart (TestMethod));
t.Start();
void TestMethod() {}
//Method with a parameter - ParameterizedThreadStart Delegate
Thread t = new Thread (new ThreadStart (TestMethod));
t.Start(5);
t.Start("test");
void TestMethod(Object o) {}
@iraSenthil
iraSenthil / gist:930351
Created April 20, 2011 04:27
Exception handling in Threading
//Bad
public static void Main()
{
try
{
new Thread (Go).Start();
}
catch (Exception ex)
{
// We'll never get here!
@iraSenthil
iraSenthil / gist:931551
Created April 20, 2011 14:55
Nested locks. Nested locking is useful when one method calls another within a lock:
//Nested locking is useful when one method calls another within a lock:
static readonly object _locker = new object();
static void Main()
{
lock (_locker)
{
AnotherMethod();
// We still have the lock - because locks are reentrant.
@iraSenthil
iraSenthil / gist:931560
Created April 20, 2011 14:59
deadlock : A deadlock happens when two threads each wait for a resource held by the other, so neither can proceed.
//A deadlock happens when two threads each wait for a resource held by the other, so neither can proceed.
object locker1 = new object();
object locker2 = new object();
new Thread (() => {
lock (locker1)
{
Thread.Sleep (1000);
lock (locker2); // Deadlock
@iraSenthil
iraSenthil / gist:931586
Created April 20, 2011 15:09
Updating UI controls from background thread/other than UI thread
//WinForms Control.Invoke
public Object Invoke(
Delegate method
)
//WPF Dispatcher.Invoke
public Object Invoke(
DispatcherPriority priority,
TimeSpan timeout,
Delegate method,
@iraSenthil
iraSenthil / gist:932230
Created April 20, 2011 18:28
IEnumerable is not thread safe.
//Modifying IEnumerable while another thread enumerates it, will throw exception
//Every thread entry method should handle exception, if not will kill entire app.
static List<int> sourceList = new List<int>() { 1, 2, 3, 4, 5 };
static void Main(string[] args)
{
//One thread enumerates thread while another thread modifies the collection
new Thread(Iterate).Start();
new Thread(Add).Start();
Console.ReadKey();
@iraSenthil
iraSenthil / gist:932311
Created April 20, 2011 18:52
.Net collections are thread-safe for concurrent thread reads
//Concurrent reading is thread safe
static List<int> sourceList = new List<int>() { 1, 2, 3, 4, 5 };
static void Main(string[] args)
{
new Thread(Iterate).Start();
new Thread(Iterate).Start();
Console.ReadKey();
}