DESIGNING .NET 4
WINDOWS APPLICATIONS:
PRESENTATION LAYER RESPONSIVENESS

- OFFLOADING OPERATIONS

FROM UI THREAD

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

for (int i = 1; i < 11; i++)

{

LongRunningProcess();

// Report percentage complete.

backgroundWorker1.ReportProgress(i * 10);

}

}

private void backgroundWorker1_ProgressChanged(object sender,

ProgressChangedEventArgs e)

{

progressBar1.Value = e.ProgressPercentage;

}

// Starting thread without having to pass parameter to method.

System.Threading.Thread thread1 = new System.Threading.Thread(ParameterlessMethod);

thread1.Start();

// Starting thread passing in parameter object.

System.Threading.Thread thread2 = new System.Threading.Thread(MethodWithParameter);

thread2.Start(parameterObject);

lock (anObject)

{

// Perform some task.

}

System.Collections.ArrayList arrayList =

new System.Collections.ArrayList();

lock (arrayList.SyncRoot)

{

// Perform some task on the ArrayList.

}

public class LockableClass

{

public object SynchronizationObject = new object();

// Additional functionality here.

}