Having a large sincronous code that use BinaryFormatter to serialize several tables how can i update progressbar ?
the code that perform serialization is :
private void Serialize()
{
try
{
int numberTableProcessed = 0;
txtBoxProcessing.Text += "Start ..." + DateTime.Now + Environment.NewLine;
AGENTS agenti = new AGENTS();
agenti.SelectAndSerializeAgenti(openedConn);
string pathAgenti = @"C:\...AGENTS.bin";
if (File.Exists(pathAgenti))
{
long size = new FileInfo(pathAgenti).Length;
txtBoxProcessing.Text += "Processed AGENTI - " + string.Format("byte: {0:n}", size) + Environment.NewLine;
}
numberTableProcessed++;
UpdateProgressBar(numberTableProcessed);
PRODUCT anaart = new PRODUCT();
anaart.SelectAndSerializeAnaart(openedConn);
string pathAnaart = @"C:\...PRODUCT.bin";
if (File.Exists(pathAnaart))
{
long size = new FileInfo(pathAnaart).Length;
txtBoxProcessing.Text += "Processed PRODUCT - " + string.Format("byte: {0:n}", size) + Environment.NewLine;
}
numberTableProcessed++;
UpdateProgressBar(numberTableProcessed);
...
}
the code that update the progressbar
private void UpdateProgressBar(int numberTableProcessed)
{
int numberTotalTables = 22;
progressBar1.Value = (numberTableProcessed / numberTotalTables) * 100;
}
Using the method UpdateProgressBar i not see progress...it becomes full green at the end because the code is running sincronous (i think)
how can update progressbar?
I try this based on Lucifer suggest
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
}
public void PerformSerialize()
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Serialize();
}
private void btnSerializza_Click(object sender, EventArgs e)
{
//Serialize();
PerformSerialize();
}
public void UpdateProgressBar(int numberTableProcessed)
{
if (this.IsHandleCreated)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { UpdateProgressBar(numberTableProcessed); });
return;
}
else
{
int numberTotalTables = 22;
progressBar1.Value = (numberTableProcessed / numberTotalTables) * 100;
progressBar1.Increment(numberTotalTables);
}
}
}