C# Forms 在启动时加载 ComboBox 项
C# Forms Loading ComboBox Items on Startup
public Form1()
{
InitializeComponent();
loadComboEmail();
}
private void loadComboEmail()
{
string path = Directory.GetCurrentDirectory();
string build = (path + "\" + "email.txt");
string[] lines = System.IO.File.ReadAllLines(build);
comboEmail.Items.AddRange(lines);
comboEmail.SelectedIndex=0;
}
我有一个组合框,我想用存储在文本文件中的客户端电子邮件地址加载它。使用 loadComboEmail()
我从文本文件中读取并加载 Combobox
。我是否通过在表单启动时调用 loadComboEmail
来做一些被认为是不好的做法?如果是这样,我如何正确地从文本文件中读取,然后将其加载到组合中,以便在加载表单时,它具有必要的数据?
不,这似乎很合法。您不必担心,因为这是在 WinForms
中加载所有内容的方式...这将在短时间内阻塞 UI
线程,但由于您不会加载大量内容大量的东西,你甚至不会注意到它。
当您开始执行大量操作和大文件时,您应该考虑使用 BackgroundWorker
或 Task
!
无论如何,您还应该考虑使用以下代码代替您的代码:
private void loadComboEmail()
{
string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath); //seems to be the same to me, but is safer
string build = System.IO.Path.Combine(path, "email.txt"); //much more safer then simple string addition
string[] lines = System.IO.File.ReadAllLines(build);
comboEmail.Items.AddRange(lines);
comboEmail.SelectedIndex = 0;
}
您可以使用:
Task task = Task.Factory.StartNew(() =>
{
// Background work
loadComboEmail();
}).ContinueWith((t) =>
{
//If you need to execute something after the loading process.
});
为了更新 UI 线程,您可以在另一个线程上进行阅读,当加载电子邮件列表时,只需更新它即可。
Task.Factory.StartNew(() =>
{
// Background work - read the file
}).ContinueWith((t) => {
// Update UI thread here
}, TaskScheduler.FromCurrentSynchronizationContext());
使用构造函数加载不被视为不良做法。阅读 Hans Passant 关于何时应该使用 window 的加载事件的回答。 What setup code should go in Form Constructors versus Form Load event?
尽管如评论中所述,您正在阻止 UI 线程。在构造函数中不能使用关键字 await。所以你必须'Fire and forget'。使用 Load
事件时,您可以使用 await
,但事件处理程序是 async void
。所以他们没有等待,你还有 'Fire and forget'.
public Form1()
{
InitializeComponent();
loadComboEmail();
}
private async Task loadComboEmail()
{
string path = Directory.GetCurrentDirectory();
string build = (path + "\" + "email.txt");
string[] lines = await Task.Run(() => File.ReadAllLines(build));
comboEmail.Items.AddRange(lines);
comboEmail.SelectedIndex=0;
}
public Form1()
{
InitializeComponent();
loadComboEmail();
}
private void loadComboEmail()
{
string path = Directory.GetCurrentDirectory();
string build = (path + "\" + "email.txt");
string[] lines = System.IO.File.ReadAllLines(build);
comboEmail.Items.AddRange(lines);
comboEmail.SelectedIndex=0;
}
我有一个组合框,我想用存储在文本文件中的客户端电子邮件地址加载它。使用 loadComboEmail()
我从文本文件中读取并加载 Combobox
。我是否通过在表单启动时调用 loadComboEmail
来做一些被认为是不好的做法?如果是这样,我如何正确地从文本文件中读取,然后将其加载到组合中,以便在加载表单时,它具有必要的数据?
不,这似乎很合法。您不必担心,因为这是在 WinForms
中加载所有内容的方式...这将在短时间内阻塞 UI
线程,但由于您不会加载大量内容大量的东西,你甚至不会注意到它。
当您开始执行大量操作和大文件时,您应该考虑使用 BackgroundWorker
或 Task
!
无论如何,您还应该考虑使用以下代码代替您的代码:
private void loadComboEmail()
{
string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath); //seems to be the same to me, but is safer
string build = System.IO.Path.Combine(path, "email.txt"); //much more safer then simple string addition
string[] lines = System.IO.File.ReadAllLines(build);
comboEmail.Items.AddRange(lines);
comboEmail.SelectedIndex = 0;
}
您可以使用:
Task task = Task.Factory.StartNew(() =>
{
// Background work
loadComboEmail();
}).ContinueWith((t) =>
{
//If you need to execute something after the loading process.
});
为了更新 UI 线程,您可以在另一个线程上进行阅读,当加载电子邮件列表时,只需更新它即可。
Task.Factory.StartNew(() =>
{
// Background work - read the file
}).ContinueWith((t) => {
// Update UI thread here
}, TaskScheduler.FromCurrentSynchronizationContext());
使用构造函数加载不被视为不良做法。阅读 Hans Passant 关于何时应该使用 window 的加载事件的回答。 What setup code should go in Form Constructors versus Form Load event?
尽管如评论中所述,您正在阻止 UI 线程。在构造函数中不能使用关键字 await。所以你必须'Fire and forget'。使用 Load
事件时,您可以使用 await
,但事件处理程序是 async void
。所以他们没有等待,你还有 'Fire and forget'.
public Form1()
{
InitializeComponent();
loadComboEmail();
}
private async Task loadComboEmail()
{
string path = Directory.GetCurrentDirectory();
string build = (path + "\" + "email.txt");
string[] lines = await Task.Run(() => File.ReadAllLines(build));
comboEmail.Items.AddRange(lines);
comboEmail.SelectedIndex=0;
}