C # multi-threaded delegate delegation to read multiple files and display them in the same text box

Source: Internet
Author: User
Tags readfile
A netizen asked: the specified directory contains several small text files, which need to be read using multiple threads. One file, one thread, or a total of 10 threads. Append all the read text to the specified edit box in the window. Only one edit box is written in this box. line feed is optional. I wrote the following solution using the delegate Method: Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; using system. io; using system. threading; namespace multithread {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs E) {using (folderbrowserdialog FBD = new folderbrowserdialog () {FBD. description = "select the path for multi-threaded File Reading"; FBD. shownewfolderbutton = false; If (FBD. showdialog (this) = dialogresult. OK) {directoryinfo di = new directoryinfo (FBD. selectedpath); foreach (fileinfo fi in Di. getfiles ("*. TXT ") {thread t = new thread (this. invokethread); T. start (Fi. fullname) ;}}} private delegate void readfile (Object filepath); Private void invokethread (Object filepath) {If (this. invokerequired) {This. invoke (New readfile (readfilecontent), filepath);} else {readfilecontent (filepath) ;}} private void readfilecontent (Object filepath) {This. textbox1.appendtext (file. readalltext (filepath. tostring (), encoding. default); this. textbox1.appendtext ("\ r \ n ");}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.