射击顺序问题

Firing order issue

我有一个问题,即某行代码在它应该执行之前就被触发了(至少在我看来是这样)。有问题的代码是下面按钮单击事件中代码的第 3 行。我不明白这是为什么。

代码的第 2 行将事件发送到关联的 Presenter,在 Presenter 事件中的代码执行完毕之前,第 3 行被触发。

也许我遗漏了一些明显的东西,但想不出是什么。

private void btnGetData_Click(object sender, EventArgs e)
{
    this.statusIndicatorLabel.Text = "Processing...";
    this.GetDataButtonClick(this, e);
    this.statusIndicatorLabel.Text = "Processing complete";
}

我在这里缺少什么?

编辑:

GetDataButtonClick 的定义是:

private async void _mainView_GetDataButtonClick(object sender, EventArgs e)
{
}

您在评论中说 GetDataButtonClick 是一种 async Task 方法。在这种情况下:

  1. GetDataButtonClick 重命名为 GetDataButtonClickAsync 因为在 .NET 中异步工作并将其 return 值表示为 Task 的方法应该具有该方法名称后缀 Async 让阅读您的代码的人清楚地知道它是一个 non-blocking TPL 方法。

  2. btnGetData_Click 更改为 async void 方法。请注意,event-handlers 是(通常) 只有 可以接受 async void 而不是 async Task 的时间。这也意味着您不应该直接调用 btnGetData_Click:只能将它用作 fire-and-forget 调用,因为调用者没有关于该方法最终完成的信息(并且您不能使用 async Task 因为 System.EventHandler 需要 return 类型的 void.

  3. 添加 await,但不要添加 .ConfigureAwait(false),因为在 GetDataButtonClick 完成后应在 UI 线程上恢复执行。

像这样:

private async void btnGetData_Click(Object sender, EventArgs e)
{
    this.statusIndicatorLabel.Text = "Processing...";
    await this.GetDataButtonClickAsync(this, e);
    this.statusIndicatorLabel.Text = "Processing complete";
}