im making file transfer (Server-Client) TCP
i’ve already looked for same questions like this one .. but no answer worked for me ..
ProgressBar doesn’t update with backgroundworker .. i’ve searched for tutorials to do this .. and i exactly followed the steps.
the form lags while sending and after the file sent.. the progressbar goes to 100%
The file sent succesfully the send method code works fine… my problem just with updating the progressbar .. how do i fix that ??
Here i call (backgroundWorker1.RunWorkerAsync)
public void Send(string destPath)
{
if (listView1.Items.Count > 0)
{
List<String> job = new List<string>();
job.Add(listView1.Items[0].ToolTipText);
job.Add(destPath);
backgroundWorker1.RunWorkerAsync(job);
}
}
DoWork Method
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
List<string> job = (List<string>)e.Argument;
SendFile(job[0],job[1]);
}
here’s the SEND method which i use (backgroundWorker1.ReportProgress)
private void SendFile(string srcPath, string destPath)
{
string dest = Path.Combine(destPath, Path.GetFileName(srcPath));
using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
{
try
{
long fileSize = fs.Length;
sizeAll = fileSize;
long sum = 0;
int count = 0;
data = new byte[fs.Length];
SendCommand("receive<" + dest + "<" + fs.Length.ToString());
while (sum < fileSize)
{
if (fileSize - sum < packetSize)
{
count = fs.Read(data, 0, (int)(fileSize - sum));
network.Write(data, 0, (int)(fileSize - sum));
}
else
{
count = fs.Read(data, 0, data.Length);
network.Write(data, 0, data.Length);
}
fs.Seek(sum, SeekOrigin.Begin);
sum += count;
sumAll += count;
backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
}
network.Flush();
}
finally
{
CloseTransfer();
}
}
}
and here is backgroundWorker1_ProgressChanged
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBarFile.Value= e.ProgressPercentage;
}
thanks for [Nikola Markovinović] his answer was:
the line that causes the error was :
after correcting the code :