[英]What is the best way to await a user response in the latest C# code?
Although the following question has been partly answered a number of times over the past decade, C# has changed significantly in the past few years to include better constructs for handling events, tasks, and the like.尽管以下问题在过去十年中已经部分回答了很多次,但 C# 在过去几年中发生了显着变化,包括用于处理事件、任务等的更好的构造。 What is the current best suggested way to convert the following procedural code into code appropriate for a form, ie, code to avoid busy waiting and keeping the computer responsive?将以下程序代码转换为适合表单的代码(即避免忙等待并保持计算机响应的代码)的当前最佳建议方法是什么? Please provide complete code.请提供完整的代码。 I have found that mere suggestions usually require a lot more follow up for full understanding.我发现仅仅提出建议通常需要更多的跟进才能完全理解。
The form consists of a textbox (NameBox) where the name is displayed and two buttons (ButtonM and ButtonF) labeled "Male" and "Female" which when pressed do the functions described below.该表单由一个显示名称的文本框 (NameBox) 和两个标记为“男性”和“女性”的按钮(ButtonM 和 ButtonF)组成,当按下它们时,它们会执行下面描述的功能。 I've left out any declaration of the buttons since their definition is part of the solution I hope you can provide.我省略了按钮的任何声明,因为它们的定义是我希望你能提供的解决方案的一部分。
public class Question
{
static string[] names = {"Steve", "Lois", "Doug"};
static char[] genders = new char[names.Length];
public static void GetGenders (string[] names)
{
// Something 1
for (int i = 0; i < names.Length; i+) {
NameBox.Text = names[i];
// Wait for user to press Male or Female button;
// if (ButtonM pressed) genders[i] = 'M';
// if (ButtonF pressed) genders[i] = 'F';
}
// Something 2
}
}
Thanks for your time and effort.感谢您的时间和精力。
I wasn't very clear on what I was asking.我不是很清楚我在问什么。 Below is the code I would have used 4 years ago.以下是我 4 年前使用的代码。 My question is "Is there a better way using the current C# language to accomplish this?"我的问题是“有没有更好的方法使用当前的 C# 语言来完成这个?”
public partial class Form1 : Form
{
static string[] names = {"Steve", "Lois", "Doug"};
static char[] genders = new char[names.Length];
public static char ch;
public Form1 ()
{
InitializeComponent();
}
private void ShownForm (object sender, EventArgs e)
{
GetGenders(names);
}
private TaskCompletionSource<bool> clickWaitTask;
public async void GetGenders (string[] names)
{
// Something 1
for (int i = 0; i < names.Length; i++) {
// Create a new TCS so that each time this logic is run,
// it will wait for the click to occur.
clickWaitTask = new TaskCompletionSource<bool>();
textBoxName.Text = names[i];
Application.DoEvents();
// Wait for user to press Male or Female button;
await clickWaitTask.Task;
textBoxResult.Text = ch.ToString();
}
// Something 2
Application.Exit();
}
// This task will only complete when TrySetResult is called
// on the TCS object.
private void ButtonClick (object sender, EventArgs e)
{
ch = (sender == buttonMale ? 'M' : 'F');
clickWaitTask.TrySetResult(true);
}
}
}
I am unhappy with having to create and remove a task for every iteration of the loop.我对必须为循环的每次迭代创建和删除任务感到不满意。 Without involving a third task in the loop, is there a simpler way to just block the looping thread until another thread (the one with the button) can unblock it?在不涉及循环中的第三个任务的情况下,是否有一种更简单的方法来阻塞循环线程,直到另一个线程(带有按钮的线程)可以解除阻塞?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.