Skip to main content

Posts

Showing posts with the label Mutex

Asynchronous Programming Model(APM) with .Net

Namespace: System.Threading Asynchronous programming is simply allowing some portion of code to be executed on separate threads. Many .Net Framework classes support APM with methods like BeginXXX and EndXXX . FileStream FS = new Filestream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.Asynchronous); IAsyncResult vResult = FS.BeginRead(buffer, 0 , Buffer.length, null, null); //Do your work int NoOfBytes = FS.EndRead(vResult); FS.Close(); This type of APM is 1. Rendezvous Wait-Until-Done model . 2. Rendezvous Polling Model : Keeping doing some other work along with checking whether the APM work is completed or not. IAsyncResult vResult = FS.BeginRead(buffer, 0 , Buffer.length, null, null); While ( ! vResult.IsCompleted ) { //Do your work } int NoOfBytes = FS.EndRead(vResult); FS.Close(); 3. Callback Model :               ...

Data Sharing Across Threads in .Net

Namespace: System.Threading Avoiding Collisions :                 Collision may occur while accessing data from multiple threads. Two threads loaded the same value, updated and set the value at the same time, and leaving the data in inconsistent state. Interlocked class can perform the arithmetic operations as single a single operation. These arithmetic operations include: Add, Decrement, Exchange, Increment, and Read operations. Synchronization Locks : C# lock can be used to make single thread enter the specified code at one time. Like:                 lock (this Or any Object)                 {                 } It’s an object base lock. We will face a de...