Programming Tutorial On The Way

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

An exception thrown in a spawned worker thread will cause this thread to be silently terminated if the exception is unhandled. If an unhandled exception occurs in the main thread of an application, the main thread terminates, along with your entire application. An unhandled exception in a spawned worker thread, however, will terminate only that thread. This will happen without any visible warnings, and your application will continue to run as if nothing happened.

Simply wrapping an exception handler around the Start method of the Thread class will not catch the exception on the newly created thread. The Start method is called within the context of the current thread, not the newly created thread. It also returns immediately once the thread is launched, so it isn’t going to wait around for the thread to finish. Therefore, the exception thrown on the new thread will not be caught since it is not visible to any other threads.

You must add exception handling to the method that you pass to the ThreadStart delegate with a try/catch, try/finally, or try/catch/finally block. The code to do this is shown here: Read the rest of this entry »

Popularity: 10% [?]

If you require more control over locking and unlocking of critical sections, you might want to try using the overloaded static Monitor.TryEnter methods. These methods allow more flexibility by introducing a timeout value. The lock keyword will attempt to acquire a lock on a critical section indefinitely. However, with the TryEnter method, you can enter a timeout value in milliseconds (as an integer) or as a TimeSpan structure. The TryEnter methods return true if a lock was acquired and false if it was not. Note that the overload of the TryEnter method that accepts only a single parameter does not block for any amount of time. This method returns immediately, regardless of whether the lock was acquired.
Read the rest of this entry »

Popularity: 7% [?]

When creating multithreading application, thread-safe access through accessor functions to an internal member variable is very important. Poorly written code may cause the application behave improperly, deadlock, etc. This article shows an example of on how to create a thread-safe access class. To achieve it, one way is the use the lock keyword to make a block of code as critical section. This keyword accepts a parameter of either the type object for the class (such as typeof(MyClass)) or a class instance object (new MyClass()). It uses this type or object to control what you are locking.
Read the rest of this entry »

Popularity: 31% [?]

By default, static fields are shared between the threads that access these fields in an application domain as a whole. You need to allow each thread to have its own nonshared copy of a static field, so that this static field can be updated on a per-thread basis. Use ThreadStaticAttribute to mark any static fields as not being shareable between threads. Read the rest of this entry »

Popularity: 66% [?]

  Next Entries »