Guest User

Untitled

a guest
May 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. class MyClass
  2. {
  3. private delegate void SomeFunctionDelegate(int param1, bool param2);
  4. private SomeFunctionDelegate sfd;
  5.  
  6. public MyClass()
  7. {
  8. sfd = new SomeFunctionDelegate(this.SomeFunction);
  9. }
  10.  
  11. private void SomeFunction(int param1, bool param2)
  12. {
  13. // Do stuff
  14.  
  15. // Notify user
  16. }
  17.  
  18. public void GetData()
  19. {
  20. // Do stuff
  21.  
  22. sfd.BeginInvoke(34, true, null, null);
  23. }
  24. }
  25.  
  26. public class MainClass
  27. {
  28. private sealed class AsyncClass
  29. {
  30. private int _counter;
  31. private readonly int _maxCount;
  32.  
  33. public AsyncClass(int maxCount) { _maxCount = maxCount; }
  34.  
  35. public void Run()
  36. {
  37. while (_counter++ < _maxCount) { Thread.Sleep(1); }
  38. CompletionTime = DateTime.Now;
  39. }
  40.  
  41. public DateTime CompletionTime { get; private set; }
  42. }
  43.  
  44. private AsyncClass _asyncInstance;
  45. public void StartAsync()
  46. {
  47. var asyncDoneTime = DateTime.MinValue;
  48. _asyncInstance = new AsyncClass(10);
  49. Action asyncAction = _asyncInstance.Run;
  50. asyncAction.BeginInvoke(
  51. ar =>
  52. {
  53. asyncAction.EndInvoke(ar);
  54. asyncDoneTime = _asyncInstance.CompletionTime;
  55. }, null);
  56. Console.WriteLine("Async task ended at {0}", asyncDoneTime);
  57. }
  58. }
Add Comment
Please, Sign In to add comment