Posts

Showing posts with the label parallel programming

IIS Windows Authentication/Delegation issue with C# Parallel Tasks

When you use double-hop authentication (WebBrowser->IIS->SQL Server) code executed on the webserver inside Parallel.Invoke() or Task.Factory.StartNew() is no longer executed as authenticated user (domain\username) but is being changed to (domain\iisservername$). You can see it in Environment.UserName when debuging. So if you're executing any SQL queries as Tasks you might get permission denied errors. The way to fix it is to pass custom TaskScheduler from CurrentSynchronizationContext Parallel.Invoke( new ParallelOptions() { TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext() }, () => { /*do something here;*/ }, ); Task.Factory.StartNew( () => { /*do something here;*/ }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext() ); This is a good article about SynchronizationContext It's All About the SynchronizationContext

Exception handling in multithreaded C#

try { Parallel.Invoke( () => { Console.WriteLine(" Starting Job 1.. "); Thread.Sleep(3*1000); Console.WriteLine(" Starting Job 1.. SUCCESS "); }, () => { Console.WriteLine(" Starting Job 2.. "); throw new Exception(" Job 2 Failed "); } ); } catch (AggregateException ex) { Console.WriteLine(ex); foreach (var innerEx in ex.InnerExceptions) { Console.WriteLine(innerEx); } }

C# Enumerable Split before running Parallel code

[Test] public void SplitWithParallel() { var list = new List< int >( new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); list.Split(4).AsParallel().ForAll(x => x.ForEach(y => Console.WriteLine(y) /*or run a query*/ )); } public static class Extensions { public static IEnumerable<IEnumerable<T>> Split<T>( this IEnumerable<T> list, int parts) { int i = 0; var splits = from item in list group item by i++%parts into part select part.AsEnumerable(); return splits; } public static void ForEach<T>( this IEnumerable<T> source, Action<T> action) { foreach (var item in source) { action(item); } } }

Using Task and WebRequest to asynchronously call RESTful service

Threading in C# is a good website describing Task Parallelism and other aspects of C# Threading. [Test] //For GET/POST/PUT/DELETE calls public void TestWebRequest() { Task< string > task = Task.Factory.StartNew< string >(() => // Begin task { var req = (HttpWebRequest)WebRequest.Create(" http://localhost:52292/Api/Product/31a3653e-2db0-429e-8cfb-8842bd984b69 "); req.Method = " GET "; //POST/PUT/DELETE using (var response = (HttpWebResponse)req.GetResponse()) { if (response.ContentLength > 0) { var reader = new StreamReader(response.GetResponseStream()); return reader.ReadToEnd(); } } return null ; }); Console.WriteLine(" Do something else.. "); //wait for Task Result string result = task.Result; Console.WriteLine(" Completed.. "); Console.WriteLine(result); } [Test] /...

.NET 4.0 Parallel programming (PFX)

Great overview of multithreading APIs in Framework 4.0 for leveraging multicore processors known as PFX (Parallel Framework)