Parallel and The C# Memory Model

Wednesday, March 6, 2013

Parallel and The C# Memory Model Parallel programming can be tricky, both compiler and CPU's optimization can lead into a twilight zone's debugging. lets take the following code snippet snippet: Code Snippet class Program {     static void Main(string args)     {         Console.WriteLine("Start");         var u = new Util();         u.Exec();         Console.ReadKey();     } }   public class Util {     private bool _stop = true;     public void Exec()     {         Task t = Task.Run(() =>             {                 bool b = true;                 while (_stop)                 {                     b = !b;                 }                 Console.WriteLine("Complete {0}", b);             });         Thread.Sleep(30);         _stop = false;     } } ...

Using async / await

Sunday, January 15, 2012

Using async / await this post will discuss parallel disposal. whenever we want to dispose a parallel execution upon completion we can't use the convenient using keyword. for example, the following code may be dispose the command before completion: Very bad Code Snippet using (var conn = new SqlConnection(CONN_STR)) using (var cmd = new SqlCommand("Select * from Employee", conn)) {     conn.Open();     cmd.BeginExecuteReader(ar =>         {             int affected = cmd.EndExecuteNonQuery(ar);         }); } the using is absolutely wrong for the above sample. what should be...

async \ await and Exception Handling

Thursday, January 12, 2012

async \ await and Exception Handling this post will discuss how async / await is handling exceptions. as we mention in previous post, about the async / await concept, await is all about continuation. before .NET 4.5 parallel execution exceptions has to be handle in separate of the synchronic handling. for example: handling ThreadPool execution: Code Snippet void Foo() {     try     {         Console.WriteLine("Synchronic");         ThreadPool.QueueUserWorkItem(state =>             {                 try                 {                     Console.WriteLine("Parallel");                 }                 catch (Exception exAsync)                 {                     EventLog.WriteEntry("application", exAsync.ToString());                 }             }, null);     }     catch (Exception ex)     {         EventLog.WriteEntry("application", ex.ToString());...

the concept of async \ await

Thursday, December 29, 2011

the concept of async \ await in this post I will survey the new .NET 4.5 / C# 5 concept of async / await. I will focus on how to understand what is really happens behind the new async / await syntax. What's it all about? the new async / await syntax is using the C# syntactic compiler to generate async operation from code that is looking very much like a synchronous code. but before we start we should discus the new C# 5  syntax. the syntax include 2 keywords: ...