加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

运行时错误和程序退出时使用 – 异步和等待使用C#

发布时间:2020-12-15 23:39:40 所属栏目:百科 来源:网络整理
导读:我试图在我的程序中使用async的概念并等待.该程序突然退出.我试图从几个随机网址获取内容长度并处理它并显示每个网址的字节大小. 码: using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Th
我试图在我的程序中使用async的概念并等待.该程序突然退出.我试图从几个随机网址获取内容长度并处理它并显示每个网址的字节大小.

码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace TestProgram
{
    public class asyncclass
    {

        public async void  MainCall() {

             await SumPageSizes();

        }

        public async Task SumPageSizes(){

            List<string> urllist = GetUrlList();

            foreach (var url in urllist)
            {
                byte[] content = await GetContent(url);
                Displayurl(content,url);

            }


        }

        private void Displayurl(byte[] content,string url)
        {
            var length = content.Length;
            Console.WriteLine("The bytes length for the url response " + url + " is of :" +length );
        }

        private async Task<byte[]> GetContent(string url)
        {
            var content = new MemoryStream();

            try
            {


                var obj = (HttpWebRequest)WebRequest.Create(url);

                WebResponse response = obj.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    await stream.CopyToAsync(content);

                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);

            }

            return content.ToArray();
        }

        private List<string> GetUrlList()
        {
            var urllist = new List<string>(){
                "http://msdn.microsoft.com/library/windows/apps/br211380.aspx","http://msdn.microsoft.com","http://msdn.microsoft.com/en-us/library/hh290136.aspx","http://msdn.microsoft.com/en-us/library/ee256749.aspx","http://msdn.microsoft.com/en-us/library/hh290138.aspx","http://msdn.microsoft.com/en-us/library/hh290140.aspx","http://msdn.microsoft.com/en-us/library/dd470362.aspx","http://msdn.microsoft.com/en-us/library/aa578028.aspx","http://msdn.microsoft.com/en-us/library/ms404677.aspx","http://msdn.microsoft.com/en-us/library/ff730837.aspx"
            };
            return urllist;
        }
    }
}

主要

public static void Main(string[] args)
        {
            asyncclass asyncdemo = new asyncclass();
            asyncdemo.MainCall();

        }

解决方法

问题是您没有等待异步方法,因此应用程序在方法结束之前退出.

在c#7中,您可以创建一个async entry point,它允许您使用await关键字.

public static async Task Main(string[] args)
{
    asyncclass asyncdemo = new asyncclass();
    await asyncdemo.MainCall();
}

如果要从MainCall中冒出异常,则需要将返回类型更改为“任务”.

public async Task MainCall() 
{
    await SumPageSizes();
}

如果您想在c#7之前运行代码异步,则可以执行以下操作.

public static void Main(string[] args)
{
    asyncclass asyncdemo = new asyncclass();
    asyncdemo.MainCall().Wait();
    // or the following line if `MainCall` doesn't return a `Task`
    //Task.Run(() => MainCall()).Wait();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读