C# Keep track of the amount of written bytes - c#

I was looking into writing data and I used:
System.IO.File.WriteAllBytes(path, GetBytes());
This worked for me but then I wanted to get the progress of the written bytes so I can have a progressbar added to it. I found out that my method can't connect to a progressbar so I decided to use this:
using (BinaryWriter writer = new BinaryWriter(System.IO.File.Open(path, FileMode.Create))) {
byte[] nextBytes = GetBytes(); // your logic to get what to write;
writer.Write(nextBytes);
}
This writes my file succesfully but after a lot of searching (maybe not enough) I can't seem to find out how to keep track of the amount of bytes being written until it's complete. Maybe I'm using a wrong method, if anyone could help me out, would be awesome! (I use a WPF)
Thanks in advance,
Update:
On top of the project I delcare BackgroundWorker()
BackgroundWorker bw;
Then when I start the download I haev this:
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerAsync(data);
My doWork method:
private void bw_DoWork (object sender, DoWorkEventArgs e) {
downloadData data = (downloadData) e.Argument;
_source = data.DownloadLocation + #"\";
sem.WaitOne();
//enters
updateStatusBar("- Downloading -", data, 0);
Video vid = youtube.GetVideo(data.Url);
byte[] bytes = vid.GetBytes();
using (var writer = new BinaryWriter(System.IO.File.Open(_source + vid.FullName, FileMode.Create))) {
// do your read logic here and read into bytes
var bytesLeft = bytes.Length;
var bytesWritten = 0;
while (bytesLeft > 0) {
int chunk = Math.Min(64, bytesLeft);
writer.Write(bytes, bytesWritten, chunk);
bytesWritten += chunk;
bytesLeft -= chunk;
// calculate progress bar status
bw.ReportProgress(bytesWritten * 100 / bytes.Length, data);
}
}
// Leaves
updateStatusBar("- Done -", data, 100);
sem.Release();
}
My Progresschanged method:
private void bw_ProgressChanged (object sender, ProgressChangedEventArgs e) {
YoutubeData data = (YoutubeData) e.UserState;
updateStatusBar("Downloading: " + e.ProgressPercentage, data, e.ProgressPercentage);
}
Method for updating status bar:
private void updateStatusBar(string txt, downloadData data, int progress) {
_downloadData.Remove(data);
data.ProgressText = txt;
data.Status = progress;
_downloadData.Add(data);
Dispatcher.Invoke(new Action(() => {
lv_DownloadData.ItemsSource = null;
lv_DownloadData.ItemsSource = _youtubeData;
}));
}
But now it freezes my WPF.

Write bytes in chunks and use a BackgroundWorker
using(var writer = new BinaryWriter(System.IO.File.Open(path, FileMode.Create)))) {
// do your read logic here and read into bytes
var bytesLeft = bytes.Length;
var bytesWritten = 0;
while(bytesLeft > 0) {
int chunk = Math.Min(64, bytesLeft);
writer.WriteBytes(array, bytesWritten, chunk);
bytesWritten += chunk;
bytesLeft -= chunk;
// calculate progress bar status
backgroundWorker.ReportProgress(bytesWritten * 100 / array.Length);
}
}
Update
OK, this is purely untested.. but stay we me..
Each BinaryWriterwill do its work on a BackgroundWorker
so you set up the background worker
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
Then you need to start the BackgroundWorker somewhere using bw.RunWorkerAsync()
void bw_DoWork(object sender, DoWorkEventArgs e)
{
// Do your task here
// report its progress
bw.ReportProgress(..);
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// update your progress bar here
}

Related

Background Worker Completed Event is firing before it is actually finished doing work

In my application, when a user clicks on a menu item, they are prompted to select a file which I then use a background worker to load and parse with progress being reported to a progress bar during this operation. when the work is complete, I start a new window which i pass the parsed data to. for some reason, the new window is getting opened before the data is parsed and null arguments are passed instead of the data.
my Click Event:
private void CIRCUIT_INSTALATION_SCHEDULED_Click(object sender, RoutedEventArgs e)
{
//prevents users from selecting other menu items
disableAll();
System.Windows.Forms.OpenFileDialog mainExcelFileDialog = new System.Windows.Forms.OpenFileDialog();
mainExcelFileDialog.InitialDirectory = System.Configuration.ConfigurationManager.AppSettings["MainWorkbookDirectory"];
mainExcelFileDialog.Title = "Main Excel File";
mainExcelFileDialog.ShowDialog();
string mainPath = mainExcelFileDialog.FileName;
this.Cursor = System.Windows.Input.Cursors.Wait;
BackgroundWorker cisWorker = new BackgroundWorker();
cisWorker.DoWork += cisWorker_DoWork;
cisWorker.ProgressChanged+=cisWorker_ProgressChanged;
cisWorker.RunWorkerCompleted += cisWorker_RunWorkerCompleted;
cisWorker.RunWorkerAsync(mainPath);
}
my Do Work:
void cisWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
using (FileStream fs = new FileStream(e.Argument.ToString() , FileMode.Open))
{
//copy filestream to memorystream chunk by chunk, reporting progress.
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[32768];
int read;
int steps = Convert.ToInt32(fs.Length / buffer.Length);
int i = 0;
while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
i++;
var progress = new Decimal(i) / new Decimal(steps);
var outOf = Math.Round(progress * 50);
worker.ReportProgress(Convert.ToInt32(outOf));
}
worker.ReportProgress(50);
ms.Position = 0;
//load excel workbook dataset from memorystream
using (var xlr = Excel.ExcelReaderFactory.CreateOpenXmlReader(ms))
{
xlr.IsFirstRowAsColumnNames = true;
mainWorkbook = xlr.AsDataSet();
worker.ReportProgress(100);
}
}
}
}
my on complete:
void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Cursor = System.Windows.Input.Cursors.Arrow;
CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow();
CIS_Window.Show();
CIS_Window.Closed += CIS_Window_Closed;
}
EDIT:
I added the mainPath argument to the cisWorker.runWorkerAsync() method. the problem persists unfortunately.
Basically you should check for your data not being null, i.e that no error occurred during DoWork
void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
{
/* Your mainWorkbook shouldn't be null */
this.Cursor = System.Windows.Input.Cursors.Arrow;
CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow();
CIS_Window.Show();
CIS_Window.Closed += CIS_Window_Closed;
}
else
{
// Handle error
}
}
Henk Holterman was right. I was getting an error because I had not set cisWorker.WorkerReportsProgress = true; I still don't know why the error wasn't breaking the debugger since it wasn't being handled, but I did a print statement in my Completed method which helped me track down the culprit.
void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine(e.Error);
this.Cursor = System.Windows.Input.Cursors.Arrow;
CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow(mainWorkbook, abfsDatabase);
CIS_Window.Show();
CIS_Window.Closed += CIS_Window_Closed;
}

C# Backgroundworker download progress in label, get bytes in label

I've created an application that patches my game servers files.
However, I've got 3 problems which I can't solve:
When downloading a new patch, it doesn't update the progressbar instantly, but refreshes it after around 30-40 seconds
I want a label to show how much mega bytes they are downloading, and how much they have so far (for example: 122Mb/750Mb
When downloading, I want a label to show ~% of how much it has downloaded so far
I am not sure how to add number 2 and 3, and the number 1 problem just seems ridiculous, because there's nothing that indicates it should refresh after 30-40 seconds in my coding (at least as far as I know)
My backgroundWorker1_DoWork:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Defines the server's update directory
string Server = "http://localhost/dl/game-updates/";
//Defines application root
string Root = AppDomain.CurrentDomain.BaseDirectory;
//Make sure version file exists
FileStream fs = null;
if (!File.Exists("version"))
{
using (fs = File.Create("version"))
{
}
using (StreamWriter sw = new StreamWriter("version"))
{
sw.Write("1.0");
}
}
//checks client version
string lclVersion;
using (StreamReader reader = new StreamReader("version"))
{
lclVersion = reader.ReadLine();
}
decimal localVersion = decimal.Parse(lclVersion);
//server's list of updates
XDocument serverXml = XDocument.Load(#Server + "Updates.xml");
//The Update Process
foreach (XElement update in serverXml.Descendants("update"))
{
string version = update.Element("version").Value;
string file = update.Element("file").Value;
decimal serverVersion = decimal.Parse(version);
string sUrlToReadFileFrom = Server + file;
string sFilePathToWriteFileTo = Root + file;
if (serverVersion > localVersion)
{
Uri url = new Uri(sUrlToReadFileFrom);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
Int64 iSize = response.ContentLength;
Int64 iRunningByteTotal = 0;
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
{
using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
{
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
backgroundWorker1.ReportProgress(iProgressPercentage);
}
streamLocal.Close();
}
streamRemote.Close();
}
}
//unzip
using (ZipFile zip = ZipFile.Read(file))
{
foreach (ZipEntry zipFiles in zip)
{
zipFiles.Extract(Root + "\\", true);
}
}
//download new version file
WebClient webClient = new WebClient();
webClient.DownloadFile(Server + "version.txt", #Root + "version");
//Delete Zip File
deleteFile(file);
}
}
}
My backgroundWorker1_ProgressChanged:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = "Downloading updates...";
}
And my backgroundWorker1_RunWorkerCompleted:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
settings_btn.Enabled = true;
start_btn_disabled.Enabled = false;
start_btn_disabled.Visible = false;
start_btn.Visible = true;
start_btn.Enabled = true;
progressBar1.Value = 100;
label1.Text = "Client is up to date!";
}
Also, a side note: I'm also having a bit problems of updating labels in backgroundWorker2_DoWork?
Any ideas?
Here's some working code which updates a label on Form1 using the BackgroundWorker.
Create a new Windows Form project and drop it in your code and it'll work.
It's super ugly, but it works.
After that, just plug your code into the DoWork method and calculate your value and send to ReportProgress.
Keep in mind that the work done in DoWork method is the actual Background Thread.
That means that in that method (DoWork) you cannot access UI (form) elements because they are on the UI thread.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FakeCountingWork();
}
private void FakeCountingWork()
{
int totalNumber = 100;
int progressCounter = 0;
while (progressCounter < totalNumber)
{
int fakecounter = 0;
for (int x = 0; x < 100000000; x++)
{
fakecounter++;
}
progressCounter++;
backgroundWorker1.ReportProgress(progressCounter);
}
}
}
################################## EDITED TO ADD OTHER FUNCTIONALITY
Okay, here's how you can implement a label which displays the number of bytes downloaded so far.
Add a second label named label2 to your form.
Next alter the following methods from my previous example.
Here we are going to use the UserState to pass an extra value to the ProgressChanged Event. It's very simple. You can see that I'm generating a random number and it will now appear in Label2. This is where you could show your number of bytes.
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
label2.Text = e.UserState.ToString();
}
private void FakeCountingWork()
{
int totalNumber = 100;
int progressCounter = 0;
Random rnd = new Random();
while (progressCounter < totalNumber)
{
int fakecounter = 0;
for (int x = 0; x < 100000000; x++)
{
fakecounter++;
}
progressCounter++;
updateValue = rnd.Next();
backgroundWorker1.ReportProgress(progressCounter,updateValue);
}
}
I would imagine this is because you are trying to update UI objects on a different thread. HAve you tried using the Dispatcher if using wpf? https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke(v=vs.110).aspx
or Invoke if using Winforms? https://msdn.microsoft.com/fr-ca/library/zyzhdc6b(v=vs.85).aspx
Edit:
As #daylight pointed out to be, the UI was being updated in the progresschanged event, which executes on the thread which created the background worker, therefore there shouldn't be an issue regarding threading. See https://msdn.microsoft.com/en-us/library/ka89zff4(v=vs.110).aspx for more info

BackgroundWorker WPF difficulties with Progress Bar

I am developing a WPF, C# application and I have to read a very big CSV file and write to a database. This process takes a long time so I want to at least show a progress bar that grows in size as it nears completition.
Now, I have the following code at the top:
private readonly BackgroundWorker worker = new BackgroundWorker();
Then inside the loop I have this:
worker.WorkerReportsProgress = true;
worker.ReportProgress((100 * i) / 10000);
and I have a private sub like this:
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
prgBar.Value = Math.Min(e.ProgressPercentage, 100);
}
Yet I don't see the progress bar updating or anything happening, program still hangs. Am I doing something wrong?
UPDATE: Tried the MSDN guide here http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx and I somehow failed to get it working with that example. I adjusted the looping part of course. Heh. It still stays idle and nothing updates. Heck, all I want to do is get a small counter that increases every time a line is read and added.
UPDATED CODE:
private BackgroundWorker bw = new BackgroundWorker();
public Process()
{
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
// Perform a time consuming operation and report progress.
int d = 0;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".csv";
dlg.Filter = "CSV File (*.csv)|*.csv";
dlg.Title = "file";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string filename = dlg.FileName;
List<String[]> fileContent = new List<string[]>();
using (FileStream reader = File.OpenRead(#filename)) // mind the encoding - UTF8
using (TextFieldParser parser = new TextFieldParser(reader))
{
using (SqlConnection conn = new SqlConnection(Classes.PublicVariables.Connection))
{
parser.Delimiters = new[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData)
{
string[] line = parser.ReadFields();
fileContent.Add(line);
SqlCommand comm = QUERYANDPARAMETERS
d += 1;
comm.ExecuteNonQuery();
worker.ReportProgress((d * 10));
}
}
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblCount.Content = "Complete";
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.lblCount.Content = (e.ProgressPercentage.ToString() + "%");
}
I am getting an excerption when the worker runs.
An exception of type 'System.NullReferenceException' occurred inSolution.exe but was not handled in user code
Additional information: Object reference not set to an instance of an object.
UPDATE3
Got it working! All I needed was the same code as above but this:
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
else
{
bw_DoWork(null, null);
}
In the event handler for the button press.
This will not compile as-is, but should get you started in the right direction:
private readonly BackgroundWorker worker
= new BackgroundWorker { WorkerReportsProgress = true };
public MainWindow()
{
InitializeComponent();
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
}
private void worker_DoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
// Do some long process, break it up into a loop so you can periodically
// call worker.ReportProgress()
worker.ReportProgress(i); // Pass back some meaningful value
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
prgBar.Value = Math.Min(e.ProgressPercentage, 100);
}
is the varible i int? if it is, the value of (100 * i) / 10000 will return 0 while i < 100. and the param of ReportProgress method should be a precent value. you can change it to worker.ReportProgress(i);,try it.

C# Multiple progress bars for multiple file downloads

I am writing a C# application for uploading and downloading file. The download uses WebClient object and its DownloadAsycDownload method. The download works fine for multiple files. It downloads as much files as I want.
My problem is I am not able to show the progress of all file in different progress bars which are dynamically added to the form's flowlayout control.
Here is my code:
public ProgressBar[] bar;
public int countBar=0;
...
bar[countBar] = new ProgressBar();
flowLayoutPanel1.Controls.Add(bar[countBar]);
countBar++;
request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownoadInProgress);
request.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
request.DownloadFileAsync(new Uri(this.uri), localPath);
byte[] fileData = request.DownloadData(this.uri);
FileStream file = File.Create(localPath);
file.Write(fileData, 0, fileData.Length);
file.Close();
}
public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
flowLayoutPanel1.Controls.Remove(bar[countBar]);
countBar--;
MessageBox.Show("Download Completed");
}
public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e)
{
bar[countBar].Maximum = total_bytes;
bar[countBar].Value = (int)e.BytesReceived;
}
You're using a count to index into progress bars, but once one is complete - you remove the last one, where you really should remove the one associated with the file.
I suggest, in this case, using a Dictionary<WebClient, ProgressBar> (might not be WebCliet - should be the type of sender in the events).
...
var progBar = new ProgressBar();
progBar.Maximum = 100;
flowLayoutPanel1.Controls.Add(progBar);
request.DownloadProgressChanged += DownoadInProgress;
request.DownloadFileCompleted += DownloadFileCompleted;
request.DownloadFileAsync(new Uri(this.uri), localPath);
dic.Add(request, progBar);
// You shouldn't download the file synchronously as well!
// You're already downloading it asynchronously.
// byte[] fileData = request.DownloadData(this.uri);
// FileStream file = File.Create(localPath);
// file.Write(fileData, 0, fileData.Length);
// file.Close();
Then, you can remove countBar altogether, and have the new methods:
public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
// Can remove (WebClient) cast, if dictionary is <object, ProgressBar>
var request = (WebClient)sender;
flowLayoutPanel1.Controls.Remove(dic[request]);
dic.Remove(request);
MessageBox.Show("Download Completed");
}
public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e)
{
var progBar = dic[(WebClient)sender];
progBar.Value = e.ProgressPercentage;
}
I would use something like this with lambdas, a bit more concise, no need in dictionary:
var webClient = new WebClient();
var pb = new ProgressBar();
pb.Maximum = 100;
flowLayoutPanel1.Controls.Add(pb);
webClient.DownloadProgressChanged += (o, args) =>
{
pb.Value = args.ProgressPercentage;
};
webClient.DownloadFileCompleted += (o, args) =>
{
flowLayoutPanel1.Controls.Remove(pb);
};
webClient.DownloadFileAsync(new Uri(this.uri), localPath);

Accessing ListView from backgroundworker

Hi have a ListView in the main forma called Dlist, i have a background worker to download a file, in the background worker how can i edit the subitem "Progress" in the main form listview ?
My Code:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, e) => WorkerDoWork(link, savepath,Dlist);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(progress_complete);
worker.ProgressChanged += new ProgressChangedEventHandler(progress_changed);
worker.RunWorkerAsync();
rivate void WorkerDoWork(string link, string savepath, ListView Dlist)
{
// Start Download
Uri url = new Uri(link);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
Int64 iSize = response.ContentLength;
Int64 iRunningByteTotal = 0;
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream streamRemote = client.OpenRead(new Uri(link)))
{
using (Stream streamLocal = new FileStream(savepath, FileMode.Create, FileAccess.Write, FileShare.None))
{
int iByteSize = 0;
byte[] byteBuffer = new byte[256];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
//Dlist.Items[Dlist.Items.IndexOfKey(fileName)].SubItems[2].Text = iProgressPercentage.ToString();
}
streamLocal.Close();
}
streamRemote.Close();
}
}
}
this line (//Dlist.Items[Dlist.Items.IndexOfKey(fileName)].SubItems[2].Text = iProgressPercentage.ToString();) it is to update the listview but i keep getting cross thread.
Why not use BackgroundWorker.ReportProgress method (see Examples section)?
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Because the backgroundworker is indeed a different thread, seperated from the UI thread you'll need to check wether an invoke is required or not for that usercontrol.
Below is a bit of sourcecode I use listView1 being your Dlist..
delegate void SetListViewItemCallBack(ListViewItem Item);
private void AddListViewItem(ListViewItem Item)
{
if (this.listView1.InvokeRequired)
{
SetListViewItemCallBack d = new SetListViewItemCallBack(AddListViewItem);
this.Invoke(d, new object[] { Item });
}
else
{
this.listView1.Items.Add(Item);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DataSet dsInfo = // whatever you want..
for (int i = 0; i < dsInfo.Tables[0].Rows.Count; i++)
{
ListViewItem li = new ListViewItem();
li.Text = dsInfo.Tables[0].Rows[i]["AXT_Tag"].ToString();
li.Tag = dsInfo.Tables[0].Rows[i]["AXT_ID"].ToString();
AddListViewItem(li);
}
}
Rather than worrying about having to use a delegate and calling it from the Do_Work method, I would set the BackgroundWorker.WorkerReportsProgress to true and subscribe to the BackgroundWorker.ProgressChanged event. Handling that event will occur on the UI thread, and you can use the progress reported (as called from the Do_Work method) by extracting it from the argument and updating your ListView from there.

Categories