Programming Tutorial On The Way

free online programming resource on dotnet, csharp, web related topics

For threads that are manually created via the Thread class, you can call the Join method to wait for a thread to finish. This works well when you need to wait for all threads to finish processing before an application terminates. Unfortunately, the thread pool threads do not have a Join method. You need to make sure that all threads in the thread pool have finished processing before another thread terminates or your application’s main thread terminates.

Use a combination of the ThreadPool methods — GetMaxThreads and GetAvailableThreads — to determine when the ThreadPool is finished processing the requests.
Read the rest of this entry »

Popularity: 34% [?]

Synchronizing the Reading and Writing of a Resource Efficiently

When you have a resource that is shared by multiple threads, you need to provide exclusive access to this resource when a thread is writing to it. However, you do not want the overhead of providing exclusive access to this resource when multiple threads are only reading from it. You want to allow one thread to access a shared resource only if it is writing to it, but you also want to allow multiple threads to read from this resource. While multiple threads can read from a resource, a write operation cannot occur while any thread is reading from this resource.

Example below uses the ReaderWriterLock class from the System.Threading. The ReaderWriterLock is optimized for scenarios where you have data that changes infrequently but needs protection for those times when it is updated in a multithreading scenario. To illustrate, the GradeBoard class represents a board where an instructor will post the grades students received from a class. Many students can read the grade board, but only the instructor can post a grade (write) to the grade board. Students will not, however, be able to read from the board while the instructor is updating it.
Read the rest of this entry »

Popularity: 29% [?]

The ThreadPool is a great way to perform background tasks without having to manage all aspects of the thread yourself. It can be handy to know when the ThreadPool itself is going to become a bottleneck to your application, and the GetAvailableThreads method can help you. However, you might want to check your application design if you are consistently using this many threads as you might be losing performance due to contention or context switching. Queuing up work when the ThreadPoolM is full simply queues it up for execution once one of the threads comes free; the request isn’t lost, just postponed.

In this example, your application will be creating many threads from the thread pool. When creating a thread from this pool, you want to be informed as to whether a thread in the pool is available or if none are available, and the request for a new thread will have to be queued. Basically, you want to know whether a thread is available for immediate use from the thread pool. Use the ThreadPool.GetAvailableThreads method to get the number of worker threads currently available in the ThreadPool to determine whether you should queue another request to launch another thread via ThreadPool.QueueUserWorkItem or take an alternate action. The Main method calls a method (SpawnManyThreads) to spawn lots of threads to do work in the ThreadPool, then waits for a bit to simulate processing.
Read the rest of this entry »

Popularity: 43% [?]

Configuring a Timer

When using a System.Threading.Timers.Timer class, here are a few scenarios you may encounter:

  • You want to use a timer to call a timer callback method at a fixed time after the timer object has been created. Once this callback method has been called the first time, you want to call this same callback method at a specified interval (this interval might be different from the time interval between the creation of the timer and the first time the timer callback method is called).
  • You want to use a timer to call a timer callback method immediately upon creation of the System.Threading.Timer object, after which the callback method is called at a specified interval.
  • You want to use a timer to call a timer callback method one time only.
  • You have been using a System.Threading.Timer object and need to change the intervals at which its timer callback method is called.

There are two types of Timer classes in .NET Framework, one from System.Threading and another one from System.Windows.Forms. The former timer is enough to serve the earlier scenarios, but if you are doing UI work and want to use timers, you should investigate the System.Windows.Forms.Timer class. If you are doing server work, you might also want to look at System.Threading.Timers.Timer as well. Both of these classes add events for when the timers are disposed and when the timer “ticks”; they also add properties that expose the settings. Here we look at the Timer class of System.Threading.Timers sample codes.
Read the rest of this entry »

Popularity: 26% [?]

If you have two threads currently running in your application and you need the main thread to wait until the worker thread has completed its processing, use the Thread.Join method to detect when a thread terminates. This ability comes in handy when your application is monitoring activities amongst multiple threads and you don’t want your main application thread to terminate until all of the workers are done processing.
Read the rest of this entry »

Popularity: 22% [?]

The asynchronous delegates in this article are created and invoked in the same fashion as the asynchronous delegate in previous articles (”Polling an Asynchronous Delegate” and “Timing Out an Asynchronous Delegate”). Instead of using the IsCompleted property to determine when the asynchronous delegate is finished processing (or the WaitOne method to block for a specified time while the asynchronous delegate continues processing), we use a callback to indicate to the calling thread that the asynchronous delegate has finished processing and that its return value, ref parameter values, and out parameter values are available. Since the asynchronous delegate will return a value, you must be able to pass this return value back to the invoking thread.

Invoking a delegate in this manner is much more flexible and efficient than simply polling the IsCompleted property to determine when a delegate finishes processing. When polling this property in a loop, the polling method cannot return and allow the application to continue processing. A callback is also better than using a WaitOne method, since the WaitOne method will block the calling thread and allow no processing to occur. You can break up the WaitOne method into a limited number of wait cycles, but this is simply a merging of the polling technique with the WaitOne operation.
Read the rest of this entry »

Popularity: 4% [?]

The WaitHandle.WaitOne method can indicate when an asynchronous operation times out. The code on the invoking thread needs to periodically wake up to do some work along with timing-out after a specific period of time. Use the approach shown in the following code, which will wake up every 20 milliseconds to do some processing. This method also times out after a specific number of wait/process cycles (note that this code will actually time out after more than two seconds of operation since work is being done between the wait cycles): Read the rest of this entry »

Popularity: 5% [?]

While an asynchronous delegate is executing, you need to continuously poll it to see whether it has completed. This ability is useful when you need to monitor the length of time it takes the asynchronous delegate to execute or if you need to monitor other objects in the system in parallel with this asynchronous delegate, possibly to determine which object finishes first, second, third, and so on. It can also be useful when performing a continuous task, such as displaying an indicator to the user that the asynchronous operation is still running.

Use the IsCompleted property of the IAsyncResult interface to determine when the asynchronous call has completed. The following example shows how this is accomplished: Read the rest of this entry »

Popularity: 5% [?]

« Previous Entries