home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_HelloAsync2_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-03-21  |  3.3 KB  |  89 lines

  1. /*=====================================================================
  2.  
  3.   File:      HelloAsync2.cs
  4.  
  5. ---------------------------------------------------------------------
  6. This file is part of the Microsoft .NET Framework SDK Code Samples.
  7.  
  8.   Copyright (C) Microsoft Corporation.  All rights reserved.
  9.  
  10. This source code is intended only as a supplement to Microsoft
  11. Development Tools and/or on-line documentation.  See these other
  12. materials for detailed information regarding Microsoft code samples.
  13.  
  14. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  15. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  17. PARTICULAR PURPOSE.
  18. =====================================================================*/
  19.  
  20. using System;
  21. using System.Runtime.Remoting;
  22. using System.Runtime.Remoting.Messaging;
  23. using System.Threading;
  24.  
  25. using Hello;
  26.  
  27. public class HelloAsync2
  28. {
  29.     public static void Main(String[] args)
  30.     {
  31.         Console.WriteLine(".NET Remoting Sample - HelloAsync2");
  32.         Console.WriteLine("----------------------------------\n");
  33.  
  34.         HelloAsync2 helloAsync = new HelloAsync2();
  35.         helloAsync.Run();
  36.     }
  37.  
  38.     public void Run()
  39.     {
  40.         String name = "Bill";    
  41.         String configFilename = "HelloAsync2.exe.config";
  42.  
  43.         Console.WriteLine("Configuring Remoting from {0}", configFilename);
  44.         RemotingConfiguration.Configure(configFilename);
  45.  
  46.         Console.WriteLine("Obtaining Proxy for HelloService, using new");
  47.         HelloService helloService = new HelloService();
  48.  
  49.         Console.WriteLine("Creating ReturnGreetingDelegate to HelloService.ReturnGreeting");
  50.         ReturnGreetingDelegate returnGreetingDelegate = new ReturnGreetingDelegate(helloService.ReturnGreeting);
  51.  
  52.         Console.WriteLine("Creating AsyncCallback to HelloAsync.greettingCallback");
  53.         AsyncCallback asyncCallback = new AsyncCallback(this.greetingCallBack);
  54.  
  55.         Console.WriteLine("Creating ManualResetEvent, will be fired when greetingCallBack is complete");
  56.         ManualResetEvent evt = new ManualResetEvent(false);
  57.  
  58.         Console.WriteLine("ManualResetEvent is passed in async state object, greetingCallBack will use it");
  59.         Object stateObject = evt; 
  60.  
  61.         Console.WriteLine("Calling returnGreetingDelegate.BeginInvokeInvoke");
  62.         IAsyncResult ar = returnGreetingDelegate.BeginInvoke(name, asyncCallback, stateObject);
  63.  
  64.         Console.WriteLine("Waiting for greetingCallback to completed");
  65.         evt.WaitOne();
  66.         Console.WriteLine("Event fired");
  67.     }
  68.  
  69.     public delegate String ReturnGreetingDelegate(String name);
  70.  
  71.     public void greetingCallBack(IAsyncResult ar)
  72.     {
  73.         ReturnGreetingDelegate returnGreetingDelegate = (ReturnGreetingDelegate)((AsyncResult)ar).AsyncDelegate;
  74.  
  75.         Console.WriteLine("Calling returnGreetingDelegate.EndInvoke");
  76.         String greeting = returnGreetingDelegate.EndInvoke(ar);
  77.  
  78.         Console.WriteLine("returnGreetingDelegate.EndInvoke returned: {0}", greeting);
  79.  
  80.         Console.WriteLine("Fire event that greetingCallBack is complete");
  81.         ManualResetEvent stateObjectEvt = (ManualResetEvent)ar.AsyncState;
  82.         stateObjectEvt.Set();
  83.     }
  84. }
  85.  
  86.  
  87.  
  88.  
  89.