当前位置:首页 >> 电力/水利 >>

C# progressBar进度条--实时


C# progressBar 进度条--实时 2009-02-23 13:15using System;

using using using using using using using using using

System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.IO; System.Threading; System.Reflection;

namespace DataBackup { public partial class FrmBackup : Form { /// <summary> /// Self_Variable: progressBar 进度条 /// </summary> private delegate void sprogressBar(); public FrmBackup() { InitializeComponent(); }

#region 本类全局变量 /// <summary> /// Self_Object:其它操作方法对象 /// </summary> BackOther other = new BackOther(); /// <summary> /// Self_Variable:统计总字节数 /// </summary> private int totalSize = 0; /// <summary> /// Self_Variable:剩余字节数 /// </summary>

private int position = 0; /// <summary> /// Self_Variable:字节数对比 /// </summary> private const int BUFFER_SIZE = 4096; /// <summary> /// buffer 字节数组 /// </summary> private byte[] buffer; /// <summary> /// Self_Variable:字节视图 /// </summary> private Stream stream; #endregion

#region 备



private void button1_Click(object sender, EventArgs e) { //创建文件夹 other.FileExists(); //备份路径 string dbpath = "..\\..\\..\\Data\\gs_back_gw680.mdb"; //源数据库路径 string sourcepath = "..\\gs_gw680.mdb"; //创建备份数据库 BackOther.CreateAccess(dbpath); //压缩源数据库 BackOther.CompactAccess(sourcepath); FileStream fs = new FileStream(sourcepath, FileMode.Open, FileAccess.Read); totalSize = Convert.ToInt32(fs.Length); stream = fs; if (totalSize > BUFFER_SIZE) { buffer = new byte[BUFFER_SIZE]; stream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null);

} else { fs.Close(); } } #endregion

#region 异步方法 /// <summary> /// Asynchronously copy file /// </summary> /// <param name="ar"></param> private void AsyncCopyFile(IAsyncResult ar) { int readedLength; // Lock FileStream lock (stream) { readedLength = stream.EndRead(ar); // When stream endread, get readed length } // Write to disk string dbpath = "..\\..\\..\\Data\\gs_back_gw680.mdb"; FileStream fsWriter = new FileStream(dbpath, FileMode.Append, FileAccess.Write); fsWriter.Write(buffer, 0, buffer.Length); fsWriter.Close(); // Current stream position position += readedLength; //调用委托实现 progressBar 进度条 progressBar.BeginInvoke(new sprogressBar(SynchProgressBar)); // Read over. if (position >= totalSize) { //Close FileStream

stream.Close(); MessageBox.Show("备份完成!"); //忽略线程中的某些异常调用 Control.CheckForIllegalCrossThreadCalls = false; this.btnBack.Enabled = false; this.progressBar.Maximum = 0; this.progressBar.Value = 0; return; }

// Continue to read and write lock (stream) { int leftSize = totalSize - position; if (leftSize < BUFFER_SIZE) buffer = new byte[leftSize]; stream.BeginRead(buffer, AsyncCallback(AsyncCopyFile), null); } } 0, buffer.Length, new

/// <summary> /// Self_Function:修改进度条信息 /// </summary> private void SynchProgressBar() { this.progressBar.Maximum = totalSize; this.progressBar.Value = position; } #endregion } }



相关文章:
C#源代码实现进度条功能
C#源代码实现进度条功能_IT/计算机_专业资料。GProgressBar,是一个进度条控件,能够设置进度条的背景色和前景色,设置进度条的外观方式(3d,single,none),是否自动显...
C#进度条的使用
C#进度条的使用_计算机软件及应用_IT/计算机_专业资料。C#进度条的使用,附有垂直...label1.Text = "Records Read = " + progressBar1.Value.ToString(); } ...
c#进度条
c# 进度条的使用(例子) 在用 c#做 WinFrom 开发的过程中。我们经常需要用到进度条(ProgressBar)用于显示进度信息。这时候 我们可能就需要用到多线程,如果不采用...
...中使用BackgroundWorker在模态对话框中显示进度条(...
C#.NET 编写 WinForm 程序时,有时候为了实现在模态对话框中实时显 示后台操作...ProgressBar 控件, 对外可以通过 ProgressValue 属性来获 取和修改进度条的当前...
C#异步进度条程序
C#异步进度条程序_IT/计算机_专业资料。C# winform 程序开发异步进度条调用示例主...{ progressBar1.Visible = true; tr.Start(); } trMain tr = new trMain...
利用Visual C#打造一个平滑的进度条
ProgressBarColor。该属性表示了进度条的颜色。 建立一个自定义的进度条控件 1、按着下面的步骤,在 Visual C# .NET 中建立一个 Windows Control Library 项目: ...
C#三匹马赛跑,使用进度条和Timer实现
C#三匹马赛跑,使用进度条和Timer实现_计算机软件及应用_IT/计算机_专业资料。C#...{ progressBar1.Value = (int)x; } Application.DoEvents(); } private ...
进度条Progressbar
进度条Progressbar_计算机软件及应用_IT/计算机_专业资料 暂无评价|0人阅读|0次下载|举报文档 进度条Progressbar_计算机软件及应用_IT/计算机_专业资料。...
C# Winform弹出一个模式窗口来显示进度条
C# Winform弹出一个模式窗口来显示进度条_互联网_IT/计算机_专业资料。有时候我们...{ prcBar.Value = prcBar.Maximum; this.Close(); return false; } } ...
如何在C# .NET 中建立一个平滑的进度条
如何在C# .NET 中建立一个平滑的进度条_IT/计算机_专业资料。概述 本文描述了...this.Invalidate(updateRect); } } public Color ProgressBarColor { get { ...
更多相关标签: