Exemple #1
0
        // Event handler used to write event information to a file
        // when a stock's threshold is reached
        public static void WriteToFile(object sender,
         StockToFileEventArgs e)
        {
            // Get the directory that holds the file to write to
             string currentDirectory = Path.GetDirectoryName(
            Path.GetDirectoryName(Directory.GetCurrentDirectory()));

             // Using a lock ensures only one thread writes to the file
             // at any given time
             lock (thisLock) {

            // Use a streamwriter to write the stock event data
            // to the StockFile text file
            using (StreamWriter sw = new StreamWriter(@currentDirectory
               + "StockFile.txt", true)) {
               sw.WriteLine(e.DateTime.ToString().PadRight(25)
                  + e.Name.PadRight(15)
                  + e.CurrentValue.ToString().PadRight(10)
                  + e.InitialValue.ToString().PadRight(10));
               sw.Close();
            }
             }
        }
Exemple #2
0
        // Method used to raise the event, called when the difference between
        // the stock's current value and its intial value is equal to the
        // threshold.
        protected void OnThresholdReached(StockNotificationEventArgs args1,
         StockToFileEventArgs args2)
        {
            // Using a lock ensures that no other thread tries to print to the
             // console at the same time.
             lock (thisLock) {
            // Calls the write to console event handler
            EventHandler<StockNotificationEventArgs> eventHandler = stockEvent;
            if (eventHandler != null) {
               eventHandler(this, args1);
            }

            // Calls the write to file event handler
            EventHandler<StockToFileEventArgs> fileHandler = stockToFileEvent;
            if (fileHandler != null) {
               fileHandler(this, args2);
            }
             }
        }