question

WaiteJooyoun-0064 avatar image
0 Votes"
WaiteJooyoun-0064 asked karenpayneoregon commented

Can Someone to provide me whole package of below example?

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-5.0

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@WaiteJooyoun-0064, do you want the reference of the example(Nuget-Package) or the complete project of the code example?

0 Votes 0 ·
WaiteJooyoun-0064 avatar image
0 Votes"
WaiteJooyoun-0064 answered Castorix31 commented

Complete Project of the code example.


Dear Jack,

I am beginner of Windows (dotNet etc) platform.
I tried to follow the example document as in MS, but I couldn't figure out with given wordy explanation.


I couldn't figure out how to create (in Form design) 'backgroundWorker1' parameter as in the step.

If you provide me a whole project of example, it will be helpful.

Thank you for your support
Jooyoun

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You just have to copy the whole class
public class FibonacciForm : System.Windows.Forms.Form
in a new .Net Framework project
and in program.cs , replace

 Application.Run(new Form1());

by

  Application.Run(new FibonacciForm());

and it works...

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Consider not using a BackgroundWorker as per the following code sample.


 using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
    
 namespace WindowsFormsApp1
 {
     public partial class Form1 : Form
     {
         private CancellationTokenSource _cts = new CancellationTokenSource();
         public Form1()
         {
             InitializeComponent();
             StatusLabel.Text = "";
         }
         private async Task AsyncMethod(IProgress<int> progress, CancellationToken ct)
         {
    
             for (int index = 100; index <= 120; index++)
             {
                 //Simulate an async call that takes some time to complete
                 await Task.Delay(200);
    
                 if (ct.IsCancellationRequested)
                 {
                     ct.ThrowIfCancellationRequested();
                 }
    
                 if (progress != null)
                 {
                     progress.Report(index);
                 }
    
             }
    
         }
    
         private async void StartButton_Click(object sender, EventArgs e)
         {
             var cancelled = false;
             if (_cts.IsCancellationRequested == true)
             {
                 _cts.Dispose();
                 _cts = new CancellationTokenSource();
             }
    
    
             var progressIndicator = new Progress<int>(ReportProgress);
             try
             {
                 await AsyncMethod(progressIndicator, _cts.Token);
             }
             catch (OperationCanceledException ex)
             {
                 StatusLabel.Text = "Cancelled";
                 cancelled = true;
             }
    
             if (cancelled)
             {
                 await Task.Delay(1000);
                 StatusLabel.Text = "Go again!";
             }
         }
    
         private void CancelButton_Click(object sender, EventArgs e)
         {
             _cts.Cancel();
         }
         private void ReportProgress(int value)
         {
             StatusLabel.Text = value.ToString();
             TextBox1.Text = value.ToString();
         }
     }
 }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WaiteJooyoun-0064 avatar image
0 Votes"
WaiteJooyoun-0064 answered karenpayneoregon commented

Dear Karen,


Your answer worked for me. Before I close the case, I would like to inquire why you commented

'Consider not using a BackgroundWorker '
[Q1]
and suggested different way of solution?

BackgroundWorker is obsolete?

[Q2]
Also, in your suggested code,
await Task.Delay(200);

Do you have advice on 'how to determine the amount of time in my own project' when we need delay for Async?


Thank you
Jooyoun

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

For question 1 read through the following series. Most developers who use BackgroundWorker component perform simple operations and the component is suffice for those but as time goes by with more emphasis on responsiveness Task are better than BackgroundWorker components.

For question 2, there is no magic number and in many cases Task.Delay is not needed and in some cases when needed and a cancellation request may be a requirement Delay accepts a CancellationToken and on a side note a CancellationToken can be used when connecting to a SQL-Server for shortening timeouts.

Try the following out to get an idea how to use Tasks. If using SQL-Server check out the following.

Finally a simple task with cancellation here.


1 Vote 1 ·

No, the answer if off-topic
and BS : BackgroundWorker is used everywhere and not obsolete at all
copy the BackgroundWorker sample from Microsoft, it works very well

0 Votes 0 ·

Yes they are used in many places but that does not make it right. Programmers and novice coders tend to find a comfort zone and stick with it even in many cases there are better ways to write code.

0 Votes 0 ·