多线程 //例子
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 = "选择要多线程读取文件的路径";
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");
}
}
}
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 = "选择要多线程读取文件的路径";
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");
}
}
}
public delegate void Callback(string str);
//.....class
private void Readfile(object filepath)
{
if (textBox2.InvokeRequired)
{
Callback call = new Callback(Readfile);
this.BeginInvoke(call,new object[]{filepath});//异步执行
}
else
{
this.textBox2.AppendText(File.ReadAllText(filepath.ToString(), Encoding.Default));
this.textBox2.AppendText("\r\n");
}
}
private void button1_Click(object sender, EventArgs e)
{ using (OpenFileDialog fbd = new OpenFileDialog())
{
fbd.Filter = "(*.*)|*.txt";
if (fbd.ShowDialog(this) == DialogResult.OK)
{
Thread t = new Thread(new ParameterizedThreadStart(Readfile));//开启线程
t.Start(fbd.FileName);
}
}
}
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K
using (OpenFileDialog fbd = new OpenFileDialog())
{
fbd.Filter = "(*.*)|*.txt";
if (fbd.ShowDialog(this) == DialogResult.OK)
{
byte[] bytcontent = new byte[ChunkSize];
FileStream fs = new FileStream(fbd.FileName,FileMode.Open);
long dataLengthToRead = fs.Length;//获取下载的文件总大小 while (dataLengthToRead > 0)
{
int lengthRead = fs.Read(bytcontent, 0, Convert.ToInt32(ChunkSize));//读取的大小
Thread t = new Thread(new ParameterizedThreadStart(Readfile));
t.Start(System.Text.Encoding.Default.GetString(bytcontent));
dataLengthToRead -= lengthRead;
}
}
}}
public delegate void Callback(string str); private void Readfile(object content)
{
if (richTextBox1.InvokeRequired)
{
Callback call = new Callback(Readfile);
this.BeginInvoke(call, new object[] { content });
}
else
{
this.richTextBox1.AppendText(content.ToString());
this.richTextBox1.AppendText("\r\n");
}
}