C # Threading in Method

I have the following method:

public List<string> someMethod()
{

   // populate list of strings
   // dump them to csv file
   //return to output
}

Question: I do not want the user to wait for the csv dump, which may take some time. If I use a stream for csvdump, will it be complete? before or after returning output?

After csvdump is complete, I would like to notify another class about the processing of the csv file. someMethod no need to wait for csvdump to complete?

+5
source share
3 answers

This is a great article on C # thread that anyone who wants to know about C # Threading should read it.

But for your situation, I would do something like this:

class Program {
  static BackgroundWorker bw;
  static void Main() {
    bw = new BackgroundWorker();
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += bw_DoWork;
    bw.RunWorkerCompleted += bw_RunWorkerCompleted;

    bw.RunWorkerAsync ();
  }

  static void bw_DoWork (object sender, DoWorkEventArgs e) {
    //Run your code here
  }

  static void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e) {
    //Completed
  }
}

If the process is likely to take some time, you can get BackgroundWorker to report its progress.

+4

, , .

.NET, .

() ThreadPool , . - ( ).

, , . 99% , , , -, , .

+2

WinForms WPF, BackgroundWorker CVS, , .

+2
source

All Articles