I have my own solution to import monthly sales data in my windows form application. When a user click on import
button, the program is actually running but it looks like it's not responding. The process takes a long time about 5 minutes.
So, I'd like to implement a progress bar with status strip label to display as an user interface and let the users know how much the task is done. This is also my first time using a progress bar in my program. So, I read through some tutorials which show how to use it. Some people use progress bar with background worker and timer.
But I don't understand where I should use the solution that I have. In background worker DoWork()
event? I don't want to fake it by abusing the progress bar like setting the progressBar.Maximum = 100, progressBar.Value = 0 and as long as the timer is ticking increasing the value by 5. The progress bar must report the actual progress while the program is running.
The following is the solution I am using now to import the data:
private void btnImport_Click(object sender, EventArgs e)
{
if (lsbxBrowsedFiles.Items.Count != 0)
{
ArrayList salesHeaderArr = new ArrayList();
ArrayList salesDetailArr = new ArrayList();
int i = 0;
while (i < browsedXmlFileList.Count)
{
if (browsedXmlFileList[i].ToUpper().EndsWith("SALESHEADER.XML"))
{
salesHeaderArr.Add(browsedXmlFileList[i]);
}
if (browsedXmlFileList[i].ToUpper().EndsWith("SALESDETAIL.XML"))
{
salesDetailArr.Add(browsedXmlFileList[i]);
}
i++;
}
if (selectedFileIsNotInDestinationFolder(salesHeaderArr, salesDetailArr) == true)
{
i = 0;
while (i < salesHeaderArr.Count)
{
SalesHeader salesHeader = new SalesHeader();
string sourceFilePath = salesHeaderArr[i].ToString();
readXMLFiles(sourceFilePath, SALES_HEADER);
SalesHeader salesCheck = (SalesHeader)salesHeaderList[0];
string checkOutletCode = salesCheck.OutletCode;
DateTime checkBusDate = salesCheck.BusinessDate.Value;
if (SalesHeader.IsThisRowAlreadyImportedInSalesHeader(checkOutletCode, checkBusDate) == false)
{
salesHeader.ImportSalesHeader(salesHeaderList);
salesHeader.CreateImportDataLog(getDestinationFilePath(sourceFilePath),
DateTime.Now, salesHeaderList.Count, SALES_HEADER);
}
else
{
string errorDate = checkBusDate.ToString("dd MMMM, yyyy");
MessageBox.Show("Selected XML File with BusinessDate: " + errorDate + " has been already imported.",
"ABC Cafe Import Sales Wizard");
MessageBox.Show("Please select a file which has not been imported!",
"ABC Cafe Import Sales Wizard");
return;
}
MoveXMLFiletoDestinationFolder(sourceFilePath);
i++;
}
i = 0;
while (i < salesDetailArr.Count)
{
SalesDetail salesDetail = new SalesDetail();
string sourceFilePath = salesDetailArr[i].ToString();
readXMLFiles(sourceFilePath, SALES_DETAIL);
SalesDetail salesCheck = (SalesDetail)salesDetailList[0];
string checkOutletCode = salesCheck.OutletCode;
DateTime checkBusDate = salesCheck.BusinessDate.Value;
if (SalesDetail.IsThisRowAlreadyImportedInSalesDetail(checkOutletCode, checkBusDate) == false)
{
salesDetail.ImportSalesDetail(salesDetailList);
salesDetail.GenerateCarryForward(salesDetailList);
salesDetail.CalculateImportInventoryBalance(salesDetailList);
salesDetail.CreateImportDataLog(getDestinationFilePath(sourceFilePath), DateTime.Now, salesDetailList.Count, SALES_DETAIL);
}
else
{
string errorDate = checkBusDate.ToString("dd MMMM, yyyy");
MessageBox.Show("Selected XML File with BusinessDate: " + errorDate + " has been already imported.",
"ABC Cafe Import Sales Wizard");
MessageBox.Show("Please select a file which has not been imported!",
"ABC Cafe Import Sales Wizard");
return;
}
MoveXMLFiletoDestinationFolder(sourceFilePath);
i++;
}
MessageBox.Show("Import has been successfully completed!",
"ABC Cafe Import Sales Wizard");
clearListBoxItems();
lblMessage.Visible = false;
}
//Abort the import operation here!
else
{
MessageBox.Show("Please select a file which has not been imported!",
"ABC Cafe Import Sales Wizard");
clearListBoxItems();
lblMessage.Visible = false;
}
}
else
{
MessageBox.Show("Please select XML files to import!",
"ABC Cafe Import Sales Wizard");
}
}
Any help will be very much appreciated!