日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网络编程 - 异步调用

發布時間:2025/3/13 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网络编程 - 异步调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??? 這幾天主要是在開發一個《MP3下載器》他的主要功能是:

  • ?通過指定格式的文本文件輸入需要下載的歌曲;
  • 自動在互聯網上尋找最優的下載鏈接;
  • 自動從最優的下載鏈接中下載文件到指定的文件夾并自動改名。
  • ??? 現在就來談談我在這個軟件開發中遇到的困難及解決的辦法:
    在軟件開發中遇的第一個問題是網絡文件的下載,參考網上的一個例子是通過定時來查詢下載文件的大小的,這樣就導致系統不能精確地計算文件下載情況,后來看到了微軟的一個例子使我解決了這個問題:

    附:微軟的代碼

    using?System;
    using?System.Net;
    using?System.IO;
    using?System.Text;
    using?System.Threading;



    public?class?RequestState
    {
    ??
    //?This?class?stores?the?state?of?the?request.
    ??const?int?BUFFER_SIZE?=?1024;
    ??
    public?StringBuilder?requestData;
    ??
    public?byte[]?bufferRead;
    ??
    public?WebRequest?request;
    ??
    public?WebResponse?response;
    ??
    public?Stream?responseStream;
    ??
    public?RequestState()
    ??
    {
    ????bufferRead?
    =?new?byte[BUFFER_SIZE];
    ????requestData?
    =?new?StringBuilder("");
    ????request?
    =?null;
    ????responseStream?
    =?null;
    ??}

    }

    class?WebRequest_BeginGetResponse
    {
    ??
    public?static?ManualResetEvent?allDone=?new?ManualResetEvent(false);
    ??
    const?int?BUFFER_SIZE?=?1024;
    ??
    static?void?Main()
    ??
    {
    ????
    try
    ????
    {
    ??????
    //?Create?a?new?webrequest?to?the?mentioned?URL.???
    ??????WebRequest?myWebRequest=?WebRequest.Create("http://www.contoso.com");
    ??????
    ??????
    //?Please,?set?the?proxy?to?a?correct?value.?
    ??????WebProxy?proxy=new?WebProxy("myproxy:80");

    ??????proxy.Credentials
    =new?NetworkCredential("srikun","simrin123");
    ??????myWebRequest.Proxy
    =proxy;
    ??????
    //?Create?a?new?instance?of?the?RequestState.
    ??????RequestState?myRequestState?=?new?RequestState();
    ??????
    //?The?'WebRequest'?object?is?associated?to?the?'RequestState'?object.
    ??????myRequestState.request?=?myWebRequest;
    ??????
    //?Start?the?Asynchronous?call?for?response.
    ??????IAsyncResult?asyncResult=(IAsyncResult)?myWebRequest.BeginGetResponse(new?AsyncCallback(RespCallback),myRequestState);
    ??????allDone.WaitOne();
    ??????
    //?Release?the?WebResponse?resource.
    ??????myRequestState.response.Close();
    ??????Console.Read();
    ????}

    ????
    catch(WebException?e)
    ????
    {
    ??????Console.WriteLine(
    "WebException?raised!");
    ??????Console.WriteLine(
    "\n{0}",e.Message);
    ??????Console.WriteLine(
    "\n{0}",e.Status);
    ????}
    ?
    ????
    catch(Exception?e)
    ????
    {
    ??????Console.WriteLine(
    "Exception?raised!");
    ??????Console.WriteLine(
    "Source?:?"?+?e.Source);
    ??????Console.WriteLine(
    "Message?:?"?+?e.Message);
    ????}

    ??}

    ??
    private?static?void?RespCallback(IAsyncResult?asynchronousResult)
    ??
    {??
    ????
    try
    ????
    {
    ??????
    //?Set?the?State?of?request?to?asynchronous.
    ??????RequestState?myRequestState=(RequestState)?asynchronousResult.AsyncState;
    ??????WebRequest??myWebRequest1
    =myRequestState.request;
    ??????
    //?End?the?Asynchronous?response.
    ??????myRequestState.response?=??myWebRequest1.EndGetResponse(asynchronousResult);
    ??????
    //?Read?the?response?into?a?'Stream'?object.
    ??????Stream?responseStream?=?myRequestState.response.GetResponseStream();
    ??????myRequestState.responseStream
    =responseStream;
    ??????
    //?Begin?the?reading?of?the?contents?of?the?HTML?page?and?print?it?to?the?console.
    ??????IAsyncResult?asynchronousResultRead?=?responseStream.BeginRead(myRequestState.bufferRead,?0,?BUFFER_SIZE,?new?AsyncCallback(ReadCallBack),?myRequestState);
    ????
    ????}

    ????
    catch(WebException?e)
    ????
    {
    ??????Console.WriteLine(
    "WebException?raised!");
    ??????Console.WriteLine(
    "\n{0}",e.Message);
    ??????Console.WriteLine(
    "\n{0}",e.Status);
    ????}
    ?
    ????
    catch(Exception?e)
    ????
    {
    ??????Console.WriteLine(
    "Exception?raised!");
    ??????Console.WriteLine(
    "Source?:?"?+?e.Source);
    ??????Console.WriteLine(
    "Message?:?"?+?e.Message);
    ????}

    ??}

    ??
    private?static??void?ReadCallBack(IAsyncResult?asyncResult)
    ??
    {
    ????
    try
    ????
    {
    ??????
    //?Result?state?is?set?to?AsyncState.
    ??????RequestState?myRequestState?=?(RequestState)asyncResult.AsyncState;
    ??????Stream?responseStream?
    =?myRequestState.responseStream;
    ??????
    int?read?=?responseStream.EndRead(?asyncResult?);
    ??????
    //?Read?the?contents?of?the?HTML?page?and?then?print?to?the?console.
    ??????if?(read?>?0)
    ??????
    {
    ????????myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.bufferRead,?
    0,?read));
    ????????IAsyncResult?asynchronousResult?
    =?responseStream.BeginRead(?myRequestState.bufferRead,?0,?BUFFER_SIZE,?new?AsyncCallback(ReadCallBack),?myRequestState);
    ??????}

    ??????
    else
    ??????
    {
    ????????Console.WriteLine(
    "\nThe?HTML?page?Contents?are:??");
    ????????
    if(myRequestState.requestData.Length>1)
    ????????
    {
    ??????????
    string?sringContent;
    ??????????sringContent?
    =?myRequestState.requestData.ToString();
    ??????????Console.WriteLine(sringContent);
    ????????}

    ????????Console.WriteLine(
    "\nPress?'Enter'?key?to?continue..");
    ????????responseStream.Close();
    ????????allDone.Set();
    ??????}

    ????}

    ????
    catch(WebException?e)
    ????
    {
    ??????Console.WriteLine(
    "WebException?raised!");
    ??????Console.WriteLine(
    "\n{0}",e.Message);
    ??????Console.WriteLine(
    "\n{0}",e.Status);
    ????}
    ?
    ????
    catch(Exception?e)
    ????
    {
    ??????Console.WriteLine(
    "Exception?raised!");
    ??????Console.WriteLine(
    "Source?:?{0}"?,?e.Source);
    ??????Console.WriteLine(
    "Message?:?{0}"?,?e.Message);
    ????}


    ??}


    }


    2、自動判斷下載速度的問題,現在回過頭來看看,因為有了微軟的代碼就顯得這個問題比較簡單了,因為我們只要去計算下載指定大小的文件的時間就可以得到;原來我是通過指定時間去計算下載文件的大小,這樣就有兩個問題:一是在多線程的環境中不能很好計算時間,二是這種做法需要增加很多的臨時文件,比較占用空間。

    3、在這個軟件的開發中主要,可以說是大部分工作要使用多線程的工作,所以在線程的管理上就顯得尤為重要,這一點我還沒有完全掌握,經常出現線程執行不能同步的問題,這和我對線程的掌握不深有很大的關系,

    經驗總結:

    ???原來,我一直認為C#的開發語言的掌握是簡單的,但是從這次的多線程管理上的缺陷,就是我深深認為掌握一門語言是需要努力去學習的。我現在只是學習其中的一點皮毛而已,尤其是C++語言我更是覺得無從下手,我只是掌握了這門語言的幾個關鍵詞而已。其中對.NET的掌握是遠遠不夠的,而我現在想掌握的語言是C++,因為我覺得我們現在的系統是建立在C語言上的,尤其是Windows系統更是建立在MFC上的,這就要求我們很好地掌握Win32 API,而這些知識的最佳使用語言就是C++;因此要成為一位好的程序員就要求掌握C++語言。有空的時候一定要好好讀讀C++方面的書籍,回過頭來看C#語言,他就像微軟自己說的那樣,這是一門快速開發語言,它能夠為人們開發一些高級系統的應用軟件而非底層的軟件提供快速開發工具。因為C#將很多底層的操作都封裝了,這就讓我們遠離了系統的底層,使我們的開發速度加快。

    轉載于:https://www.cnblogs.com/hnhls99/archive/2005/07/29/203110.html

    總結

    以上是生活随笔為你收集整理的网络编程 - 异步调用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。