Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Asynchronous IO in .NET 4.5

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report