各位,以下代码实现的是从Y文件夹复制bmp文件到YTest文件夹。
而复制的过程中会实时的通过progressbar展现,功能上没有什么不正确,但效果上有点小问题。
当文件数目过大时,progressBar1.PerformStep();会占用到UI主线程,导致看着有点卡,或者说有点像死了的感觉。
是不是通过多线程能解决此问题呢,希望各位大牛不吝赐教,感激不尽啊!
progressBar1.Visible = true;
string directorySource = Application.StartupPath + "\\Y";
string directoryDest = Application.StartupPath + "\\YTest";
string[] filePathSource = Directory.GetFiles(directorySource, "*.bmp");
string[] filePathDest = Directory.GetFiles(directoryDest); progressBar1.Minimum = 1;
progressBar1.Maximum = filePathSource.Length;
if (filePathDest.Length > 0)
{
for (int i = 0; i < filePathDest.Length; i++)
{
File.Delete(filePathDest[i]);
}
}
for (int i = 0; i < filePathSource.Length; i++)
{
File.Copy(filePathSource[i],
directoryDest + "\\" + Path.GetFileNameWithoutExtension(filePathSource[i]) + ".bmp");
progressBar1.PerformStep();
Console.WriteLine(i);
}
progressBar1.Visible = false;
而复制的过程中会实时的通过progressbar展现,功能上没有什么不正确,但效果上有点小问题。
当文件数目过大时,progressBar1.PerformStep();会占用到UI主线程,导致看着有点卡,或者说有点像死了的感觉。
是不是通过多线程能解决此问题呢,希望各位大牛不吝赐教,感激不尽啊!
progressBar1.Visible = true;
string directorySource = Application.StartupPath + "\\Y";
string directoryDest = Application.StartupPath + "\\YTest";
string[] filePathSource = Directory.GetFiles(directorySource, "*.bmp");
string[] filePathDest = Directory.GetFiles(directoryDest); progressBar1.Minimum = 1;
progressBar1.Maximum = filePathSource.Length;
if (filePathDest.Length > 0)
{
for (int i = 0; i < filePathDest.Length; i++)
{
File.Delete(filePathDest[i]);
}
}
for (int i = 0; i < filePathSource.Length; i++)
{
File.Copy(filePathSource[i],
directoryDest + "\\" + Path.GetFileNameWithoutExtension(filePathSource[i]) + ".bmp");
progressBar1.PerformStep();
Console.WriteLine(i);
}
progressBar1.Visible = false;
解决方案 »
- C# 回发页面不用static怎么写???大虾进,谢谢!!
- 在server2000中创建链接服务器访问oracle数据库
- c# 如何将ppt中的每一页保存为一张张的图片?
- DataGridView 编辑状态时的按键事件。
- 关于SourceColumn迷惑
- Global.asax文件在发布网站后没有用了 怎么办?
- 本人最近在用c#做一个类似windows 优化大师一类的工具,但是,对C#在这方面的编程不熟悉,请高手多多提供方法?
- 续:来,做道题目试试看,看看自己是不是高手
- 如何从相对路径获得绝对路径?
- 用C#如何实现菜单条中“查找”,“替换”,“查找下一个”的功能?
- 有谁用过xeccd这个画图控件,进来指导下啊
- winform程序客户端报错
string directorySource = Application.StartupPath + "\\Y";
string directoryDest = Application.StartupPath + "\\YTest";
string[] filePathSource = Directory.GetFiles(directorySource,
"*.bmp");
string[] filePathDest = Directory.GetFiles(directoryDest); progressBar1.Minimum = 1;
progressBar1.Maximum = filePathSource.Length;
if (filePathDest.Length > 0)
{
for (int i = 0; i < filePathDest.Length; i++)
{
File.Delete(filePathDest[i]);
}
}
for (int i = 0; i < filePathSource.Length; i++)
{
File.Copy(filePathSource[i],
directoryDest + "\\" + Path.GetFileNameWithoutExtension(
filePathSource[i]) + ".bmp");
progressBar1.PerformStep();
Console.WriteLine(i);
}
progressBar1.Visible = false;
private void DoWithCommon()
{
WaitCallback waitCallBack = new WaitCallback(this.InvokeMethod);
ThreadPool.QueueUserWorkItem(waitCallBack, "Knights Warrior");
}
private void InvokeMethod(object x)
{
this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon), x.ToString());
}
private void ChangeUIWithCommon(string name)
{
this.lblMessage.Text = name;
}在线程中修改UI控件
关键
在progressindexchange事件中设置progressBar1的value
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void InvokeMethodDelegate(string name);
private void button1_Click(object sender, EventArgs e)
{
DoWithCommon();
} private void DoWithCommon()
{
WaitCallback waitCallBack = new WaitCallback(this.InvokeMethod);
ThreadPool.QueueUserWorkItem(waitCallBack, "Knights Warrior");
}
private void InvokeMethod(object x)
{
this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon), x.ToString());
}
private void ChangeUIWithCommon(string name)
{
this.button1.Text = name;
} }
}
private void InvokeMethod(object x)
{
for (int i = 0; i < filePathSource.Length; i++)
{
File.Copy(filePathSource[i],
directoryDest + "\\" + Path.GetFileNameWithoutExtension(
filePathSource[i]) + ".bmp");
Console.WriteLine(i);
this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon)); } }
private void ChangeUIWithCommon()
{
progressBar1.PerformStep();
}