In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.