Advertisement
Guest User

EventsAndDelegates

a guest
Jun 25th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace EventsAndDelegates
  5. {
  6. class MessagingService
  7. {
  8. public void OnPatcherFinished(object source, PatcherEventArgs e)
  9. {
  10. Console.WriteLine("Check to see if your patch was successful? Y or N\n");
  11.  
  12. char input = Convert.ToChar(Console.ReadKey().KeyChar);
  13.  
  14. Console.WriteLine("\n");
  15.  
  16. if (input == 'y' || input == 'Y')
  17. {
  18. Console.WriteLine("Checking to see if game is up-to-date...\n");
  19. Thread.Sleep(3000);
  20.  
  21. if (e.UpdateData.IsGameUpToDate == true)
  22. {
  23. Console.WriteLine("Game is up to date! Press any key to close!\n");
  24. Console.ReadLine();
  25. }
  26.  
  27. else
  28. {
  29. Console.WriteLine("Game is no up to date. Please re-run the patcher. :(\n");
  30. Console.ReadLine();
  31. }
  32. }
  33. else
  34. {
  35. Console.WriteLine("Game patch was no validated. Goodbye.\n");
  36. Console.ReadLine();
  37. }
  38.  
  39. }
  40. }
  41.  
  42. public class UpdateData
  43. {
  44. public string DataToBeAppendedLocation { get; set; }
  45. public bool IsGameUpToDate { get; set; }
  46.  
  47. }
  48.  
  49. }
  50.  
  51. public class PatcherEventArgs : EventArgs
  52. {
  53. public UpdateData UpdateData { get; set; }
  54. }
  55.  
  56. public class Patcher
  57. {
  58. // 1 - Define a delegate { No need, used pre-defined "EventHandler" from System namespace }
  59. // 2 - Define an event based on that delegate { Line 21 }
  60. // 3 - Raise that event in a protected, virtual method { Line 36-41 }
  61. // 4 - Derive a new class from EventArts containing event arguments to be passed { Line 9-12 }
  62. // 5 - Link event to function to be calling on the event { in Program.cs, Line 17 }
  63. // 6 - Call the method that raises the event { Line 35 }
  64.  
  65.  
  66. public event EventHandler<PatcherEventArgs> PatcherFinished;
  67.  
  68. public void Patch(UpdateData data)
  69. {
  70. Console.WriteLine("Patching game files from {0}...", data.DataToBeAppendedLocation);
  71. Thread.Sleep(3000);
  72. Console.WriteLine("\nSuccess! Press any key to continue...");
  73. Console.ReadLine();
  74.  
  75. data.IsGameUpToDate = true;
  76.  
  77. OnPatcherFinished(data);
  78.  
  79. }
  80.  
  81. protected virtual void OnPatcherFinished(UpdateData data)
  82. {
  83. if (PatcherFinished != null)
  84. PatcherFinished(this, new PatcherEventArgs() { UpdateData = data });
  85.  
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement