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: 26% [?]

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: 25% [?]

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: 26% [?]

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: 25% [?]

Encrypting the string will prevent users from being able to read and decipher the information. If you have a string you want to be able to encrypt and decrypt — perhaps a password or software key — which will be stored in some form accessible by users, such as in a file, the registry, or even a field, that may be open to attack from malicious code. The sample code in this article, CryptoString class, contains two static methods to encrypt and decrypt a string and two static properties to retrieve the generated key and inititialization vector (IV—a random number used as a starting point to encrypt data) after encryption has occurred.

This class uses the Rijndael algorithm to encrypt and decrypt a string. This algorithm is found in the System.Security.Cryptography.RijndaelManaged class. This algorithm requires a secret key and an initialization vector; both are byte arrays. A random secret key can be generated for you by calling the GenerateKey method on the RijndaelManaged class. This method accepts no parameters and returns void. The generated key is placed in the Key property of the RijndaelManaged class. The GenerateIV method generates a random initialization vector and places this vector in the IV property of the RijndaelManaged class.
Read the rest of this entry »

Popularity: 25% [?]

Now it’s easy and it has many ways to get the thing that you need on internet, whether you are looking for electronic products, clothes, skincare, or food. Savebuckets is a site that gives you the ease to find them all. This site has many kinds of product that you need in your daily life. Beside that, it also has the buying that can help you find the product and explain what you need to look for. I think this site is really a great help for us. The design of this site is easy to use too, for example, if you want to find the product with the low price, you just need to type it in the search box, and this site will give you the result of all the information that you need. It will give you the price comparison from all shop that has the product that you are looking for, or you also can search by the shop that you know only, it’s easy and save time right?
Read the rest of this entry »

Popularity: 5% [?]

Rapid C# Windows Development

Using the Self-Servicing template group is similar to an office environment where
coworkers are empowered and. expected to know what needs to be done to do …
Read the rest of this entry »

Popularity: 3% [?]

Play a Simple Beep

The .NET Framework does not include any managed classes for playing audio files or even for playing the system beep sound. However, you can easily bridge this gap using the Win32 API or Visual Basic .NET, which provides a legacy Beep function that’s exposed through the Microsoft.VisualBasic.Interaction class. In the latter case, you must add a reference to the Microsoft.VisualBasic.dll assembly (which is included with all versions of the .NET Framework).

The following example uses both the Beep API function and the Visual Basic Beep function. Notice that the API function actually uses the computer’s internal speaker and plays a tone with the indicated frequency (in hertz, ranging from 37 to 32,767) and duration (in milliseconds). This won’t produce any sound if the computer does not have an internal speaker. The Visual Basic Beep function, on the other hand, plays the standard system-defined beep sound event (which is a WAV audio file). This won’t produce any sound if the computer doesn’t have a sound card, if the sound card is not connected to external speakers, or if Windows is configured (via the Sounds and Audio Devices section of the Control Panel) to not play sounds.
Read the rest of this entry »

Popularity: 4% [?]

« Previous Entries