Po wciśnięciu przycisku odpala mi się backgroundWorker:
private void send_Click(object sender, EventArgs e)
{
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
A w nim wykonuje się taka funkcja:
public void SplitFile(string inputFile, int chunkSize, string path)
{
const int BUFFER_SIZE = 20 * 1024;
byte[] buffer = new byte[BUFFER_SIZE];
using (Stream input = File.OpenRead(inputFile))
{
int index = 0;
int partCount = (int)Math.Ceiling((double)input.Length / (double)chunkSize);
while (input.Position < input.Length)
{
using (Stream output = File.Create(path + "\\" + index + ".me"))
{
int remaining = chunkSize, bytesRead;
while (remaining > 0 && (bytesRead = input.Read(buffer, 0, Math.Min(remaining, BUFFER_SIZE))) > 0)
{
output.Write(buffer, 0, bytesRead);
remaining -= bytesRead;
}
}
index++;
//int myValue = (index / partCount);
Thread.Sleep(500);
}
}
}
Zależy mi, żeby postęp dzielenia plików z tej funkcji był widoczny w progessbarze:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
SplitFile(fullPathFileToUpload, 9437184, AppDomain.CurrentDomain.BaseDirectory);
//backgroundWorker1.ReportProgress(myValue);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
uploadCreatorProgressbar.Value = e.ProgressPercentage;
}
I mam problem z zakomentowanymi fragmentami: jak wyświetlić w progressbarze taki postęp? Załóżmy, że w funkcji SplitFile partCount ma wartość 7, a index inkrementuje się od 1 właśnie do 7. Tyle, że nie wiem jak go zwrócić (nie przerywając pętli/funkcji), żeby dobrze wyświetlić pasek postępu.