To get the preview of all available fonts installed in your computer, you can create a simple windows application for that. Basically, you need to create a new instance of the System.Drawing.Text.InstalledFontCollection class, which contains a collection of FontFamily objects representing all the installed fonts.
The InstalledFontCollection class allows you to retrieve information about currently installed fonts. The following code shows a form that iterates through the font collection when it’s first created. Every time it finds a font, it creates a new label that will display the font name in the given font face (at a size of 14 point). The label is added to a scrollable panel, allowing the user to scroll through the list of available fonts.
Read the rest of this entry »
Popularity: 3% [?]
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: 9% [?]
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% [?]
In .NET, you can use the FileSystemWatcher to notify when a particular event occurs in the filesystem, such as the renaming of a particular file or directory, the increasing or decreasing of the size of a file, the user deleting a file or directory, the creation of a file or directory, or even the changing of a file or directory’s attribute(s).
The WaitForChanged method of the FileSystemWatcher class can be called to wait synchronously for an event notification. This is illustrated in the following method, which waits for an action¡ªmore specifically, the action of creating the Backup.zip file somewhere on the C:\ drive¡ªto be performed before proceeding on to the next line of code, which is the WriteLine statement. Finally, we ask the ThreadPool to use a thread to go create the file in question using the PauseAndCreateFile method, so that the FileSystemWatcher can detect the file creation: 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% [?]
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% [?]