C # multithreaded delegate delegate way to read multiple files to the same text box display

Source: Internet
Author: User
Tags readfile thread

Today, there is a netizen, ask:

There are several small text files in the specified directory, and you now need to use multithreading for reading.

A file can be a thread or set of 10 threads in a way.

Append all the read text to the specified edit box in the window, and only one edit box is written in this, in this case, in the order of wrapping.

I wrote the following solution in the form of a delegate:

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 Button1_Click (object sender, EventArgs e)
{
using (FolderBrowserDialog FBD = new FolderBrowserDialog ())
{
Fbd. Description = "Select the path to read the file multiple threads";
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");
}
}
}

Put it here, give you a reference.

Source: http://www.zu14.cn/

Related Article

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.