Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Asynchronous IO in .NET 4.5

Shulou Source: shulou.com Published: 2022-06-03 05:43:07 11月17日 Update

This article mainly shows you the "sample Analysis of Asynchronous IO in .NET 4.5", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the "sample Analysis of Asynchronous IO in .NET 4.5".

Both traditional winform and wpf may encounter the time of asynchronously manipulating files in Cpact S architecture. It's okay to say that the file is small, write the operation code directly.

If it is a large file, it is often done asynchronously. Display a progress bar on the interface and use a backgroundworker to do it in the background. Here we tell you that asynchronous IO operations are supported in the .NET Framework4.5. The previous asynchronous method code is greatly simplified.

Use backgroundworker code

The code is as follows:

View Code

Private void Button_Click_3 (object sender, RoutedEventArgs e)

{

System.ComponentModel.BackgroundWorker bak = new System.ComponentModel.BackgroundWorker ()

Bak.DoWork + = bak_DoWork

Bak.RunWorkerCompleted + = bak_RunWorkerCompleted

Bak.RunWorkerAsync ()

}

Void bak_DoWork (object sender, System.ComponentModel.DoWorkEventArgs e)

{

String sourceDir = @ "E:\"

String endDir = @ "F:\"

Foreach (string filename in Directory.EnumerateFiles (sourceDir))

{

Using (FileStream SourceStream = File.Open (filename, FileMode.Open))

{

Using (FileStream DestinationStream = File.Create (endDir + filename.Substring (filename.LastIndexOf ('\\)

{

SourceStream.CopyTo (DestinationStream)

}

}

}

}

Void bak_RunWorkerCompleted (object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)

{

MessageBox.Show ("ok")

}

The above should be the most basic operation, is there a lot of code? Look at the way the .NET Framework 4.5 is written.

The copy code is as follows:

Private async void Button_Click_2 (object sender, RoutedEventArgs e)

{

String sourceDir = @ "E:\"

String endDir = @ "F:\"

Foreach (string filename in Directory.EnumerateFiles (sourceDir))

{

Using (FileStream SourceStream = File.Open (filename, FileMode.Open))

{

Using (FileStream DestinationStream = File.Create (endDir + filename.Substring (filename.LastIndexOf ('\\)

{

Await SourceStream.CopyToAsync (DestinationStream)

}

}

}

MessageBox.Show ("ok")

}

The above is all the content of the article "sample Analysis of Asynchronous IO in .NET 4.5". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Code examples analysis content files articles time learning help tradition writing background method easy to understand more organization architecture interface knowledge editing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology MySQL Huawei vpn