另一个等待不等待

问题描述 投票:0回答:1

通常我不发布,我阅读并找到了我的答案。

但是这一次,尽管事实上我已经找到了关于异步/等待问题的十几个帖子,但似乎没有解决方案适用于我的情况(或者我至少不明白)。

我的代码很长,所以我不要把它整整放在这里,但它看起来像这样:我有一个写在数据库中的任务:

public async Task WatchSpool(Button buttonSend)
{
    LoadOptions();
    //SpoolWatcher spoolWatcher = new SpoolWatcher(this, buttonSend);
    SpoolWatcher spoolWatcher = new SpoolWatcher(this);
    spoolWatcher.OnSpoolWatcherException += OnSpoolWatcherException;
    Task thread = new Task(spoolWatcher.Run);
    spoolWatcherMustStop = false;
    thread.Start();
    //spoolWatcher.Run();
}

然后另一个计算数据库中特定表中的记录数:

public int CountWaitingRecords(string table)
{
    int WaitingRecords = 0;
    try
    {
        using (SqliteConnection connexion = new SqliteConnection("Filename = spool.db"))
        {
            connexion.Open();
            using (SqliteCommand cmd = new SqliteCommand("SELECT COUNT(*) AS Count FROM " + table, connexion))
            {
                SqliteDataReader rs = cmd.ExecuteReader();
                rs.Read();

                WaitingRecords = rs.GetInt32(rs.GetOrdinal("Count"));

                rs.Dispose();
                cmd.Dispose();
                connexion.Dispose();
            }
        }
    }
    catch(Exception ex)
    {
        return -1;
    }
    return WaitingRecords;
}

这两个都在这里被称为:

private async void buttonSendSelectedItems_Click(object sender, RoutedEventArgs e)
{
    try
    {
        //things doing fine here…

        //Start spoolWatcher
        batigest = new Batigest(localFolder + "\\spool.db");
        batigest.OnSpoolWatcherException += new OnSpoolWatcherExceptionHandler(batigest_OnSpoolWatcherException);
        //Alternative code
        //Task.Run(() => batigest.WatchSpool(Envoyer)).Wait();
        //Actual code
        Task task = batigest.WatchSpool(Envoyer);
        await task;
        //Another alternative code
        //task.Wait();

        //Here I count how many rows I've in my table
        LocalDB localDB = new LocalDB(localFolder.Path.ToString() + "\\spool.db");
        int count = localDB.CountWaitingRecords("MouvementStock");
        if (count > 0)
        {
            Envoyer.Content = "Envoyer (" + count + ")";
            Envoyer.IsEnabled = true;
        }
        else
        {
            Envoyer.Content = "Envoyé!";
        }

        //Other things doing fine here…

    }
    catch (Exception ex)
    {
        Envoyer.IsEnabled = true;
        Envoyer.Content = "Erreur!";
        InterpretException("InventoryView2.buttonSendSelectedItems_Click()", ex);
    }
}

问题是:在我开始计算我的数据库中有多少行之前,我应该等待该任务完成。但我不能成功。所以,如果有人能告诉我我做错了什么,我会很感激。

c# uwp async-await task
1个回答
4
投票

这段代码有一些问题:

public async Task WatchSpool(Button buttonSend)
{
    SpoolWatcher spoolWatcher = new SpoolWatcher(this);
    spoolWatcher.OnSpoolWatcherException += OnSpoolWatcherException;
    Task thread = new Task(spoolWatcher.Run);
    thread.Start();
}

首先,Task不是一个线程,因此将其命名为误导和错误。

其次,必须避免使用Task构造函数。假设SpoolWatcher.Run不是Task,你应该使用Task.Run

Task runner = Task.Run(() => spoolWatcher.Run());

第三,也是最重要的一点,你不是因为它完成了awaiting所以WatchSpool几乎会马上回来。

public async Task WatchSpool(Button buttonSend)
{
    SpoolWatcher spoolWatcher = new SpoolWatcher(this);
    spoolWatcher.OnSpoolWatcherException += OnSpoolWatcherException;
    await Task.Run(() => spoolWatcher.Run());
}

一般来说,Task返回方法应该以Async为后缀,所以它应该是WatchSpoolAsync

© www.soinside.com 2019 - 2024. All rights reserved.