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/