Async Await 기능을 배우고 실험하고 있습니다.비동기 대기 및 작업. 내가 잘못하고있는 것
//first select the direcgtory in this event. Then called the async function..to be written
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
foldername = folderBrowserDialog1.SelectedPath;
}
// CreateFilesParellel();
var x = CreateAsync();
다음
같은 기능 CreateAsync는 모습입니다 : 모드 상세 그래서 여기 여기
async Task<int> CreateAsync()
{
string fileType;
List<Task> tasks = new List<Task>();
//get the files in the existing directory.
var files = Directory.GetFiles(foldername);
fileType = Path.GetExtension(files[0]);
var filepath = Path.GetFullPath(files[0]);
string d = Path.GetDirectoryName(files[0]);
var ss = Path.GetFileNameWithoutExtension(files[0]);
var progress = new Progress<string>();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 100000; i++)
{
Action act =() =>
{
File.Copy(files[0], d + "\\" + ss + i.ToString() + fileType);
};
//await File.Copy(files[0], d + "\\" + ss + i.ToString() + fileType);
Task t = new Task(act);
tasks.Add(t);
// t.Start();
progress.ProgressChanged += (s, e) =>
{
textBox1.Text = System.Environment.NewLine;
textBox1.Text = "Creating file = " + e;
};
}
await Task.WhenAll(tasks);
sw.Stop();
string ExecutionTimeTaken = string.Format("Minutes :{0}\nSeconds :{1}\n Mili seconds :{2}", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.TotalMilliseconds);
MessageBox.Show(ExecutionTimeTaken);
return 0;
}
이동이 질문이 코드를 호출하는 버튼 클릭 이벤트가 있습니다. 이 코드를 실행할 때 아무 일도 일어나지 않습니다. 의미 파일이 생성되지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까? 아니면 나는 무엇을 놓치고 있습니까?
http://www.microsoft. com/ko-us/download/details.aspx? id = 19957,이 문서를 검토하십시오. 나는 특히 유용하다는 것을 알았다. – Anupam