background preloader


Facebook Twitter


Multi-threading in .NET: Introduction and suggestions. One of the greatest understatements I've heard in a newsgroup was made by Patricia Shanahan, in a Java newsgroup in 2001: "Multi-threaded programming needs a little care.

Multi-threading in .NET: Introduction and suggestions

" Multi-threading is probably one of the worst understood aspects of programming, and these days almost all application programmers need to understand it to some extent. This article acts as an introduction to multi-threading and gives some hints and tips for how to do it safely. Warning: I'm not an expert on the subject, and when the real experts start discussing it in detail, my head starts to spin somewhat.

However, I've tried to pay attention to those who know what they're doing, and hopefully the contents of this article form at least part of a multi-threading "best practice". This article uses the C# type shorthands throughout - int for Int32 etc. So, what is a thread? .NET has been designed from the start to support multi-threaded operation. Next page: Passing Parameters to Threads Back to the main C# page. Deadlock Detection in Existing Code. Free source code and progra. Introduction Deadlocks are common problems in multi-threaded programming.

Deadlock Detection in Existing Code. Free source code and progra

When it comes to multithreading development, the most common problem developers are facing is critical sections. It is not uncommon to use more than a single lock, but if one does not pay attention to the order of the locks, or to the context in which they are being called (e.g., from within a callback), deadlocks will form. (There are many reasons for deadlocks to occur other than the obvious critical section, e.g., two threads that are waiting for each other to signal an event, but we will not discuss them here). As with anything that is related to threads, timing is everything. What if we could make the rare case the normal case? The attached ZIP file contains a DLL that does exactly that. Note that there is no need for the deadlock to really occur; rather, it is only important that suspected flows (or all flows) will be performed at least once.

Using the code. Thread synchronization: Wait and Pulse demystified. Free source. Table of Contents Introduction This short article is about the three less well understood methods of the Monitor class: Wait, Pulse, and PulseAll.

Thread synchronization: Wait and Pulse demystified. Free source

The MSDN documentation for these methods explains what they do, but not why or how to use them. I will try to fill this gap and demystify them. These methods provide low-level synchronization between threads. Basics public partial static class Monitor { public static bool Wait( object o ) { ... } public static void Pulse( object o ) { ... } public static void PulseAll( object o ) { ... } } At the most basic level, a thread calls Wait when it wants to enter a wait state, and another thread calls Pulse when it wants to wake up the waiting thread.

Concurrent Affairs: More AsyncEnumerator Features. 5pm Posts: .Net Threads - 10 Best Practices. Quantum Bit Designs » Blog Archive » Stop Polluting the UI Threa. A basic NamedLock class. Free source code and programming help. Download source code - 1.08 KB Introduction There are a few problems I've come across where synchronizing a particular "name" might be useful.

A basic NamedLock class. Free source code and programming help

One of the apps I work on makes heavy use of Lucene.NET; each page shows the results of a couple of queries. It doesn't make a whole ton of sense to run multiple, identical queries against Lucene at the same time, so I generate a key for each query, lock against that key, and let the first thread do the actual work while the others sit around sipping coffee. Using the code It's pretty simple to use this class. NamedLock<string> locker = new NamedLock<string>(); var url = " using (locker.Lock(url)) { var xml = new XmlDocument(); xml.Load(url); } NParallel, A Small Parallel Execution Library - The Code Project.

Important Updates NParallel0.2 is released with loop parallelism and multi-task support (For, ForEach, Do), together with a lot of other enhancements(parameterized async call for instance). The codes are almost rewriten and heavily commented for the intensional users. VS2005 version will no longer be supported because later versions will rely highly on functional programming concept with C#3.0 lumbda. This article mainly remains to introduce the main construct of NParallel, for loop parallelism , I posted a new article discuss the design and usage of loop parallelism for NParallel, you could refer to the testcases attached. Bounded Blocking Queue (One Lock) - The Code Project - C# Progra. Download source files - 5.99 Kb.

Bounded Blocking Queue (One Lock) - The Code Project - C# Progra

Threading in C# - Free E-book. Threading in C# Joseph Albahari Last updated: 2011-4-27 Translations: Chinese | Czech | Persian | Russian | Japanese Download PDF Part 1: Getting Started C# supports parallel execution of code through multithreading.

Threading in C# - Free E-book

A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. All examples assume the following namespaces are imported: using System; using System.Threading; class ThreadTest{ static void Main() { Thread t = new Thread (WriteY); t.Start(); for (int i = 0; i < 1000; i++) Console.Write ("x"); } static void WriteY() { for (int i = 0; i < 1000; i++) Console.Write ("y"); }}