Should i use Task.Run or Background worker for long running operation

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
private async void button1_Click(object sender, EventArgs e)
{
try
{
var text = textBox1.Text;
var progress = new Progress<string>((x) => label1.Text = x);
await Task.Run(() => DoWork(progress, text));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void DoWork(IProgress<string> progress, string text)
{
foreach (string file in Directory.EnumerateFiles("\\\\Mypcname-PC\\vxheaven\\malware"))
{
count++;
progress.Report(Convert.ToString(count));
if (file.Contains(text))
{
progress.Report(Convert.ToString(count) + " reached the file");
break;
}
}
}


OR background worker

backgroundWorker1.RunWorkerAsync(2000);

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

}
i am doing long running operation which load huge data in step & store in memory. which causes application freezing.

please tell me should i use background worker OR Task.Run

Continue reading...
 
Back
Top