资源简介

C# ToolStripProgressBar Thread 线程操作

资源截图

代码片段和文件信息

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 OperaControsInThreads
{
    public partial class FormMain : Form
    {
        private string sourceDir;
        private string targetDir;
        private int iFileCount;
        public delegate void ReportProcessInfo(string Info int iPercent); //实现BackgroundWorker的ProgressChanged事件
        public delegate void DoneAfterCompleted(string Info);              ////实现BackgroundWorker的RunWorkerCompleted事件

        ReportProcessInfo CopyOneFileIsOK;
        DoneAfterCompleted CopyFilesIsCompleted;

        //更新Listview和ProgressBar
        private void UpdateInfoToUser(string infoint percent)
        {
            if (InvokeRequired)
                Invoke(new ReportProcessInfo(UpdateInfoToUser) info percent);
            else
            {
                lvOutput.Items.Add(new ListViewItem(new string[] { System.DateTime.Now.ToLongTimeString() info })).EnsureVisible();
                tssbProcess.Value = percent;
            }
               
        }
        //清空源目录和目标目录信息,显示拷贝文件数
        private void ShowUserFilesCountInfo(string info)
        {
            if (InvokeRequired)
                Invoke(new DoneAfterCompleted(ShowUserFilesCountInfo) info);
            else
            {                
                tbSource.Text = string.Empty;
                tbTargetDir.Text = string.Empty;
                MessageBox.Show(info);             
            }
            
        }

        public FormMain()
        {
            InitializeComponent();
            //加入委托方法
            CopyOneFileIsOK = new ReportProcessInfo(UpdateInfoToUser);
            CopyFilesIsCompleted = new DoneAfterCompleted(ShowUserFilesCountInfo);
        }

        private void btnStart_Click(object sender EventArgs e)
        {
            sourceDir = tbSource.Text.Trim();
            targetDir = tbTargetDir.Text.Trim();
            if (Directory.Exists(sourceDir))
            {                
                DirectoryInfo di = new DirectoryInfo(sourceDir);               
                iFileCount = di.GetFiles().Length;

                if (!Directory.Exists(targetDir))
                    Directory.CreateDirectory(targetDir);
                Thread WorkThread = new Thread(new ParameterizedThreadStart(CopyFiles));
                WorkThread.Start(di);
            }
            
        }

        private int GetPercent(float iCurfloat iTotal)
        {
            return (int)((iCur*100)/iTotal);
        }

        private void CopyFiles(object SourceDir)
        {
            DirectoryInfo di = (DirectoryInfo)SourceDir;
            int icur = 0;
            foreach (FileInfo fi in di.GetFiles())
            {
                icur++;
                tsslInfo.Text = s

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      24576  2008-12-11 22:05  OperaControsInThreads\OperaControsInThreads\bin\Debug\OperaControsInThreads.exe

     文件      28160  2008-12-11 22:05  OperaControsInThreads\OperaControsInThreads\bin\Debug\OperaControsInThreads.pdb

     文件       5632  2005-12-08 14:51  OperaControsInThreads\OperaControsInThreads\bin\Debug\OperaControsInThreads.vshost.exe

     文件       3374  2008-12-11 22:17  OperaControsInThreads\OperaControsInThreads\Form1.cs

     文件       7596  2008-12-11 21:41  OperaControsInThreads\OperaControsInThreads\Form1.Designer.cs

     文件       6008  2008-12-11 21:41  OperaControsInThreads\OperaControsInThreads\Form1.resx

     文件        842  2008-12-11 21:41  OperaControsInThreads\OperaControsInThreads\obj\Debug\OperaControsInThreads.csproj.GenerateResource.Cache

     文件      24576  2008-12-11 22:05  OperaControsInThreads\OperaControsInThreads\obj\Debug\OperaControsInThreads.exe

     文件        180  2008-12-11 21:41  OperaControsInThreads\OperaControsInThreads\obj\Debug\OperaControsInThreads.FormMain.resources

     文件      28160  2008-12-11 22:05  OperaControsInThreads\OperaControsInThreads\obj\Debug\OperaControsInThreads.pdb

     文件        180  2008-12-10 15:00  OperaControsInThreads\OperaControsInThreads\obj\Debug\OperaControsInThreads.Properties.Resources.resources

     文件        369  2008-12-11 22:07  OperaControsInThreads\OperaControsInThreads\obj\OperaControsInThreads.csproj.FileList.txt

     文件       3249  2008-12-10 15:00  OperaControsInThreads\OperaControsInThreads\OperaControsInThreads.csproj

     文件        483  2008-12-10 14:18  OperaControsInThreads\OperaControsInThreads\Program.cs

     文件       1198  2008-12-10 14:10  OperaControsInThreads\OperaControsInThreads\Properties\AssemblyInfo.cs

     文件       2897  2008-12-10 14:10  OperaControsInThreads\OperaControsInThreads\Properties\Resources.Designer.cs

     文件       5612  2008-12-10 14:10  OperaControsInThreads\OperaControsInThreads\Properties\Resources.resx

     文件       1105  2008-12-10 14:10  OperaControsInThreads\OperaControsInThreads\Properties\Settings.Designer.cs

     文件        249  2008-12-10 14:10  OperaControsInThreads\OperaControsInThreads\Properties\Settings.settings

     文件        952  2008-12-10 14:10  OperaControsInThreads\OperaControsInThreads.sln

    ..A..H.     14848  2008-12-11 22:17  OperaControsInThreads\OperaControsInThreads.suo

     目录          0  2008-12-10 16:26  OperaControsInThreads\OperaControsInThreads\obj\Debug\Refactor

     目录          0  2008-12-10 16:26  OperaControsInThreads\OperaControsInThreads\obj\Debug\TempPE

     目录          0  2008-12-10 16:26  OperaControsInThreads\OperaControsInThreads\bin\Debug

     目录          0  2008-12-11 22:05  OperaControsInThreads\OperaControsInThreads\obj\Debug

     目录          0  2008-12-10 16:26  OperaControsInThreads\OperaControsInThreads\bin

     目录          0  2008-12-10 16:26  OperaControsInThreads\OperaControsInThreads\obj

     目录          0  2008-12-10 16:26  OperaControsInThreads\OperaControsInThreads\Properties

     目录          0  2008-12-11 22:17  OperaControsInThreads\OperaControsInThreads

     目录          0  2008-12-10 16:26  OperaControsInThreads

............此处省略3个文件信息

评论

共有 条评论