گرفتن Progress در BackgroundWorker
سلام به همه !
چطوری میتونم Progress یه بک گراند ورکر را بگیرم؟؟؟
یا اصن Progress کل فرم را بگیرم؟؟؟
میخوام یک فایل را با webcilent دانلود کنم!
webcilent تو یه یکگراند ورکر تعریف شده!
حالا میخوام اون مقدار از فایلی که دانلود شده یا اون مقدار از عملیات که انجام شده را در قالب 0 تا 100 به پراپرتی Value یه ProgressBar بدم!
چطوری ین کار را بکنم؟؟؟؟
نقل قول: گرفتن Progress در BackgroundWorker
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
public class Form1 : Form
{
private BackgroundWorker backgroundWorker1;
private Button dowloadButton;
private XmlDocument document = null;
public Form1()
{
InitializeComponent();
}
private void dowloadButton_Click(object sender, EventArgs e)
{
// Start the download operation in the background.
this.backgroundWorker1.RunWorkerAsync();
// Disable the button for the duration of the download.
this.dowloadButton.Enabled = false;
// Wait for the BackgroundWorker to finish the download.
while (this.backgroundWorker1.IsBusy)
{
// Keep UI messages moving, so the form remains
// responsive during the asynchronous operation.
Application.DoEvents();
}
// The download is done, so enable the button.
this.dowloadButton.Enabled = true;
}
private void backgroundWorker1_DoWork(
object sender,
DoWorkEventArgs e)
{
document = new XmlDocument();
// Replace this file name with a valid file name.
document.Load(@"http://www.tailspintoys.com/sample.xml");
// Uncomment the following line to
// simulate a noticeable latency.
//Thread.Sleep(5000);
}
private void backgroundWorker1_RunWorkerCompleted(
object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(document.InnerXml, "Download Complete");
}
else
{
MessageBox.Show(
"Failed to download file",
"Download failed",
MessageBoxButtons.OK,
MessageBoxIcon.Error );
}
}
/// <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.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.dowloadButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// backgroundWorker1
//
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandl er(this.backgroundWorker1_RunWorkerCompleted);
//
// dowloadButton
//
this.dowloadButton.Location = new System.Drawing.Point(12, 12);
this.dowloadButton.Name = "dowloadButton";
this.dowloadButton.Size = new System.Drawing.Size(75, 23);
this.dowloadButton.TabIndex = 0;
this.dowloadButton.Text = "Download file";
this.dowloadButton.UseVisualStyleBackColor = true;
this.dowloadButton.Click += new System.EventHandler(this.dowloadButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(104, 54);
this.Controls.Add(this.dowloadButton);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
نقل قول: گرفتن Progress در BackgroundWorker
داداش من اینا از کجاش شروع کنم؟؟؟؟
یه چیزی بگو به گروه خونی ما بخوره!!!
نقل قول: گرفتن Progress در BackgroundWorker
نقل قول:
نوشته شده توسط
hosseinghaheri
داداش من اینا از کجاش شروع کنم؟؟؟؟
یه چیزی بگو به گروه خونی ما بخوره!!!
:لبخند: چه میدونم شما یجور نام تایپیک انتخاب کردید گفتم شاید بدونید
خوب عیبی نداره این دو تا سایت هستش بصورت خیلی ساده و با مثال پیش رفتن
http://stackoverflow.com/questions/7...ckgroundworker
http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx
نقل قول: گرفتن Progress در BackgroundWorker
نقل قول: گرفتن Progress در BackgroundWorker
شرمنده دادش ولی توی قسمت
backgroundWorker1.ProgressChanged += (s, e) =>
{
ReportProgress(String.Format("Progress: {0}%", e.ProgressPercentage));
};
دو تا ارور گرفت !
1) ReportProgress را نشناخت ! حالا نمی دونم یوزینگ می خواد ایا؟
2) به e توی خط اولم گیر داد!
مشکل چیه؟؟
نقل قول: گرفتن Progress در BackgroundWorker
نقل قول:
نوشته شده توسط
hosseinghaheri
شرمنده دادش ولی توی قسمت
backgroundWorker1.ProgressChanged += (s, e) =>
{
ReportProgress(String.Format("Progress: {0}%", e.ProgressPercentage));
};
دو تا ارور گرفت !
1) ReportProgress را نشناخت ! حالا نمی دونم یوزینگ می خواد ایا؟
2) به e توی خط اولم گیر داد!
مشکل چیه؟؟
گمون کنم اون e مال رویداد خاصی باشه
البته من BackGroundWorker کار نکردم
ولی اگه اشتباه نکنم باید در یک رویداد خاصی این کد ها رو بذارید که از E ارور نگیره
نقل قول: گرفتن Progress در BackgroundWorker
سلام،در شیء WebClient یدونه رخداد هست به نام DownloadProgressChanged که هر بار تغییر در Progress دریافت انجام بشه،فراخوانی میشود.بعد نیازی به BackgroundWorker نیست و میتونید از متد DownloadFileAsync استفاده کنید.نمونه کد هم در زیر براتون میذارم :
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += (s, ee) =>
{
progressBar1.Value = ee.ProgressPercentage;
};
webClient.DownloadFileAsync(new Uri("www.example.com/output.dat"), @"f:\Output.dat");