等待时出了什么问题

等待时出了什么问题

本文介绍了我在使用异步/等待时出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基本上可以为我玩游戏的应用程序.它形成团队,加入团队,然后发起团队.我正在尝试异步执行所有操作.

I'm working on creating an application that basically plays a game for me. It forms raids, joins the raids, and then launches the raid. I'm trying to do this all asynchronously.

某些背景:FormRaidAsync,JoinRaidAsync和LaunchRaidAsync都发出Web请求.这些方法本身也设置为异步的,但是当我运行该程序时,它仅以大约2-3个帐户/秒的速度加入.

Some background: FormRaidAsync, JoinRaidAsync, and LaunchRaidAsync all make web requests. The methods themselves are set up to be asynchronous as well, but when I run the program, it's only joining about 2-3 accounts/second.

我是在做错什么,还是async/await不一定在新线程上运行每个请求?在这种情况下,如何调整此代码以接近10/秒的速度加入帐户?我是否必须使用其他形式的多线程来一次发出更多请求?

Am I doing something wrong, or does async/await not necessarily run each request on a new thread? If that's the case, how can I tweak this code to join the accounts at a rate closer to 10/second? Will I have to use some other form of multi-threading to make more requests at once?

谢谢大家.让我知道是否需要更多细节.

Thanks everyone. Let me know if there's more detail needed.

public async Task<string> StartRaidAsync()
{
    string raid_id = String.Empty;

    try
    {
        raid_id = await this.Helper.FormRaidAsync(this.TargetRaid.Id, this.Former.Id, this.TargetRaid.IsBossRaid).ConfigureAwait(false);
        Console.WriteLine("Formed raid with {0}.", this.Former.Name);

        List<Task> joinTasks = new List<Task>();

        foreach (var joiner in this.Joiners)
        {
            try
            {
                joinTasks.Add(this.Helper.JoinRaidAsync(raid_id, joiner.Id));
            }
            catch (Exception)   // Not sure which exceptions to catch yet.
            {
                Console.WriteLine("Error joining {0}. Skipped.", joiner.Name);
            }
        }

        Task.WaitAll(joinTasks.ToArray());

        await this.Helper.LaunchRaidAsync(raid_id, this.Former.Id).ConfigureAwait(false);
        Console.WriteLine("{0} launched raid.", this.Former.Name);
    }
    catch (Exception)   // Not sure which exceptions to catch yet.
    {
        return "ERROR";
    }

    return raid_id;
}

内部JoinRaidAsync:

Inside JoinRaidAsync:

public async Task JoinRaidAsync(string raid_id, string suid)
{
    var postUrl = "some url";
    var postData = "some data";

    await this.Socket.PostAsync(postUrl, postData).ConfigureAwait(false);
    Console.WriteLine("Joined {0}.", suid);
}

内部Socket.PostAsync:

Inside Socket.PostAsync:

public async Task<string> PostAsync(string url, string postData)
{
    return await SendRequestAsync(url, postData).ConfigureAwait(false);
}

内部SendRequestAsync:

Inside SendRequestAsync:

protected virtual async Task<string> SendRequestAsync(string url, string postData)
{
    for (int i = 0; i < 3; i++)
    {
        try
        {
            HttpWebRequest request = this.CreateRequest(url);

            if (!String.IsNullOrWhiteSpace(postData))
            {
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;
                var stream = await request.GetRequestStreamAsync().ConfigureAwait(false);

                using (StreamWriter writer = new StreamWriter(stream))
                {
                    await writer.WriteAsync(postData).ConfigureAwait(false);
                    await writer.FlushAsync().ConfigureAwait(false);
                }
            }

            using (HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync().ConfigureAwait(false)))
            {
                string responseString = String.Empty;

                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    responseString = await reader.ReadToEndAsync().ConfigureAwait(false);
                }

                return responseString;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    return String.Empty;
}

推荐答案

async/await不会创建新线程.

异步代码可以一次执行多个请求.当然,这些请求的速度取决于Web服务调用的速度.

Asynchronous code can do multiple requests at once. The speed of those requests, of course, is determined by the speed of the web service calls.

我现在在代码中唯一看到的错误是混合了阻塞代码和异步代码.替换:

The only thing I see wrong in the code right now is that it is mixing blocking and asynchronous code. Replace:

Task.WaitAll(joinTasks.ToArray());

具有:

await Task.WhenAll(joinTasks.ToArray());

如果仍然遇到问题,则可能是由于您的Web服务或其他支持代码(例如JoinRaidAsync中的代码).

If you're still seeing problems, it's likely due to your web services or other supporting code (such as the code within JoinRaidAsync).

这篇关于我在使用异步/等待时出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:32