/// <summary> /// Callback fired each time some work is performed /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OnMessageReceived(object sender, WorkPerformedEventArgs args) { Application.Current.Dispatcher.Invoke(() => { ListBox.Add(args.Data); }); }
// Job Start public virtual void DoWork(WorkPerformedEventArgs e) { // Time Consuming Work Console.WriteLine("Start {0} ...", e.Name); var time = e.Hours * 1000; Thread.Sleep(time); // Event Invocation WorkPerformed?.Invoke(e); }
static void Main(string[] args) { // Jobs WorkPerformedEventArgs game = new WorkPerformedEventArgs("Gaming", 6); WorkPerformedEventArgs develop = new WorkPerformedEventArgs("Developing", 3); // Worker Worker worker = new Worker(); worker.WorkPerformed += worker.workPerformed; // worker.WorkPerformed += delegate (WorkPerformedEventArgs e) { Console.WriteLine(e.Hours.ToString()); }; worker.DoWork(game); worker.DoWork(develop); }
private static void Worker_WorkPerformed(object sender, WorkPerformedEventArgs e) { // Expected var expectedWorkType = WorkType; var expectedHourCount = ++HourCount; const bool expectedCompletionState = false; // Actual var actualWorkType = e.WorkType; var actualHourCount = e.Hours; var actualCompletionState = Worker.IsWorkCompleted(); // Assert Work Type Debug.WriteLine($"Expected Work Type = {expectedWorkType}. Actual Work Type = {actualWorkType}."); Assert.AreEqual(expectedWorkType, actualWorkType, "Unexpected work type."); // Assert Hour Count Debug.WriteLine($"Expected Hour Count = {expectedHourCount}. Actual Hour Count = {actualHourCount}."); Assert.AreEqual(expectedHourCount, actualHourCount, "Unexpected hour count."); // Assert Completion State Debug.WriteLine($"Expected Completion State = {expectedCompletionState}. Actual Completion State = {actualCompletionState}."); Assert.AreEqual(expectedCompletionState, actualCompletionState, "Unexpected completion state."); }
// On Job Finished public void workPerformed(WorkPerformedEventArgs e) { Console.WriteLine("Finished after {0} hours", e.Hours); }
//private static void worker_WorkCompleted(object sender, EventArgs e) //{ // Console.WriteLine("Worker is done working!"); //} static void worker_WorkPerformed(object sender, WorkPerformedEventArgs e) { Console.WriteLine($"Total Work Hours : {e.Hours} in {e.WorkType}"); }