using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
async void ExecuteTasks()
{
string filePath = @"d:\1.csv";
Task<string> task = OpenFileAsync(filePath);
for (int i = 0; i < 20; i++)
{
textBox1.Text = i.ToString();
System.Threading.Thread.Sleep(1000);
Application.DoEvents();
}
string s = await task;
MessageBox.Show(s);
}
async Task<string> OpenFileAsync(string filePath)
{
Console.WriteLine("OK");
StringBuilder sb = new StringBuilder();
Console.WriteLine("Start to read file");
using (StreamReader sr = new StreamReader(filePath))
{
var line = await sr.ReadLineAsync();
while(line!=null)
{
sb.Append(line);
//System.Threading.Thread.Sleep(1000);
line = await sr.ReadLineAsync();
}
}
Console.WriteLine("read file complete");
return sb.ToString();
}
private void Button1_Click(object sender, EventArgs e)
{
ExecuteTasks();
}
}
}