C# - 文本框不需要等到最后?
C# - textbox don't need to wait until the end?
我有这个代码:
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
logBox.Text += "My text";
try
{
ping thing;
}
catch
{
}
}
如何在尝试之前显示 logBox.Text +=?实际上,需要等待线程结束才能在我的日志框中显示文本...
我不想等待应用程序执行尝试并在我单击按钮时直接显示 logBox.Text。
谢谢你的帮助。
您可以使用 Task.Factory 来实现。来自 docs 的示例:
using System;
using System.IO;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
Task[] tasks = new Task[2];
String[] files = null;
String[] dirs = null;
String docsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
tasks[0] = Task.Factory.StartNew( () => files = Directory.GetFiles(docsDirectory));
tasks[1] = Task.Factory.StartNew( () => dirs = Directory.GetDirectories(docsDirectory));
Task.Factory.ContinueWhenAll(tasks, completedTasks => {
Console.WriteLine("{0} contains: ", docsDirectory);
Console.WriteLine(" {0} subdirectories", dirs.Length);
Console.WriteLine(" {0} files", files.Length);
} );
}
}
// The example displays output like the following:
// C:\Users\<username>\Documents contains:
// 24 subdirectories
// 16 files
只需将您创建的任务放入您正在使用的事件处理程序中即可。
将您的代码修改为:
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
logBox.Text += "My text";
logBox.Refresh();//This will display the modifications
try
{
ping thing;
}
catch
{
}
}
注意:
不推荐此答案解决问题的方式。这并不能真正使您的代码响应迅速,并且在某些边缘情况下也不能保证输出。一般来说,不建议使用这里推荐的方式。
使用后台线程或任务是更好的方法。其他答案中已经解释了任务方式。另外,有问题的评论也有相同的解释。
我正在复制一些评论(因为评论是第二 class 成员):
If you want the UI thread to update before "ping thing" completes, you can't do "ping thing" on the UI thread. Do it in a Task. – 15ee8f99-57ff-4f92-890c-b56153
..........and that long-running tasks like this should generally be done on a background thread. – Cody Gray
Hanging the UI is an anti-pattern too. – 15ee8f99-57ff-4f92-890c-b56153
Any piece of code that you know is going to take at least a few seconds, should be done asynchronously so that you don't block the UI thread (you'll know when this happens because your program will freeze). Give this a read: msdn.microsoft.com/library/hh191443(vs.110).aspx – ThePerplexedOne
我有这个代码:
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
logBox.Text += "My text";
try
{
ping thing;
}
catch
{
}
}
如何在尝试之前显示 logBox.Text +=?实际上,需要等待线程结束才能在我的日志框中显示文本...
我不想等待应用程序执行尝试并在我单击按钮时直接显示 logBox.Text。
谢谢你的帮助。
您可以使用 Task.Factory 来实现。来自 docs 的示例:
using System;
using System.IO;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
Task[] tasks = new Task[2];
String[] files = null;
String[] dirs = null;
String docsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
tasks[0] = Task.Factory.StartNew( () => files = Directory.GetFiles(docsDirectory));
tasks[1] = Task.Factory.StartNew( () => dirs = Directory.GetDirectories(docsDirectory));
Task.Factory.ContinueWhenAll(tasks, completedTasks => {
Console.WriteLine("{0} contains: ", docsDirectory);
Console.WriteLine(" {0} subdirectories", dirs.Length);
Console.WriteLine(" {0} files", files.Length);
} );
}
}
// The example displays output like the following:
// C:\Users\<username>\Documents contains:
// 24 subdirectories
// 16 files
只需将您创建的任务放入您正在使用的事件处理程序中即可。
将您的代码修改为:
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
logBox.Text += "My text";
logBox.Refresh();//This will display the modifications
try
{
ping thing;
}
catch
{
}
}
注意:
不推荐此答案解决问题的方式。这并不能真正使您的代码响应迅速,并且在某些边缘情况下也不能保证输出。一般来说,不建议使用这里推荐的方式。
使用后台线程或任务是更好的方法。其他答案中已经解释了任务方式。另外,有问题的评论也有相同的解释。
我正在复制一些评论(因为评论是第二 class 成员):
If you want the UI thread to update before "ping thing" completes, you can't do "ping thing" on the UI thread. Do it in a Task. – 15ee8f99-57ff-4f92-890c-b56153
..........and that long-running tasks like this should generally be done on a background thread. – Cody Gray
Hanging the UI is an anti-pattern too. – 15ee8f99-57ff-4f92-890c-b56153
Any piece of code that you know is going to take at least a few seconds, should be done asynchronously so that you don't block the UI thread (you'll know when this happens because your program will freeze). Give this a read: msdn.microsoft.com/library/hh191443(vs.110).aspx – ThePerplexedOne