Instruction

by Paweł Paduch published 2021/10/28 16:38:05 GMT+2, last modified 2021-10-28T16:38:05+02:00
Delegates, Events and Timers
  1. Simple Delegate (1,0)
    • delegate void PrintSomething(string sth);
      
      static void PrintOryginal(string sth)
              {
                  Console.WriteLine(sth);
              }
      
       static void PrintInCapital(string sth)
              {
                  Console.WriteLine(sth.ToUpper());
              }
      
       static void Main(string[] args)
              {
                  PrintSomething printingDelegate = new PrintSomething(PrintOryginal);
                  printingDelegate("Hello World");
                  printingDelegate = new PrintSomething(PrintInCapital);
                  printingDelegate("Hello World");
                  printingDelegate += new PrintSomething(PrintOryginal);
                  printingDelegate("Hello New World!");
                  Console.WriteLine("Printing delegate has following methods:");
                  foreach (var item in printingDelegate.GetInvocationList())
                  {
                      Console.WriteLine(item.Method.Name);
                  }
              }
      
    • Base on the code of the SimpleDelegate project, define a delegate of type void with two parameters of type int.
    • To the given delegate create 4 methods that print sum, difference, quotient and product.(+,-,/,*) 
    • Add the new methods from the previous section to the delegate invocation list.
    • Call delegate
    • Print the method names from the delegate invocation list. 

  2. Ending v1 (0,5)
    • Run Project SimpleEvent
    • Even when you press any key, the program does not end.
      The reason is waiting for the child process.
    • In the method startClock set the thread property IsBackground to the true.
    • Restart. Press any key without waiting for the event to start.

  3. Ending v2 (1,0)
    • Comment on the line just added
    • Let the Alarm class implement the IDisposable interface
    • This is mainly about the Dispose, method in which we will perform the Interrupt() operation on the internalTrhead thread and then assign it null
    • The thread should handle the ThreadInterruptedException exception, causing it to terminate with an appropriate message.
    • In Main immediately after reading the key, call alarm.Dispose();
    • Launch the program. Check the functionality.

  4.  Possibility to set checkingInterval (0,5) 
    • Overload the constructors and methods of SetAlarm for the option to specify checkingInterval.
    • Currently, the default is 1000ms.

  5.  Add new Event (1,0)
    • Add a timespanToAlarm remaining time counter update, and a read-only property that returns this value.
    • Add an event triggered on every check
    • Along with checking, the event should return the remaining time to an alarm.
    • Add handling of a new event. Demonstrate the operation in the program.

  6. TimerExampe (0,5)
    • Run the project TimerExample.
      We see that timerThreading is started after five seconds with one-second intervals.
      On the other hand, timerSystem starts immediately and at intervals of 1.5s
    • Add the line
      timerSystem.AutoReset = false;
      This will make the timer act as a one-shot alarm clock.
    • We add a counter. Add the following method to the program:
      private static void TimerTask(object timerState)
          {
              Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff}: starting a new callback.");
              var state = timerState as TimerState;
              T.Interlocked.Increment(ref state.Counter);
          }
    • Add class
          class TimerState
          {
              public int Counter;
          }
    • In Main add an object of the class TimerState timerState = new TimerState { Counter = 0 }; 
    • Replace simple display with a call to TimerTask
      T.Timer timerThreading = new T.Timer(new T.TimerCallback(TimerTask), timerState, 5000, 1000);
    • Run it

  7. Turning off the timer (0,5)
    • Using timerThreading.Change(T.Timeout.Infinite, 0);
      Turn off timer after 5 calls (when the counter reaches 4)

  8. Other timers .
    Some libraries may contain their own timers:
    • System.Windows.Forms.Timer: a Windows Forms component that triggers an event at regular intervals. The component has no user interface and is intended for use in a single-threaded environment.
    • System.Web.UI.Timer: an ASP.NET component that performs asynchronous or synchronous web page callbacks at regular intervals.
    • System.Windows.Threading.DispatcherTimer: a timer integrated into a queue that is processed at a specified interval and a Dispatcher with a specified priority.