简体   繁体   English

C#(异步/等待)在任务块中循环我的主线程?

[英]C# (async / await) loop in task block my main thread?

private async Task<PortalODataContext> CallConnection(Connection connection)
    {
        bool cancel = false;
        connection.Connected = true;
        var task = getConnection(connection);

        while (!cancel && !task.IsCompleted)
        {
            Thread.Sleep(100);

            if (connection.Disconected)
            {
                cancel = true;
            }
        }

        return await task;
    }

That is my function that I call on the main thread Like so : 这是我在主线程上调用的函数,如下所示:

PortalODataContext portalContext = await this.CallConnection(connectionOpen);

I am new to async and await so I'm just trying to figure out why My task"CallConnection" block my main UI thread ... can you guys help me ? 我是异步的新手,正在等待,所以我只是想弄清楚为什么我的任务“ CallConnection”阻止了我的主UI线程……你们可以帮我吗?

Ohh and there is the GetConnection : 哦,还有GetConnection:

private Task<PortalODataContext> getConnection(Connection connection)
    {            
        return Task.Factory.StartNew(() =>
        {
            try
            {
                var context = connection.ConnectToPortal();
                connection.ListTemplateLib = this.ShellModel.ConnectionManager.GetTemplateLibrarys(connection);
                connection.ListTemplateGrp = this.ShellModel.ConnectionManager.GetTemplateGroups(connection, connection.TemplateLibraryId);
                connection.ListTemplates = this.ShellModel.ConnectionManager.GetTemplates(connection, connection.TemplateGroupId);
                return context;
            }
            catch (Exception)
            {
                throw;
            }
       });

Thanks in advance 提前致谢

Becasue the Thread.Sleep 由于Thread.Sleep

async/await split your method in two, before and after the await . async / await将您的方法在await之前和之后一分为二。 In the first half you have a Thread.Sleep , causing the caller thread to freeze. 在上半部分,您有一个Thread.Sleep ,导致调用方线程冻结。

Use: 采用:

private async Task<PortalODataContext> CallConnection(Connection connection)
{
    bool cancel = false;
    connection.Connected = true;
    var task = getConnection(connection);

    while (!cancel && !task.IsCompleted)
    {
        await Task.Delay(100);

        if (connection.Disconected)
        {
            cancel = true;
        }
    }

    return await task;
}

I am not completely sure what you are trying to achieve here. 我不确定您要在这里实现什么。

When you call an async task , you have to wait for a result. 调用异步任务时,您必须等待结果。

The benefit of asyncs is that you can run two processes in a method, but just inside that method. 异步的好处是,您可以在一个方法中运行两个进程,但是只能在该方法内部运行。 Before you leave that block both processes must be unified again. 在离开该块之前,必须再次统一这两个过程。 That makes you easier to run different processes not having to write delegates and Locks for objects involved. 这使您可以轻松地运行不同的进程,而不必为涉及的对象编写委托和锁。

Take a look here to see what happens in an async methods. 看一下这里 ,看看异步方法会发生什么。 There is a diagram there that can help you to clarify the flow. 那里有一个可以帮助您阐明流程的图表。

In your code you don't need the Threed.Sleep. 在您的代码中,您不需要Threed.Sleep。 The CallConnection method won't return anything until getConnection returns something. 在getConnection返回某些内容之前,CallConnection方法将不返回任何内容。 Then you can continue in your main Thread. 然后,您可以继续执行主线程。

Hope that help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM