cardano7
جمعه 28 خرداد 1389, 15:53 عصر
سلام بچه ها
من امروز یک پست (http://barnamenevis.org/forum/showpost.php?p=480943)را دیدم که به نظرم جالب اومد.
این پست در مورد یک سیستم کپی پیشرفته به همراه ProgressBar بود.
اما مشکلاتی هم داشت. مثلا از آنجا که سعی می کرد حجم یک فایل را در درون مقدار ماکزیمم یک ProgressBar بریزه، برای اعداد بزرگ تجاوز از حد int.MaxValue صورت می گرفت و در کپی فایل های بزرگ ایراد داشت. من این ایراد را بر طرف کردم و قابلیت Pause و Stop و یک lable حاوی اطلاعات را هم به برنامه اضافه کردم. یک سری تغییرات دیگه هم روش ایجاد کردم که بهتر بشه.
حالا کل برنامه را اینجا قرار می دم که هر کسی دوست داشت یک نگاهی بهش بندازه. کافیه این کد را جایگزین کدهای Program.cs کنید و برنامه را اجرا کنید.
اگر کسی نکته ی مفیدی می دونه ما را در جریان قرار بده. به خصوص اینکه احساس می کنم سرعت این برنامه از سرعت کپی ویندوز یکمی کمتره.
موفق باشید.
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;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
//User Form
#region User Form
public Form1()
{
InitializeComponent();
label_Progress.Text = "";
}
private void button_From_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
textBox_From.Text = openFileDialog.FileName;
}
private void button_To_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
textBox_To.Text = saveFileDialog.FileName;
}
private void button_AdvancedCopy_Click(object sender, EventArgs e)
{
AdvancedCopy(textBox_From.Text, textBox_To.Text);
}
bool IsPaused = false;
bool IsStopped = false;
private void AdvancedCopy(string FileSource, string FileDestination)
{
int NumRead;
double ReadProgress = 0;
long FileLength_Remain;
long FileLength;
IsPaused = false;
IsStopped = false;
Referesh_Buttons();
System.IO.FileStream From = null;
System.IO.FileStream To = null;
try
{
button_AdvancedCopy.Visible = false;
progressBar_AdvancedCopy.Visible = true;
label_Progress.Visible = true;
button_Pause.Visible = true;
button_Stop.Visible = true;
From = new System.IO.FileStream(FileSource, System.IO.FileMode.Open);
To = new System.IO.FileStream(FileDestination, System.IO.FileMode.CreateNew);
byte[] buffer = new byte[1024];
FileLength = From.Length;
FileLength_Remain = FileLength;
progressBar_AdvancedCopy.Minimum = 0;
progressBar_AdvancedCopy.Maximum = 100;
DateTime Lastreferesh = DateTime.Now;
while (FileLength_Remain > 0 && !IsStopped)
{
System.IO.BinaryReader Reader = new System.IO.BinaryReader(From);
NumRead = Reader.Read(buffer, 0, 1024);
FileLength_Remain = FileLength_Remain - NumRead;
System.IO.BinaryWriter Writer = new System.IO.BinaryWriter(To);
Writer.Write(buffer, 0, NumRead);
ReadProgress += NumRead;
int Percent = Math.Min((int)((ReadProgress / FileLength) * 100), 100);
progressBar_AdvancedCopy.Value = Percent;
label_Progress.Text = "" + (int)Math.Ceiling(ReadProgress / 1024.0 / 1024.0) + " out of " + (int)Math.Ceiling(FileLength / 1024.0 / 1024.0) + " MB (" + Percent + "%)";
Writer.Flush();
if (DateTime.Now - Lastreferesh > TimeSpan.FromMilliseconds(500))
{
Application.DoEvents();
Lastreferesh = DateTime.Now;
}
while (IsPaused && !IsStopped)
Application.DoEvents();
}
if (progressBar_AdvancedCopy.Value > 99)
{
progressBar_AdvancedCopy.Value = 0;
MessageBox.Show("Copy Finished successfuly");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (From != null)
From.Close();
if (To != null)
To.Close();
button_AdvancedCopy.Visible = true;
progressBar_AdvancedCopy.Visible = false;
label_Progress.Visible = false;
button_Pause.Visible = false;
button_Stop.Visible = false;
}
}
private void button_Pause_Click(object sender, EventArgs e)
{
IsPaused = !IsPaused;
Referesh_Buttons();
}
public void Referesh_Buttons()
{
if (IsPaused)
button_Pause.Text = "Resume";
else
button_Pause.Text = "Pause";
}
private void button_Stop_Click(object sender, EventArgs e)
{
IsStopped = true;
}
#endregion
//Designer
#region Designer
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button_Stop = new System.Windows.Forms.Button();
this.button_Pause = new System.Windows.Forms.Button();
this.label_Progress = new System.Windows.Forms.Label();
this.progressBar_AdvancedCopy = new System.Windows.Forms.ProgressBar();
this.button_AdvancedCopy = new System.Windows.Forms.Button();
this.textBox_To = new System.Windows.Forms.TextBox();
this.textBox_From = new System.Windows.Forms.TextBox();
this.button_To = new System.Windows.Forms.Button();
this.button_From = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button_Stop
//
this.button_Stop.Location = new System.Drawing.Point(229, 98);
this.button_Stop.Name = "button_Stop";
this.button_Stop.Size = new System.Drawing.Size(75, 23);
this.button_Stop.TabIndex = 19;
this.button_Stop.Text = "Stop";
this.button_Stop.UseVisualStyleBackColor = true;
this.button_Stop.Visible = false;
this.button_Stop.Click += new System.EventHandler(this.button_Stop_Click);
//
// button_Pause
//
this.button_Pause.Location = new System.Drawing.Point(148, 98);
this.button_Pause.Name = "button_Pause";
this.button_Pause.Size = new System.Drawing.Size(75, 23);
this.button_Pause.TabIndex = 20;
this.button_Pause.Text = "Pause";
this.button_Pause.UseVisualStyleBackColor = true;
this.button_Pause.Visible = false;
this.button_Pause.Click += new System.EventHandler(this.button_Pause_Click);
//
// label_Progress
//
this.label_Progress.AutoSize = true;
this.label_Progress.Location = new System.Drawing.Point(310, 115);
this.label_Progress.Name = "label_Progress";
this.label_Progress.Size = new System.Drawing.Size(35, 13);
this.label_Progress.TabIndex = 18;
this.label_Progress.Text = "label1";
this.label_Progress.Visible = false;
//
// progressBar_AdvancedCopy
//
this.progressBar_AdvancedCopy.Location = new System.Drawing.Point(310, 98);
this.progressBar_AdvancedCopy.Name = "progressBar_AdvancedCopy";
this.progressBar_AdvancedCopy.Size = new System.Drawing.Size(204, 14);
this.progressBar_AdvancedCopy.TabIndex = 17;
this.progressBar_AdvancedCopy.Visible = false;
//
// button_AdvancedCopy
//
this.button_AdvancedCopy.Location = new System.Drawing.Point(22, 98);
this.button_AdvancedCopy.Name = "button_AdvancedCopy";
this.button_AdvancedCopy.Size = new System.Drawing.Size(120, 23);
this.button_AdvancedCopy.TabIndex = 16;
this.button_AdvancedCopy.Text = "Advanced Copy";
this.button_AdvancedCopy.UseVisualStyleBackColor = true;
this.button_AdvancedCopy.Click += new System.EventHandler(this.button_AdvancedCopy_Click );
//
// textBox_To
//
this.textBox_To.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Wind ows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_To.Location = new System.Drawing.Point(105, 53);
this.textBox_To.Name = "textBox_To";
this.textBox_To.Size = new System.Drawing.Size(432, 20);
this.textBox_To.TabIndex = 11;
//
// textBox_From
//
this.textBox_From.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Wind ows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_From.Location = new System.Drawing.Point(105, 25);
this.textBox_From.Name = "textBox_From";
this.textBox_From.Size = new System.Drawing.Size(432, 20);
this.textBox_From.TabIndex = 12;
//
// button_To
//
this.button_To.Location = new System.Drawing.Point(23, 53);
this.button_To.Name = "button_To";
this.button_To.Size = new System.Drawing.Size(75, 23);
this.button_To.TabIndex = 10;
this.button_To.Text = "To";
this.button_To.UseVisualStyleBackColor = true;
this.button_To.Click += new System.EventHandler(this.button_To_Click);
//
// button_From
//
this.button_From.Location = new System.Drawing.Point(23, 23);
this.button_From.Name = "button_From";
this.button_From.Size = new System.Drawing.Size(75, 23);
this.button_From.TabIndex = 9;
this.button_From.Text = "From";
this.button_From.UseVisualStyleBackColor = true;
this.button_From.Click += new System.EventHandler(this.button_From_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(549, 159);
this.Controls.Add(this.button_Stop);
this.Controls.Add(this.button_Pause);
this.Controls.Add(this.label_Progress);
this.Controls.Add(this.progressBar_AdvancedCopy);
this.Controls.Add(this.button_AdvancedCopy);
this.Controls.Add(this.textBox_To);
this.Controls.Add(this.textBox_From);
this.Controls.Add(this.button_To);
this.Controls.Add(this.button_From);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button_Stop;
private System.Windows.Forms.Button button_Pause;
private System.Windows.Forms.Label label_Progress;
private System.Windows.Forms.ProgressBar progressBar_AdvancedCopy;
private System.Windows.Forms.Button button_AdvancedCopy;
private System.Windows.Forms.TextBox textBox_To;
private System.Windows.Forms.TextBox textBox_From;
private System.Windows.Forms.Button button_To;
private System.Windows.Forms.Button button_From;
#endregion
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}
من امروز یک پست (http://barnamenevis.org/forum/showpost.php?p=480943)را دیدم که به نظرم جالب اومد.
این پست در مورد یک سیستم کپی پیشرفته به همراه ProgressBar بود.
اما مشکلاتی هم داشت. مثلا از آنجا که سعی می کرد حجم یک فایل را در درون مقدار ماکزیمم یک ProgressBar بریزه، برای اعداد بزرگ تجاوز از حد int.MaxValue صورت می گرفت و در کپی فایل های بزرگ ایراد داشت. من این ایراد را بر طرف کردم و قابلیت Pause و Stop و یک lable حاوی اطلاعات را هم به برنامه اضافه کردم. یک سری تغییرات دیگه هم روش ایجاد کردم که بهتر بشه.
حالا کل برنامه را اینجا قرار می دم که هر کسی دوست داشت یک نگاهی بهش بندازه. کافیه این کد را جایگزین کدهای Program.cs کنید و برنامه را اجرا کنید.
اگر کسی نکته ی مفیدی می دونه ما را در جریان قرار بده. به خصوص اینکه احساس می کنم سرعت این برنامه از سرعت کپی ویندوز یکمی کمتره.
موفق باشید.
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;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
//User Form
#region User Form
public Form1()
{
InitializeComponent();
label_Progress.Text = "";
}
private void button_From_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
textBox_From.Text = openFileDialog.FileName;
}
private void button_To_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
textBox_To.Text = saveFileDialog.FileName;
}
private void button_AdvancedCopy_Click(object sender, EventArgs e)
{
AdvancedCopy(textBox_From.Text, textBox_To.Text);
}
bool IsPaused = false;
bool IsStopped = false;
private void AdvancedCopy(string FileSource, string FileDestination)
{
int NumRead;
double ReadProgress = 0;
long FileLength_Remain;
long FileLength;
IsPaused = false;
IsStopped = false;
Referesh_Buttons();
System.IO.FileStream From = null;
System.IO.FileStream To = null;
try
{
button_AdvancedCopy.Visible = false;
progressBar_AdvancedCopy.Visible = true;
label_Progress.Visible = true;
button_Pause.Visible = true;
button_Stop.Visible = true;
From = new System.IO.FileStream(FileSource, System.IO.FileMode.Open);
To = new System.IO.FileStream(FileDestination, System.IO.FileMode.CreateNew);
byte[] buffer = new byte[1024];
FileLength = From.Length;
FileLength_Remain = FileLength;
progressBar_AdvancedCopy.Minimum = 0;
progressBar_AdvancedCopy.Maximum = 100;
DateTime Lastreferesh = DateTime.Now;
while (FileLength_Remain > 0 && !IsStopped)
{
System.IO.BinaryReader Reader = new System.IO.BinaryReader(From);
NumRead = Reader.Read(buffer, 0, 1024);
FileLength_Remain = FileLength_Remain - NumRead;
System.IO.BinaryWriter Writer = new System.IO.BinaryWriter(To);
Writer.Write(buffer, 0, NumRead);
ReadProgress += NumRead;
int Percent = Math.Min((int)((ReadProgress / FileLength) * 100), 100);
progressBar_AdvancedCopy.Value = Percent;
label_Progress.Text = "" + (int)Math.Ceiling(ReadProgress / 1024.0 / 1024.0) + " out of " + (int)Math.Ceiling(FileLength / 1024.0 / 1024.0) + " MB (" + Percent + "%)";
Writer.Flush();
if (DateTime.Now - Lastreferesh > TimeSpan.FromMilliseconds(500))
{
Application.DoEvents();
Lastreferesh = DateTime.Now;
}
while (IsPaused && !IsStopped)
Application.DoEvents();
}
if (progressBar_AdvancedCopy.Value > 99)
{
progressBar_AdvancedCopy.Value = 0;
MessageBox.Show("Copy Finished successfuly");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (From != null)
From.Close();
if (To != null)
To.Close();
button_AdvancedCopy.Visible = true;
progressBar_AdvancedCopy.Visible = false;
label_Progress.Visible = false;
button_Pause.Visible = false;
button_Stop.Visible = false;
}
}
private void button_Pause_Click(object sender, EventArgs e)
{
IsPaused = !IsPaused;
Referesh_Buttons();
}
public void Referesh_Buttons()
{
if (IsPaused)
button_Pause.Text = "Resume";
else
button_Pause.Text = "Pause";
}
private void button_Stop_Click(object sender, EventArgs e)
{
IsStopped = true;
}
#endregion
//Designer
#region Designer
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button_Stop = new System.Windows.Forms.Button();
this.button_Pause = new System.Windows.Forms.Button();
this.label_Progress = new System.Windows.Forms.Label();
this.progressBar_AdvancedCopy = new System.Windows.Forms.ProgressBar();
this.button_AdvancedCopy = new System.Windows.Forms.Button();
this.textBox_To = new System.Windows.Forms.TextBox();
this.textBox_From = new System.Windows.Forms.TextBox();
this.button_To = new System.Windows.Forms.Button();
this.button_From = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button_Stop
//
this.button_Stop.Location = new System.Drawing.Point(229, 98);
this.button_Stop.Name = "button_Stop";
this.button_Stop.Size = new System.Drawing.Size(75, 23);
this.button_Stop.TabIndex = 19;
this.button_Stop.Text = "Stop";
this.button_Stop.UseVisualStyleBackColor = true;
this.button_Stop.Visible = false;
this.button_Stop.Click += new System.EventHandler(this.button_Stop_Click);
//
// button_Pause
//
this.button_Pause.Location = new System.Drawing.Point(148, 98);
this.button_Pause.Name = "button_Pause";
this.button_Pause.Size = new System.Drawing.Size(75, 23);
this.button_Pause.TabIndex = 20;
this.button_Pause.Text = "Pause";
this.button_Pause.UseVisualStyleBackColor = true;
this.button_Pause.Visible = false;
this.button_Pause.Click += new System.EventHandler(this.button_Pause_Click);
//
// label_Progress
//
this.label_Progress.AutoSize = true;
this.label_Progress.Location = new System.Drawing.Point(310, 115);
this.label_Progress.Name = "label_Progress";
this.label_Progress.Size = new System.Drawing.Size(35, 13);
this.label_Progress.TabIndex = 18;
this.label_Progress.Text = "label1";
this.label_Progress.Visible = false;
//
// progressBar_AdvancedCopy
//
this.progressBar_AdvancedCopy.Location = new System.Drawing.Point(310, 98);
this.progressBar_AdvancedCopy.Name = "progressBar_AdvancedCopy";
this.progressBar_AdvancedCopy.Size = new System.Drawing.Size(204, 14);
this.progressBar_AdvancedCopy.TabIndex = 17;
this.progressBar_AdvancedCopy.Visible = false;
//
// button_AdvancedCopy
//
this.button_AdvancedCopy.Location = new System.Drawing.Point(22, 98);
this.button_AdvancedCopy.Name = "button_AdvancedCopy";
this.button_AdvancedCopy.Size = new System.Drawing.Size(120, 23);
this.button_AdvancedCopy.TabIndex = 16;
this.button_AdvancedCopy.Text = "Advanced Copy";
this.button_AdvancedCopy.UseVisualStyleBackColor = true;
this.button_AdvancedCopy.Click += new System.EventHandler(this.button_AdvancedCopy_Click );
//
// textBox_To
//
this.textBox_To.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Wind ows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_To.Location = new System.Drawing.Point(105, 53);
this.textBox_To.Name = "textBox_To";
this.textBox_To.Size = new System.Drawing.Size(432, 20);
this.textBox_To.TabIndex = 11;
//
// textBox_From
//
this.textBox_From.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Wind ows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_From.Location = new System.Drawing.Point(105, 25);
this.textBox_From.Name = "textBox_From";
this.textBox_From.Size = new System.Drawing.Size(432, 20);
this.textBox_From.TabIndex = 12;
//
// button_To
//
this.button_To.Location = new System.Drawing.Point(23, 53);
this.button_To.Name = "button_To";
this.button_To.Size = new System.Drawing.Size(75, 23);
this.button_To.TabIndex = 10;
this.button_To.Text = "To";
this.button_To.UseVisualStyleBackColor = true;
this.button_To.Click += new System.EventHandler(this.button_To_Click);
//
// button_From
//
this.button_From.Location = new System.Drawing.Point(23, 23);
this.button_From.Name = "button_From";
this.button_From.Size = new System.Drawing.Size(75, 23);
this.button_From.TabIndex = 9;
this.button_From.Text = "From";
this.button_From.UseVisualStyleBackColor = true;
this.button_From.Click += new System.EventHandler(this.button_From_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(549, 159);
this.Controls.Add(this.button_Stop);
this.Controls.Add(this.button_Pause);
this.Controls.Add(this.label_Progress);
this.Controls.Add(this.progressBar_AdvancedCopy);
this.Controls.Add(this.button_AdvancedCopy);
this.Controls.Add(this.textBox_To);
this.Controls.Add(this.textBox_From);
this.Controls.Add(this.button_To);
this.Controls.Add(this.button_From);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button_Stop;
private System.Windows.Forms.Button button_Pause;
private System.Windows.Forms.Label label_Progress;
private System.Windows.Forms.ProgressBar progressBar_AdvancedCopy;
private System.Windows.Forms.Button button_AdvancedCopy;
private System.Windows.Forms.TextBox textBox_To;
private System.Windows.Forms.TextBox textBox_From;
private System.Windows.Forms.Button button_To;
private System.Windows.Forms.Button button_From;
#endregion
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}