Winform 多线程 文件操作
来源:互联网 发布:js push(obj) 编辑:程序博客网 时间:2024/05/22 10:23
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading;using System.Windows.Forms;namespace ThreadAndDelegate{ /* Winform 中使用多线程实现文件复制操作,其中涉及到控件跨线程操作。 */ public delegate void SetControlEnable(); public delegate void SetProgressBarValue(int value); public delegate void SetProgressBarMaxValue(int value); public partial class FileOperation_Thread : Form { long blockSize = -1; string filePath = string.Empty; public FileOperation_Thread() { InitializeComponent(); } private void FileOperation_Thread_Load(object sender, EventArgs e) { this.comboBox1.SelectedIndex = 0; blockSize = Convert.ToInt64(this.comboBox1.Text); this.btnCopy.Enabled = false; } private void btnSelectFile_Click(object sender, EventArgs e) { openFileDialog1.Multiselect = false; openFileDialog1.Title = "选择文件"; DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { this.txtBoxSource.Text = openFileDialog1.FileName; } } private void btnFolder_Click(object sender, EventArgs e) { DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { txtBoxDest.Text = folderBrowserDialog1.SelectedPath; } } private void btnCopy_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtBoxSource.Text)) { MessageBox.Show("未选择源文件"); return; } if (string.IsNullOrWhiteSpace(txtBoxDest.Text)) { MessageBox.Show("未指定目录"); return; } Thread threadOne = new Thread(RunCopy); threadOne.Start(); } private void txtBoxSource_TextChanged(object sender, EventArgs e) { TextBox tb = sender as TextBox; if (string.IsNullOrWhiteSpace(tb.Text)) { this.btnCopy.Enabled = false; return; } this.btnCopy.Enabled = true; } private void RunCopy() { if (!Directory.Exists(this.txtBoxDest.Text.Trim())) { Directory.CreateDirectory(this.txtBoxDest.Text.Trim()); } string sourcePath = this.txtBoxSource.Text.Trim(); string destPath = this.txtBoxDest.Text.Trim(); string fileName = sourcePath.Substring(sourcePath.LastIndexOf("\\") + 1); byte[] content = new byte[blockSize]; FileStream readerStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); FileStream writerStream = new FileStream(destPath + "\\" + fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); long loop = readerStream.Length / blockSize; long r = readerStream.Length % blockSize; int ProcessMax = (int)loop; if (InvokeRequired) { this.Invoke(new SetProgressBarMaxValue(SetMaxValue), r != 0 ? ++ProcessMax : ProcessMax); } else { this.progressBar1.Value = r != 0 ? ++ProcessMax : ProcessMax; } for (int i = 0; i < loop; i++) { readerStream.Read(content, 0, content.Length); writerStream.Write(content, 0, content.Length); try { if (InvokeRequired) { this.Invoke(new SetProgressBarValue(SetEnable), i + 1); } else { this.progressBar1.Value = i + 1; } } catch { Thread.CurrentThread.Abort(); } } if (r != 0) { long remain = readerStream.Length - loop * blockSize; content = new byte[remain]; readerStream.Read(content, 0, content.Length); writerStream.Write(content, 0, content.Length); try { if (InvokeRequired) { this.Invoke(new SetProgressBarValue(SetEnable), Convert.ToInt32(loop + 1)); } else { this.progressBar1.Value = Convert.ToInt32(loop + 1); } } catch { Thread.CurrentThread.Abort(); } } readerStream.Flush(); writerStream.Flush(); readerStream.Close(); writerStream.Close(); DialogResult result = MessageBox.Show("拷贝成功!"); if (result == DialogResult.OK) { if (InvokeRequired) { this.Invoke(new SetControlEnable(SetEnable)); } else { this.btnSearch.Enabled = true; } Thread.CurrentThread.Abort(); } } private void SetMaxValue(int value) { this.progressBar1.Maximum = value; progressBar1.Visible = true; } private void SetEnable(int value) { progressBar1.Value = value; if (progressBar1.Value == progressBar1.Maximum) { Thread.Sleep(1000); progressBar1.Visible = false; } } private void SetEnable() { this.btnSearch.Enabled = true; filePath = this.txtBoxDest.Text.Trim(); } private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { blockSize = Convert.ToInt64(this.comboBox1.Text); } private void btnSearch_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start(filePath); } }}
设计效果:
运行效果:
0 0
- Winform 多线程 文件操作
- WinForm 多线程操作UI
- winform操作Excel文件
- C#WinForm文件操作
- WinForm 文件操作
- C#文件操作----winform
- C# winform操作网页文件
- C#Winform文件操作总结
- Winform开发TreeView不能多线程操作
- Winform控件多线程操作控件的解决方案
- c# winform 多线程操作统一对象(lock)
- OpenMP: 多线程文件操作
- 多线程写文件操作
- winform 多线程执行操作,并跨线程操作控件
- WinForm多线程
- WinForm多线程
- Winform多线程
- winform多线程
- LingPipe's Competition
- RMAN SET NEWNAME
- Evaluate Reverse Polish Notation
- Linux下environ环境变量操作函数
- linux服务器如何检查远程用户登陆状态、时间、ip地址等信息
- Winform 多线程 文件操作
- iOS读取和显示PDF文档
- hdu 1846 (博弈)
- oracle-开窗函数-求最近7天平均值
- Nsis 宏定义及调用方法
- 调用系统相机的方式
- 【BZOJ】】【P3529】【Sdoi2014】【数表】【题解】【莫比乌斯反演+离线+树状数组】
- ios,android读取文件
- Unity 解决 动态设置Lightmap 贴图会歪的问题