background preloader

C#

Facebook Twitter

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"); }} The main thread creates a new thread t on which it runs a method that repeatedly prints the character “y”.

Once started, a thread’s IsAlive property returns true, until the point where the thread ends. Done static void Go(){ if (! Done Done (usually!) Join and Sleep. Low-Lock Techniques. Memory Models Understand the Impact of Low-Lock Techniques in Multithreaded Apps Vance Morrison I n my recent article on writing multithreaded applications (see " Concurrency: What Every Dev Must Know About Multithreaded Apps "), I outlined the basic principles of multithreaded programs, explained why most multithreaded programs are full of concurrency bugs (races), and illustrated a locking protocol that can keep such bugs out of your programs.

Low-Lock Techniques

Unfortunately, the locks introduced by this protocol have overhead that can sometimes be an issue, which raises the question of whether it is possible to avoid the overhead of methodically locking and still write correct, multithreaded programs? In some cases you can avoid the locking overhead; however, an improvement in performance comes with two significant software engineering costs. Principles of Safe Multithreaded Access All memory that a thread accesses should be frozen before use. There is a spectrum of possible design choices. C# tutorial. String Format for Double. Formatting Types. [This topic is pre-release documentation and is subject to change in future releases.

Formatting Types

Blank topics are included as placeholders.] The .NET Framework provides a consistent, flexible, and comprehensive means for you to represent any of the numeric, enumeration, and date and time base data types as a string. Formatting is controlled by strings of format specifier characters that indicate how a base type value is to be represented. For example, format specifiers indicate whether a formatted number should be represented in scientific notation, or whether a formatted date should present the month as a number or a name. The .NET Framework also uses cultural settings to represent a base type in a form appropriate to a particular culture.

The .NET Framework allows you to define custom formatting schemes and custom cultural settings. Describes standard and custom format strings used to create string representations of numeric types. Describes how to embed one or more formatted values in a string. Left Join in Linq and using the variable for other joins. Modèle mémoire - théorie / pratique.

Il s'agit du premier volet d'une série qui en comprend deux racontant la longue histoire du modèle de mémoire C#.

Modèle mémoire - théorie / pratique

Le premier volet explique les garanties que le modèle de mémoire C# effectue et présente les modèles de code qui motivent les garanties. Le second volet détaillera de quelle manière les garanties sont obtenues sur différentes architectures matérielles dans Microsoft .NET Framework 4.5. Une source de complexité concernant la programmation multithread repose sur le fait que le compilateur et le matériel peuvent transformer légèrement les opérations mémoire d'un programme de telle manière que le comportement monothread ne soit pas affecté mais que le comportement multithread puisse l'être.

Examinons la méthode suivante : void Init() { _data = 42; _initialized = true;} void Init() { _initialized = true; _data = 42;} Plusieurs optimisations à la fois dans les compilateurs et dans les processeurs peuvent entraîner ce type de réorganisation, comme je l'expliquerai dans le volet 2. Rounding Decimal Values in C# Rounding Values C# provides three native numeric data types for holding floating point values.

Rounding Decimal Values in C#

These are the single-precision, double-precision and decimal numbers, which are declared using the "float", "double" and "decimal" keywords respectively. Each of these types holds a different range of possible values with varying levels of precision. The "float" type is the least precise with the smallest range of values and the "decimal" type has the largest range and greatest precision. The selection of the type of value to use for any purpose is a trade-off between the size and accuracy requirements of the numbers being held and the amount of memory used by a declared variable. In this article we will be working with the decimal data type, not for its size or precision but for the additional functionality provided by this type over the other two options. NB: The methods described in this article require the .NET framework version 2.0 or later.

Truncation Floor and Ceiling Controlled Rounding.