FREE BOOK

Chapter 10: Processes, AppDomains,Contexts, and Threads

Posted by Apress Free Book | C#.NET February 02, 2009
In this Chapter you drill deeper into the constitution of a .NET executable host and come to understand the relationship between Win32 processes, application domains, contexts, and threads.

Programming with Timer Callbacks
 
At this point you have seen a number of ways in which you are able to provide synchronized access to shared blocks of data. To be sure, there are additional types under the System.Threading namespace, which I will allow you to explore at your leisure. However, to wrap up our examination of thread programming, allow me to introduce two additional types, TimerCallback and Timer.
 
Many applications have the need to call a specific method during regular intervals of time. For example, you may have an application that needs to display the current time on a status bar via a given helper function. As another example, you may wish to have your application call a helper function every so often to perform noncritical background tasks such as checking for new e-mail messages. For situations such as these, the System.Threading.Timer type can be used in conjunction with a related delegate named TimerCallback.
 
To illustrate, assume you have a console application that will print the current time every second until the user hits a key to terminate the application. The first obvious step is to write the method that will be called by the Timer type:
 
     class TimePrinter
 
    {
         static void PrintTime(object state)
         {
             Console.WriteLine("Time is: {0}",
             DateTime.Now.ToLongTimeString());
         }
 
    }
 

Notice how this method has a single parameter of type System.Object and returns void. This is not optional, given that the TimerCallback delegate can only call methods that match this signature. The value passed into the target of your TimerCallback delegate can be any bit of information whatsoever (in the case of the e-mail example, this parameter might represent the name of the MS Exchange server to interact with during the process). Also note that given that this parameter is indeed a System.Object, you are able to pass in multiple arguments using a System.Array type.
 
The next step would be to configure an instance of the TimerCallback type and pass it into the Timer object. In addition to a TimerCallback delegate, the Timer constructor also allows you to specify the optional parameter information to pass into the delegate target, the interval to poll the method, as well as the amount of time to wait before making the first call. For example:
 
         static void Main(string[] args)
         {
             Console.WriteLine("***** Working with Timer type *****\n");
             // Create the delegate for the Timer type.
 
            TimerCallback timeCB = new TimerCallback(PrintTime);
             // Establish timer settings.
 
            Timer t = new Timer(
             timeCB, // The TimerCallback delegate type.
 
            null, // Any info to pass into the called method (null for no info).
 
            0, // Amount of time to wait before starting.
 
            1000); // Interval of time between calls.
 
            Console.WriteLine("Hit key to terminate...");
             Console.ReadLine();
 
        }
 
In this case, the PrintTime() method will be called roughly every second, and will pass in no additional information to said method. If you did wish to send in some information for use by the delegate target, simply substitute the null value of the second constructor parameter with the appropriate information. For example, ponder the following updates:
 
         static void PrintTime(object state)
         {
             Console.WriteLine("Time is: {0}, Param is: {1}",
             DateTime.Now.ToLongTimeString(), state.ToString());
         }
 
        Timer t = new Timer(timeCB, "Hi", 0, 1000);
 

 Figure 10-18 shows the output.
 
 
 
 Figure 10-18. The (very useful) console-based clock application

SOURCE CODE The TimerApp application is included under the Chapter 10 subdirectory.
 
That wraps up our examination of multithreaded programming under .NET. To be sure, the System.Threading namespace defines numerous types beyond what I had the space to examine in this chapter. Nevertheless, at this point you should have a solid foundation to build on.

Total Pages : 13 910111213

comments