使用C#在进度条中显示复制文件的进度

Canoe CAPL TCP通信避坑指南:从OnTcpConnect回调不触发到Socket句柄管理 本文详细解析了Canoe CAPL中TCP通信的常见问题与解决方案,包括OnTcpConnect回调不触发的三大原因、Socket句柄管理的内存泄漏风险、数据发送截断规则等。通过实战案例和代码示例,帮助开发者规避TCP通信中的典型陷阱,提升车载网络测试的稳定性和可靠性。 阅读详情

 

Code List: 
------------------------------------------------------------------------- 

/***************************************************************** 
** File Name: frmMain.cs 
** Copyright (c) 1999 -2003 
** Creator: FirePhoenix 
** Created Date: 2004-11-13 15:24 
** Modifier: 
** Modify Date: 
** Description: 
** Version:1.0 
******************************************************************/ 

#region Using Directives 
using System; 
using System.IO ; 
using System.Xml ; 
using System.Collections ; 
using System.Reflection ; 
using System.Text ; 
using System.Data ; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Threading ; 
#endregion 

namespace WindowsApplication4 
{ 
/// <summary> 
/// Copy Large File 
/// </summary> 
public class frmMain : System.Windows.Forms.Form 
{ 
#region 
private System.Windows.Forms.ProgressBar progressBar1; 
private System.Windows.Forms.Button btnCopy; 
/// <summary> 
/// 必需的设计器变量。 
/// </summary> 
private System.ComponentModel.Container components = null; 

public frmMain() 
{ 
// 
// Windows 窗体设计器支持所必需的 
// 
InitializeComponent(); 

// 
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
// 
} 

/// <summary> 
/// 清理所有正在使用的资源。 
/// </summary> 
protected override void Dispose( bool disposing ) 
{ 
if( disposing ) 
{ 
if (components != null) 
{ 
components.Dispose(); 
} 
} 
base.Dispose( disposing ); 
} 

#region Initialize Components 
/// <summary> 
/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
/// 此方法的内容。 
/// </summary> 
private void InitializeComponent() 
{ 
this.progressBar1 = new System.Windows.Forms.ProgressBar(); 
this.btnCopy = new System.Windows.Forms.Button(); 
this.SuspendLayout(); 
// 
// progressBar1 
// 
this.progressBar1.Location = new System.Drawing.Point(8, 16); 
this.progressBar1.Name = "progressBar1"; 
this.progressBar1.Size = new System.Drawing.Size(208, 16); 
this.progressBar1.TabIndex = 0; 
// 
// btnCopy 
// 
this.btnCopy.Location = new System.Drawing.Point(8, 48); 
this.btnCopy.Name = "btnCopy"; 
this.btnCopy.TabIndex = 1; 
this.btnCopy.Text = "Copy"; 
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click); 
// 
// frmMain 
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
this.ClientSize = new System.Drawing.Size(232, 77); 
this.Controls.Add(this.btnCopy); 
this.Controls.Add(this.progressBar1); 
this.Name = "frmMain"; 
this.Text = "Copy File"; 
this.ResumeLayout(false); 

} 
#endregion 

/// <summary> 
/// Entry Point 
/// </summary> 
[STAThread] 
static void Main() 
{ 
Application.Run(new frmMain()); 
} 


#endregion 

int totalSize; //Total Size 
int position; //Position 
const int BUFFER_SIZE = 4096; 
byte[] buffer; 
Stream stream; 

private void btnCopy_Click(object sender, System.EventArgs e) 
{ 
string strFile = ""; 

OpenFileDialog dlg = new OpenFileDialog(); 
if ( dlg.ShowDialog() == DialogResult.OK ) 
{ 
strFile = dlg.FileName ; 
} 
else 
{ 
return ; 
} 

FileStream fs = new FileStream( strFile , FileMode.Open , FileAccess.Read ) ; 
totalSize = (int)fs.Length ; 
stream = fs; 

//Delete file which aready exists. 
if ( File.Exists( "c://copyedFile.bin" ) ) 
File.Delete( "c://copyedFile.bin" ); 

//Copy file while larger than 4KB. 
if ( totalSize > BUFFER_SIZE ) 
{ 
buffer = new byte[ BUFFER_SIZE ]; 

// Async Invoke 
stream.BeginRead( buffer , 0 , BUFFER_SIZE , new AsyncCallback( AsyncCopyFile ) , null ); 
} 
else 
{ 
fs.Close(); 
} 

} 

/// <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 
FileStream fsWriter = new FileStream( "C://copyedFile.bin" , FileMode.Append , FileAccess.Write ); 
fsWriter.Write( buffer , 0 , buffer.Length ); 
fsWriter.Close(); 

// Current stream position 
position += readedLength; 

// Response UI 
MethodInvoker m = new MethodInvoker( SynchProgressBar ); 
m.BeginInvoke( null , null ); 

if ( position >= totalSize ) // Read over. 
{ 
stream.Close(); //Close FileStream 
return ; 
} 

// Continue to read and write 
lock ( stream ) 
{ 
int leftSize = totalSize - position; 

if ( leftSize < BUFFER_SIZE ) 
buffer = new byte[ leftSize ]; 

stream.BeginRead( buffer , 0 , buffer.Length , new AsyncCallback( AsyncCopyFile ) , null ); 

} 
} 

private void SynchProgressBar() 
{ 
this.progressBar1.Maximum = totalSize; 
this.progressBar1.Value = position ; 
} 

} 
} 

 

 

MethodInvoker m = new MethodInvoker( SynchProgressBar ); 
m.BeginInvoke( null , null ); 
跨线程访问不了this.progressBar1;
----------------------------
MethodInvoker m = new MethodInvoker( SynchProgressBar ); 
m.BeginInvoke( null , null ); 
改成
SynchProgressBar();
-------------------------------
private void SynchProgressBar() 
{ 
this.progressBar1.Maximum = totalSize; 
this.progressBar1.Value = position ; 
} 
改成
delegate void SynchProgressBarCallback(); 
private void SynchProgressBar()
{
if (this.progressBar1.InvokeRequired)
{
SynchProgressBarCallback d = new SynchProgressBarCallback(SynchProgressBar);
this.Invoke(d);
}
else
{
this.progressBar1.Maximum = totalSize;
this.progressBar1.Value = position;
}
}

YOLO11,ultralytics-main源代码目录结构详解 本文详细介绍了YOLO11框架的ultralytics-main源代码目录结构,帮助用户更好地理解和使用YOLO模型。主要目录包括:docker文件夹,包含多种Docker镜像文件,适用于不同硬件环境;docs文件夹,存放YOLO的说明文档;examples文件夹,提供YOLO使用的示例代码;tests文件夹,包含单元测试模块;ultralytics文件夹,为核心代码所在,包含推理逻辑、配置、数据处理等模块。其中,engine文件夹负责训练和推理引擎,models文件夹存放模型架构,nn定义神经网络结构 阅读详情

相关推荐

上一篇: Asp.Net 动态生成验证码
下一篇: ASP.NET实现投票结果的图片进度条显示
beichen_
博客等级 码龄19年 17粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值