×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

4 kinds of asynchrous call method

本文发表在 rolia.net 枫下论坛I have 4 kinds of asychrous calls methods. I have tested them all.

First one

using System;

public class AsyncMain4
{
// Asynchronous method puts the thread id here.
private static int threadId;

public static void Main(string[] args)
{
// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();

// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

// Initiate the asychronous call. Include an AsyncCallback
// delegate representing the callback method, and the data
// needed to call EndInvoke.
IAsyncResult ar = dlgt.BeginInvoke(3000,
out threadId,
new AsyncCallback(CallbackMethod),
dlgt );

Console.WriteLine("Press Enter to close application.");
Console.ReadLine();
}

// Callback method must have the same signature as the
// AsyncCallback delegate.
static void CallbackMethod(IAsyncResult ar)
{
// Retrieve the delegate.
AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState;

// Call EndInvoke to retrieve the results.
string ret = dlgt.EndInvoke(out threadId, ar);

Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
}
}

Second one:
using System;
using System.Threading;

public class AsyncMain1
{
public static void Main(string[] args)
{
// The asynchronous method puts the thread id here.
int threadId;

// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();

// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

// Initiate the asychronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, null, null);

Thread.Sleep(0);
Console.WriteLine("Main thread {0} does some work.",
AppDomain.GetCurrentThreadId());

// Call EndInvoke to Wait for the asynchronous call to complete,
// and to retrieve the results.
string ret = dlgt.EndInvoke(out threadId, ar);

Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
}
}

third one
using System;
using System.Threading;

public class AsyncMain3
{
public static void Main(string[] args)
{
// The asynchronous method puts the thread id here.
int threadId;

// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();

// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

// Initiate the asychronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000,
out threadId, null, null);

// Poll while simulating work.
while(ar.IsCompleted == false)
{
Thread.Sleep(10);
}

// Call EndInvoke to retrieve the results.
string ret = dlgt.EndInvoke(out threadId, ar);

Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
}
}

fourth one:
using System;
using System.Threading;

public class AsyncMain2
{
public static void Main(string[] args)
{
// The asynchronous method puts the thread id here.
int threadId;

// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();

// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

// Initiate the asychronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000,
out threadId, null, null);

Thread.Sleep(0);
Console.WriteLine("Main thread {0} does some work.",
AppDomain.GetCurrentThreadId());

// Wait for the WaitHandle to become signaled.
ar.AsyncWaitHandle.WaitOne();

// Perform additional processing here.
// Call EndInvoke to retrieve the results.
string ret = dlgt.EndInvoke(out threadId, ar);

Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 哪位大虾用过asynchronous的web service调用?偶照葫芦画瓢在asp.net中写的居然是死锁, 大虾们若不救偶, 偶的cpu要爆炸了。:((
    • 咦,你不是在跟我做同一个公司的面试题吧?你要想去我就不做了,反正我有事做,那里比较远,我还懒得去了。要是不是的话,你可以试试下面的题,我懒得做
      本文发表在 rolia.net 枫下论坛Details:
      The application should provide the following functionality:
      · Allow the user to submit either a Synchronous Mode or an Asynchronous Mode request to the web service.
      · For a Synchronous Mode request, immediately display the result of the web service’s processing.
      · For an Asynchronous Mode request, immediately return an indication to the user that the request has been submitted. The user must then have some means of querying the web service to determine if the request has completed yet or not. Once the request has completed, the user must be able to retrieve the results of the request.
      The web service itself can do whatever you like. For example, it could retrieve a .pdf document for the user. In the Synchronous Mode it would retrieve the document and immediately present it to the user. In the Asynchronous Mode you might imagine it creating the document (maybe from information stored in a database).
      For the purposes of this assignment, the Asynchronous Mode can simply return the same document as the Synchronous Mode request – perhaps after a certain period of time has elapsed (so that we can have the opportunity to determine if the request has completed or not).更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • 收信,请!
        • 收信,请?
          • 请你check你的email。:((
          • 看见了。
    • up
      • Ask 秋波for consequence, hopefully he is not struggling in storm right now.
        • :D:D
          • 解决了?
            • waithandle is ok. callback is dead. :-(
              • callback 死掉是不是因为 thread 的问题?要 merge 回 UI thread...
                • 能不能详细些?谢谢!
                • 喂, 你这个人怎么说了一半就不来了,7456。 :((
                  • 啊,不好意思。不太清楚你的具体问题。在 windows form 中异步调用 web svc 常犯的错误是在 callback method 里面直接操作 UI,比如 update control.Text 或者 update DataSet 什么的。不知道你碰到的是什么情况。
                    • 谢谢。 我好象刚才贴过代码, 现在找不到了,到BeginXXX调用的时候就不行了,总是不complete。 我来把刚才的帖子顶出来, 你等着,要不我今天睡不着觉。
    • Hope it helpful
      1. Create a WebService, and run it to make sure that your web service is correct. Copy the url of the web service for future use. For example you create a web service called CustomerService, and a method called GetCustomersAll.
      2. Create a client application, right click reference and click add web reference, put the URL you just copied. Then .NET will automatically create a proxy. For each method, .Net will create 3 method call to that service, for example, one is GetCustomersAll, another 2 is BeginGetCustomersAll, and EndGetCustomersAll. If you call GetCustomersAll directly, this is a synchrous call. If you create a callback, and call BeginGetCustomersAll, this is a asynchrous call.

      I will send you an email include a WebService and its client
      • 我不用callback的asynchronous call能正确执行, 就是用callback做的不行, 不知道问题在哪里,越是不对越想知道为什么。 谢谢! :(
        • 4 kinds of asynchrous call method
          本文发表在 rolia.net 枫下论坛I have 4 kinds of asychrous calls methods. I have tested them all.

          First one

          using System;

          public class AsyncMain4
          {
          // Asynchronous method puts the thread id here.
          private static int threadId;

          public static void Main(string[] args)
          {
          // Create an instance of the test class.
          AsyncDemo ad = new AsyncDemo();

          // Create the delegate.
          AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

          // Initiate the asychronous call. Include an AsyncCallback
          // delegate representing the callback method, and the data
          // needed to call EndInvoke.
          IAsyncResult ar = dlgt.BeginInvoke(3000,
          out threadId,
          new AsyncCallback(CallbackMethod),
          dlgt );

          Console.WriteLine("Press Enter to close application.");
          Console.ReadLine();
          }

          // Callback method must have the same signature as the
          // AsyncCallback delegate.
          static void CallbackMethod(IAsyncResult ar)
          {
          // Retrieve the delegate.
          AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState;

          // Call EndInvoke to retrieve the results.
          string ret = dlgt.EndInvoke(out threadId, ar);

          Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
          }
          }

          Second one:
          using System;
          using System.Threading;

          public class AsyncMain1
          {
          public static void Main(string[] args)
          {
          // The asynchronous method puts the thread id here.
          int threadId;

          // Create an instance of the test class.
          AsyncDemo ad = new AsyncDemo();

          // Create the delegate.
          AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

          // Initiate the asychronous call.
          IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, null, null);

          Thread.Sleep(0);
          Console.WriteLine("Main thread {0} does some work.",
          AppDomain.GetCurrentThreadId());

          // Call EndInvoke to Wait for the asynchronous call to complete,
          // and to retrieve the results.
          string ret = dlgt.EndInvoke(out threadId, ar);

          Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
          }
          }

          third one
          using System;
          using System.Threading;

          public class AsyncMain3
          {
          public static void Main(string[] args)
          {
          // The asynchronous method puts the thread id here.
          int threadId;

          // Create an instance of the test class.
          AsyncDemo ad = new AsyncDemo();

          // Create the delegate.
          AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

          // Initiate the asychronous call.
          IAsyncResult ar = dlgt.BeginInvoke(3000,
          out threadId, null, null);

          // Poll while simulating work.
          while(ar.IsCompleted == false)
          {
          Thread.Sleep(10);
          }

          // Call EndInvoke to retrieve the results.
          string ret = dlgt.EndInvoke(out threadId, ar);

          Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
          }
          }

          fourth one:
          using System;
          using System.Threading;

          public class AsyncMain2
          {
          public static void Main(string[] args)
          {
          // The asynchronous method puts the thread id here.
          int threadId;

          // Create an instance of the test class.
          AsyncDemo ad = new AsyncDemo();

          // Create the delegate.
          AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);

          // Initiate the asychronous call.
          IAsyncResult ar = dlgt.BeginInvoke(3000,
          out threadId, null, null);

          Thread.Sleep(0);
          Console.WriteLine("Main thread {0} does some work.",
          AppDomain.GetCurrentThreadId());

          // Wait for the WaitHandle to become signaled.
          ar.AsyncWaitHandle.WaitOne();

          // Perform additional processing here.
          // Call EndInvoke to retrieve the results.
          string ret = dlgt.EndInvoke(out threadId, ar);

          Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
          }
          }更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • 谢谢! 我的就是跟你的第一种一样, 后面的我试过, 同样的webservice,不用callback都行。你觉得问题在哪里?
            • General asynchrous need a delegate, maybe delegate can not refer a web service method because it is too far.
        • Put this file in a project
          using System;
          using System.Threading;

          public class AsyncDemo
          {
          // The method to be executed asynchronously.
          //
          public string TestMethod(int callDuration, out int threadId)
          {
          Console.WriteLine("Test method begins.");
          Thread.Sleep(callDuration);
          threadId = AppDomain.GetCurrentThreadId();
          return "MyCallTime was " + callDuration.ToString();
          }
          }

          // The delegate must have the same signature as the method
          // you want to call asynchronously.
          public delegate string AsyncDelegate(int callDuration, out int threadId);

          public class MainClass
          {
          public static void Main(string[] args)
          {
          AsyncMain1.Main1(args);
          // AsyncMain2.Main2(args);
          // AsyncMain3.Main3(args);
          // AsyncMain4.Main4(args);
          }
          }