C#(2)

今天老师上课讲到了异步编程。

一个异步过程调用发出后,调用者不能立刻得到结果。基于事件机制,实际处理这个调用的部件在完成后,通过状态、通知和回调来通知调用者。比如,你现在要有一批数据要大数据要入库,你又不想一边入库一边等待返回结果,你可以用异步,将大数据推入一个队列,然后另外一个线程来操作这个队列里面的数据入库,入完了,就通知一下主线程。这段时间你的主线程可以做任何事。

首先提到了Asynchromous Programming Model,异步编写模型是一种模式,该模式允许用更少的线程去做更多的操作,.NET
Framework很多类也实现了该模式,同时我们也可以自定义类来实现该模式,(也就是在自定义的类中实现返回类型为IAsyncResult接口的BeginXXX方法和EndXXX
方法)委托类型也定义了BeginInvoke和EndInvoke方法。

以FileStream 为例

同步方法

public override int Read(byte[] array,int offset, int count);

异步方法

public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject);

结束异步的操作:

public override int EndRead(IAsyncResult asyncResult);

APM提供了四种方式供开发人员选择:

1. 在调用BeginXxx方法的线程上调用EndXxx方法来得到异步操作的结果,但是这种方式会阻塞调用线程,知道操作完成之后调用线程才继续运行

2. 查询IAsyncResult的syncWaitHandle属性,从而得到WaitHandle,然后再调用它的WaitOne方法来使一个线程阻塞并等待操作完成再调用EndXxx方法来获得操作的结果。

3. 循环查询IAsyncResult的IsComplete属性,操作完成后再调用EndXxx方法来获得操作返回的结果。

4. 使用 AsyncCallback委托来指定操作完成时要调用的方法,在操作完成后调用的方法中调用EndXxx操作来获得异步操作的结果。

推荐使用第四种方法:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Specialized;
using System.Collections;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class UseDelegateForAsyncCallback
    {
        static int requestCounter;
        static ArrayList hostData = new ArrayList();
        static StringCollection hostNames = new StringCollection();
        static void UpdateUserInterface()
        {
            // Print a message to indicate that the application
            // is still working on the remaining requests.
            Console.WriteLine("{0} requests remaining.", requestCounter);
        }
        public static void Main()
        {
            // Create the delegate that will process the results of the 
            // asynchronous request.
            AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation);
            string host;
            do
            {
                Console.Write(" Enter the name of a host computer or <enter> to finish: ");
                host = Console.ReadLine();
                if (host.Length > 0)
                {
                    // Increment the request counter in a thread safe manner.
                    Interlocked.Increment(ref requestCounter);
                    // Start the asynchronous request for DNS information.
                    Dns.BeginGetHostEntry(host, callBack, host);
                 }
            } while (host.Length > 0);
            // The user has entered all of the host names for lookup.
            // Now wait until the threads complete.
            while (requestCounter > 0)
            {
                UpdateUserInterface();
            }
            // Display the results.
            for (int i = 0; i< hostNames.Count; i++)
            {
                object data = hostData [i];
                string message = data as string;
                // A SocketException was thrown.
                if (message != null)
                {
                    Console.WriteLine("Request for {0} returned message: {1}", 
                        hostNames[i], message);
                    continue;
                }
                // Get the results.
                IPHostEntry h = (IPHostEntry) data;
                string[] aliases = h.Aliases;
                IPAddress[] addresses = h.AddressList;
                if (aliases.Length > 0)
                {
                    Console.WriteLine("Aliases for {0}", hostNames[i]);
                    for (int j = 0; j < aliases.Length; j++)
                    {
                        Console.WriteLine("{0}", aliases[j]);
                    }
                }
                if (addresses.Length > 0)
                {
                    Console.WriteLine("Addresses for {0}", hostNames[i]);
                    for (int k = 0; k < addresses.Length; k++)
                    {
                        Console.WriteLine("{0}",addresses[k].ToString());
                    }
                }
            }
       }
        // The following method is called when each asynchronous operation completes.
        static void ProcessDnsInformation(IAsyncResult result)
        {
            string hostName = (string) result.AsyncState;
            hostNames.Add(hostName);
            try 
            {
                // Get the results.
                IPHostEntry host = Dns.EndGetHostEntry(result);
                hostData.Add(host);
            }
            // Store the exception message.
            catch (SocketException e)
            {
                hostData.Add(e.Message);
            }
            finally 
            {
                // Decrement the request counter in a thread-safe manner.
                Interlocked.Decrement(ref requestCounter);
            }
        }
    }
}
C# 5.0 async / await
异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要。 对Web 资源的访问有时很慢或会延迟。 如果此类活动在同步过程中受阻,则整个应用程序必须等待。 在异步过程中,应用程序可继续执行不依赖 Web 资源的其他工作,直至潜在阻止任务完成。

async Task<int> AccessTheWebAsync()
{
<span style="white-space:pre">	</span>HVpClient client = new HVpClient();
<span style="white-space:pre">	</span>Task<string> getStringTask = client.GetStringAsync("hVp://msdn.microsod.com");
<span style="white-space:pre">	</span>DoIndependentWork();
<span style="white-space:pre">	</span>string urlContents = await getStringTask;
<span style="white-space:pre">	</span>return urlContents.Length;
}


最后就是HttpClient

HttpClient是接收HttpResponseMessages和发送HttpRequestMessages的主要类,如果你习惯了使用WebClient或者是HttpWebRequest, 需要注意HttpClient和他们
不同的地方:

1、在HttpClient实例上配置扩展,设置默认的头部,取消未完成的请求和更多的设置。
2、你通过一个单一的HttpClient实例,它有自己的连接池。
3、HttpClients不与特定的HTTP服务器绑定,你可以使用相同的HttpClient实例提交任何HTTP请求。
4、你可以用HttpClient为特定的站点创建特殊的Client
5、HttpClient采用新的型模式处理异步请求使它更容易管理和协调更多的请求。

HttpClient - Get

<pre name="code" class="csharp">client.GetAsync(_address).ConenueWith((requestTask)=>
{
	//Get HTTP response from completed task.
	HttpResponseMessage response = requestTask.Result;
	//Check that response was successful or throw exception
	response.EnsureSuccessStatusCode();
	//Read response asynchronously as JsonValue and write out top facts for each country
	response.Content.ReadAsAsync<JsonArray>().ConenueWith((readTask)=>{
		Console.WriteLine("First 50 countries listed by The World Bank...");
		foreach(var country in readTask.Result[1])
		{
		Console.WriteLine("{0},Country Code:{1},Capital:{2},Laetude:{3},Longitude:{4}",country.Value["name"],
		country.Value["iso2Code"],country.Value["capitalCity"],country.Value["laetude"],country.Value["longitude"]);
		}
	});
});

HttpClient-Post

Uri serviceReq = new Uri(serviceAddress);
HttpClient client = new HttpClient();
HttpContent content = new StringContent(@"{ ""value"": ""test""}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// Send a request asynchronously continue when complete
client.PostAsync(serviceReq, content).ContinueWith(
(requestTask) =>
{
// Get HTTP response from completed task.
HttpResponseMessage response = requestTask.Result;
// Check that response was successful or throw exception
response.EnsureSuccessStatusCode();
// Read response asynchronously as JsonValue and write out top facts for each country
response.Content.ReadAsAsync<string>().ContinueWith(
(readTask) =>
{
Console.WriteLine(readTask.Result);
});
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章