//CustomEvent void CustomEvent(object sender, StockNotificationEventArgs e) { //lock this code block lock (thisLock) { string stockFormat = String.Format("{0} \t {1} \t {2} \t {3}", name, e.Name, e.CurrentValue, e.NumOfChanges); Console.WriteLine("{0} \t {1} \t {2} \t {3}", name, e.Name, e.CurrentValue, e.NumOfChanges); StringBuilder sb = new StringBuilder(); DateTime now = DateTime.Now; sb.Append("Written on ->" + now.ToString() + "\t"); sb.Append(stockFormat); try { File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + @"\" + "output.txt", sb.ToString() + Environment.NewLine); } catch (IOException) { //do nothing } }//unlock code block }
//start event protected virtual void startEvent(StockNotificationEventArgs e) { //declare event handler StockNotificationHandler handler = StockEvent; //check event handler if (handler != null) { handler(this, e); } }
//UpdateStock = currentValue + rand(maxChange) public void updateStock() { Random rand = new Random(); //update currentValue with +/- random number < maxChange currentValue += rand.Next(-(maxChange), maxChange); //Calculate diffValue between current and initalValue int diffValue = Math.Abs(currentValue - initialValue); //update number of change that take place on this stock numOfChange++; //throw event if diffValue is larger than threshold if(diffValue >= threshold) { StockNotificationEventArgs args = new StockNotificationEventArgs(name, initialValue, currentValue, diffValue, numOfChange); startEvent(args); } }