2
0

AsyncHandler.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Threading;
  3. using System.Web;
  4. namespace TestMonoWeb {
  5. /// <summary>
  6. /// Summary description for AsyncHandler.
  7. /// </summary>
  8. public class AsyncHandler : IHttpAsyncHandler {
  9. private HttpContext _context;
  10. public bool IsReusable {
  11. get {
  12. //To enable pooling, return true here.
  13. //This keeps the handler in memory.
  14. return false;
  15. }
  16. }
  17. public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) {
  18. AsynchOperation asynch = new AsynchOperation(cb, context, null);
  19. asynch.StartAsyncWork();
  20. context.Response.Write("AsyncHandler.BeginProcessRequest<br>\n");
  21. context.Response.Flush();
  22. //Signal the application that asynchronous
  23. //processing is being performed.
  24. SomeResult asynchForBegin = new SomeResult();
  25. //Processing is not synchronous.
  26. asynchForBegin.SetSynch(false);
  27. //Processing is not complete.
  28. asynchForBegin.SetCompleted(false);
  29. _context = context;
  30. return new SomeResult();
  31. }
  32. public void EndProcessRequest(IAsyncResult result) {
  33. _context.Response.Write("AsyncHandler.EndProcessRequest<br>\n");
  34. }
  35. //This method is required but is not called.
  36. public void ProcessRequest(HttpContext context) {
  37. }
  38. }//end class
  39. public class SomeResult : IAsyncResult {
  40. /*
  41. An instance of this class is returned to the application.
  42. This class lets the application know how the BeginEventHandler method has been handled. The application checks the CompletedSynchronously method.
  43. */
  44. private bool _blnIsCompleted = false;
  45. private Mutex myMutex = null;
  46. private Object myAsynchStateObject = null;
  47. private bool _blnCompletedSynchronously = false;
  48. public void SetCompleted(bool blnTrueOrFalse) {
  49. _blnIsCompleted = blnTrueOrFalse;
  50. }
  51. public void SetSynch(bool blnTrueOrFalse) {
  52. _blnCompletedSynchronously = blnTrueOrFalse;
  53. }
  54. public bool IsCompleted {
  55. /*
  56. This is not called by the application. However, set it to true.
  57. */
  58. get {
  59. return _blnIsCompleted;
  60. }
  61. }
  62. public WaitHandle AsyncWaitHandle {
  63. //The application does not call this method.
  64. get {
  65. return myMutex;
  66. }
  67. }
  68. public Object AsyncState {
  69. //The application does not call this method because
  70. //null is passed in as the last parameter to BeginEventHandler.
  71. get {
  72. return myAsynchStateObject;
  73. }
  74. }
  75. public bool CompletedSynchronously {
  76. //The application wants to know if this is synchronous.
  77. //Return true if the Begin method was called synchronously.
  78. get {
  79. return _blnCompletedSynchronously;
  80. }
  81. }
  82. }
  83. }