I have very simple WinForms application which uses class that collects countries and regions from some web-service and store collected data into file. This CollectData class worked perfect in console application, and inform about each step via Console.WriteLine
My code looks like:
public class CollectData
{
public string OutputFile { get; private set; }
public CollectData(string outputFile)
{
OutputFile = outputFile;
}
public void ProceedCollectingData()
{
Console.WriteLine("Getting countries...");
List<string> countries = GetWorldCountries();
foreach (string country in countries)
{
Console.WriteLine("Getting regions of country: " + country);
List<string> regions = GetCountryRegions(country);
AppendToFile(OutputFile, country, regions);
}
}
private List<string> GetWorldCountries()
{
List<string> list = new List<string>();
System.Threading.Thread.Sleep(20000); // here service fills list of countries.
return list;
}
private List<string> GetCountryRegions(string country)
{
List<string> list = new List<string>();
System.Threading.Thread.Sleep(20000); // here service fills list of regions.
return list;
}
private void AppendToFile(string outputFile, string country, List<string> regions)
{
// Save everything into output file.
}
}
and WinForm code: private void btnCollectData_Click(object sender, EventArgs e) { CollectData collectData = new CollectData(tbOutputFile.Text.Trim()); collectData.ProceedCollectingData();
//???? what should be here
backgroundWorker.RunWorkerAsync(collectData);
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
//???? what should be here?
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//???? what should be here?
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBarCountries.Value = 100;
progressBarRegions.Value = 100;
lblCurrentlyProgressingStatus.Text = "Done";
}
What is wrong and what should I change in my class, what should I open, return from class methods or redevelop, what is the right way to develop and inform progressBars about internal class logic?
I seacrhed over internet and all examples are like simple "for" loop inside backgroundWorker_DoWork with Thread.Sleep, but I have different situation.
Appreciate any clarification replies! Thanks!
System.Threading.Thread.Sleep(20000)
? – King King