我是异步编程的新手。我的用户界面中有一个按钮和文本框。我想单击该按钮,它将使用 FileStream.ReadAsync 方法读取文件,然后它应该在文本框中显示文件的结果。问题是我不想在读取文件时阻止我的 UI。我认为使用 Read 方法应该这样做。但它不起作用。此方法有什么不正确以及如何将 Read 更改为 ReadAsync?
private void Button_Click(object sender, RoutedEventArgs e)
{
string filename = @"D:\Log\log.txt";
byte[] result;
UnicodeEncoding uniencoding = new UnicodeEncoding();
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
result = new byte[SourceStream.Length];
Task<int> tf = new Task<int>(()=> SourceStream.Read(result, 0, (int)SourceStream.Length));
tf.ContinueWith((x) =>
{
try
{
string txt = Encoding.ASCII.GetString(result);
Dispatcher.BeginInvoke((Action)(() => txtBox.Text = txt));
}
catch (Exception ae)
{
MessageBox.Show(ae.Message);
}
});
tf.Start();
}
Run Code Online (Sandbox Code Playgroud) 我有以下清单:
List<DateTime> list =
new [] { "12:00", "12:00", "12:00", "12:30", "12:30", "14:00" }
.Select(DateTime.Parse)
.ToList();
Run Code Online (Sandbox Code Playgroud)
我需要一些可以以这种方式制作此列表的功能:
{ 12:00, 12:01, 12:02, 12:30, 12:31, 14:00 }
Run Code Online (Sandbox Code Playgroud)
所以,如果有相同的,DateTime我应该为每个增加 1 分钟。