Using multiple background workers is ok?

  • Thread starter Thread starter pooh9594
  • Start date Start date
P

pooh9594

Guest
I have 3 tabs and in each tab there is a listbox.

I have used background worker for each listbox.

Is there any other way of doing this?

Here is my code.


public List<string> getFiles()
{
List<string> found = new List<string>();
String date = DateTime.Now.ToString("dd-MM-yyyy");
var path = Properties.Settings.Default.path;



if (path == "")
{

return found;
}


else
{
try
{
var files = Directory.GetFiles(path).Where(filename => filename.Contains(date)).ToList();


if (files != null)
{

foreach (var file in files)
{
using (StreamReader filess = new StreamReader(@file))
{
string line;
while ((line = filess.ReadLine()) != null)
{

if (line.StartsWith("SFTP") || line.StartsWith("ERROR"))

found.Add(line);

}
}
}
}
}
catch (Exception ex)
{
throw ex;

}


}


found.Reverse();
return found;
}


private void Form1_Load(object sender, EventArgs e)
{

backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.RunWorkerAsync();

backgroundWorker2.WorkerReportsProgress = true;
backgroundWorker2.DoWork += backgroundWorker2_DoWork;
backgroundWorker2.ProgressChanged += backgroundWorker2_ProgressChanged;
backgroundWorker2.RunWorkerCompleted += backgroundWorker2_RunWorkerCompleted;
backgroundWorker2.RunWorkerAsync();


backgroundWorker3.WorkerReportsProgress = true;
backgroundWorker3.DoWork += backgroundWorker3_DoWork;
backgroundWorker3.ProgressChanged += backgroundWorker3_ProgressChanged;
backgroundWorker3.RunWorkerCompleted += backgroundWorker3_RunWorkerCompleted;
backgroundWorker3.RunWorkerAsync();


notifyIcon1.Visible = true;
notifyIcon1.Icon = SystemIcons.Exclamation;
this.Hide();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{


List<string> result = new List<string>();

var found = obj.getFiles();
if (found.Count!=0)
{

for (int i = 0; i < found.Count; i++)
{
int progress = (int)(((float)(i + 1) / found.Count) * 100);

result.Add(found);

(sender as BackgroundWorker).ReportProgress(progress, found);

System.Threading.Thread.Sleep(500);
}



e.Result = result;

}
else
{ MessageBox.Show("Data not found... "); }
}





private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{

if (e.UserState != null)

listBox1.Items.Add(e.UserState);
pop.Show();


}

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
List<string> result1 = new List<string>();

var found = obj.getFiles();

foreach (var item in found)
{
if (item.Contains("ERROR"))
{
result1.Add(item);

(sender as BackgroundWorker).ReportProgress(0, item);

}
else
(sender as BackgroundWorker).ReportProgress(0);
System.Threading.Thread.Sleep(1000);

}
e.Result = result1;
}

private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Popup p = new Popup();
//p.Close();
if (e.UserState != null)
{

listBox2.Items.Add(e.UserState);
pop.SetLable(e.UserState.ToString());

pop.Hide();


}



}

private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Popup p = new Popup();
//p.Hide();

}

private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{


List<string> result = new List<string>();

var found = obj.getFiles();
if (found.Count != 0)
{

for (int i = 0; i < found.Count; i++)
{
int progress = (int)(((float)(i + 1) / found.Count) * 100);
if (found.Contains("SFTP"))
{
result.Add(found);

(sender as BackgroundWorker).ReportProgress(progress, found);
}
System.Threading.Thread.Sleep(500);
}



e.Result = result;

}
}

private void backgroundWorker3_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.UserState != null)
listBox3.Items.Clear();
listBox3.Items.Add(e.UserState);

}

Is there any other way of doing this in effective way.

Continue reading...
 

Similar threads

E
Replies
0
Views
124
elfenliedtopfan55
E
L
Replies
0
Views
160
LearningVisualC2005
L
Back
Top