【问题标题】:How to know if Task or Void method is still running when using Async使用 Async 时如何知道 Task 或 Void 方法是否仍在运行
【发布时间】:2013-01-24 11:36:03
【问题描述】:

我正在尝试使用异步和等待,我的问题是它不会等待后台进程完成。也许您想知道为什么我不同步运行应用程序? 我正在尝试尽快完成我的部分任务,其余的可以等待,如本例所示。 谢谢你们的帮助! =)

class Program
{
    static void Main(string[] args)
    {
        Run();
        //Problem or Maybe Not? Needs this 
        //So that my application won't close immediately
        Console.ReadLine();
    }

    private async static void Run()
    {
        Task<bool> TBool = ProcessRecords();

        if (await TBool)
        {
            //Problem #1 This Line Doesn't Show
            Console.WriteLine("End of Code");
            //SHould be safe to close the application by Now
            //But goes through here not waiting for the return
            //of the last process.

            Environment.Exit(0);

            //My temporary solution is to indicate a Task.Delay(80000)
            //to make sure that all the logging in the background
            //are all done. I dont know if there is a function that shows
            //that there are task running on the background or any 
            //other workaroung will help. =) thanks
        }

    }

    private async static Task<bool> ProcessRecords()
    {
        Task newTask = null;
        //Loop through all the records and send
        //all the records to MQ ASAP
        for (int x = 0; x < 10; x++)
        {
            //I wont wait for this Task so
            //I can send the next record
            newTask = SendToMQ(x);
        }
        //I only need to wait for the last Task to
        //indicate that I can exit the system
        //as soon as it finish
        //Problem #2 The Console.WriteLine doesnt show the last record.
        await newTask;
        return true;
    }

    private async static Task SendToMQ(int count)
    {
        //actual sending of message (Important)
        await Task.Delay(1000);
        //Process of Logging Connect to DB etc, (Not so Important, but takes most of the time)
        await LoggingRecord();
        Console.Clear();
        Console.WriteLine("Done Processing  " + count.ToString() + " records");
    }

    //Logging of each record
    private async static Task LoggingRecord()
    {
        await Task.Delay(5000);
        //Problem #3 This Line Doesn't Show
        Console.WriteLine("Last Log Finished");
    }
}

【问题讨论】:

  • 你不能设置一些布尔标志或分配并检查一些属性..吗?当您单步执行代码时会发生什么是 Last Log Finished 写入控制台主窗口..?
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 显然 Logging 还没有完成他的工作,但是即使 Logging 尚未完成写入,应用程序也会强制关闭整个应用程序,这就是为什么我给我的应用程序 Task.Delay(8000) 到等待它记录,但这不仅仅是好的编码使它不是动态的。感谢所有的帮助 =)

标签: c# asynchronous task async-await multitasking


【解决方案1】:

您应该尽可能使用 await:

await Run();

但是,在这种情况下你不能,所以你必须使用 Wait

static void Main(string[] args)
{
    Run().Wait();

    //Problem or Maybe Not? Needs this 
    //So that my application won't close immediately
    //Console.ReadLine();
}

// note the return type is Task
private async static Task Run()
{
...
}

在 ProcessRecords() 中有以下行 - 我不太清楚你的意思,所以我没有解决它:

//Problem #2 The Console.WriteLine doesnt show the last record.

上面打印出来

Done Processing  9 records
End of Code

【讨论】:

  • 谢谢,我很想用 void 而不是它应该是 Task,void 没有 Wait()。感谢您的新见解 =)。
  • 虽然这是控制台应用程序的正确解决方案,但在 GUI 应用程序或 ASP.NET 中使用 Wait() 时要非常小心,它几乎总是会导致死锁。
猜你喜欢
  • 2022-10-11
  • 2013-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多