为什么我不能在异步方法中调试代码? [英] Why can't I debug code in an async method?

查看:295
本文介绍了为什么我不能在异步方法中调试代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是从那天晚上开始的,试图学习有关MongoDB的更多信息,但是却挂断了电话,并开始使用.NET进行等待/异步操作.我正在尝试实现在MongoDB的站点上显示的代码.我不得不对其稍作修改,以便可以编译我的程序.现在,我的控制台应用程序中包含以下内容:

I actually started the night trying to learn more about MongoDB, but am getting hung up and the .NET await/async stuff. I am trying to implement the code shown on MongoDB's site. I've had to modify it a tad bit, so I could get my program to compile. I now have the following in my console application:

protected static IMongoClient _client;
protected static IMongoDatabase _database;

static void Main(string[] args)
{
    _client = new MongoClient();
    _database = _client.GetDatabase("test");

    GetDataAsync();
}

private static async void GetDataAsync() //method added by me.
{
    int x = await GetData();
}

private static async Task<int> GetData()
{
    var collection = _database.GetCollection<BsonDocument>("restaurants");
    var filter = new BsonDocument();
    var count = 0;
    Func<int> task = () => count; //added by me.
    var result = new Task<int>(task); //added by me.
    using (var cursor = await collection.FindAsync(filter)) //Debugger immediately exits here, goes back to main() and then terminates. 
    {
        while (await cursor.MoveNextAsync())
        {
            var batch = cursor.Current;
            foreach (var document in batch)
            {
                // process document
                count++;
            }
        }
    }

    return count; //added by me
}

当我运行应用程序时,调试器将调用我的GetDataAsync()方法,该方法又调用GetData()方法.它到达using (var cursor = await collection.FindAsync(filter))行,然后立即返回以完成main()方法.

When I run the application, the debugger will call into my GetDataAsync() method which in turn calls into the GetData() method. It gets to the line using (var cursor = await collection.FindAsync(filter)) and then immediately returns to finish the main() method.

我在该行下方放置的任何断点都会被忽略,我在GetDataAsync()方法中放置的任何断点也会被忽略.只是因为程序退出,该代码才不会运行吗?有人可以向我解释发生了什么吗?

Any break points I put below that line are ignored, as are any breakpoints I put in the GetDataAsync() method. Is this code just not getting run because the program exits? Can someone explain to me what is going on?

推荐答案

因为您没有await使用GetDataAsync方法.当到达第一个await时,线程将返回给调用方.由于您不等待任务的完成,因此控制台应用程序将退出,并且不会到达断点.您还需要更新GetDataAsync方法以返回Task而不是void.您不能等待虚空.您应该避免对事件处理程序以外的任何内容使用异步void .

Because you are not awaiting your GetDataAsync method. When the first await is reached the thread is returned to the caller. Since you are not waiting for the completion of the task, your console application exits and your breakpoint is not reached. You will also need to update the GetDataAsync method to return a Task rather than void. You cannot await void. You should avoid using async void for anything other than the event handler.

protected static IMongoClient _client;
protected static IMongoDatabase _database;

static void Main(string[] args)
{
    _client = new MongoClient();
    _database = _client.GetDatabase("test");

    GetDataAsync().Wait(); 
    // Will block the calling thread but you don't have any other solution in a console application
}

private static async Task GetDataAsync() //method added by me.
{
    int x = await GetData();
}

private static async Task<int> GetData()
{
    var collection = _database.GetCollection<BsonDocument>("restaurants");
    var filter = new BsonDocument();
    var count = 0;
    Func<int> task = () => count; //added by me.
    var result = new Task<int>(task); //added by me.
    using (var cursor = await collection.FindAsync(filter)) //Debugger immediately exits here, goes back to main() and then terminates. 
    {
        while (await cursor.MoveNextAsync())
        {
            var batch = cursor.Current;
            foreach (var document in batch)
            {
                // process document
                count++;
            }
        }
    }

    return count; //added by me
}

这篇关于为什么我不能在异步方法中调试代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆