简体   繁体   中英

Start second method after first method is finished with thread functionality c#

I have a method which is rebuilding the product catalog of a webshop. This is neccessary after I change some product information. After the rebuilding method I would like to start a second method to generate full text index of the webshop. I can watch the status of the first method (RebuildCatalog). If the status is "RebuildFinished" then I would like to start the second method (GenerateFullTextIndex). I would like to use Threads functionality. Does someone can create an example of how to implementate this scenario?

I would like to use Threads functionality.

It really doesn't sound like you do. Starting one method after another finishes is as simple as:

var status = RebuildCatalog();
if (status == Status.RebuildFinished)
{
    GenerateFullTextIndex();
}

No threading required. If you really think you need multiple threads, you should explain why you think they'll help. At what point do you need to perform multiple tasks concurrently?

Well, if you want to use a multiple threads and oranize your calls in chain so they are executed on another thread, but in sequence, and you are using .NET Framework 4.0> , you can use a Task Parallelism , like for example using Task::ContinueWith method.

Example (preudocode from MSDN):

Task<byte[]> getData = new Task<byte[]>(() => GetFileData());
Task<double[]> analyzeData = getData.ContinueWith(x => Analyze(x.Result));
Task<string> reportData = analyzeData.ContinueWith(y => Summarize(y.Result));
getData.Start();

            //or...
Task<string> reportData2 = Task.Factory.StartNew(() => GetFileData())
              .ContinueWith((x) => Analyze(x.Result))
              .ContinueWith((y) => Summarize(y.Result));

Using events would seem to be simpler than watching the status.

In your rebuild catalog code fire a "finished" event on completion:

public event EventHandler<EventArgs> RebuildFinished;

private void Rebuild(...)
{
    // Rebuild the catalog

    this.RebuildFinished(this, new EventArgs(...));
}

Then handle it:

this.catalog.RebuildFinished += this.RebuildFinished;

private void RebuildFinished(object sender, EventArgs e)
{
    // Rebuild the index
}

Now both of these can (and probably should) be using threads to ensure that the UI of your application stays responsive:

this.catalogThread = new Thread(new ThreadStart(this.catalog.Rebuild));

As I can assume from your question your rebuilding method probably takes up considerable time and that is why you want to run in a separate thread. Therefore I would suggest implementing Event based async pattern . When your rebuilding (async) method finishes it will throw finished event with AsyncCompletedEventArgs (which you can subclass to pass your result status) and from there you will start your second method.

BackgroundWorker bw1 = new BackgroundWorker();//To rebuild catalog.
BackgroundWorker bw2 = new BackgroundWorker();//To generate text.

public Form1()
{
    InitializeComponent();

    bw1.DoWork += bw1_DoWork;
    bw1.RunWorkerCompleted += bw1_RunWorkerCompleted;
    bw2.DoWork += bw2_DoWork;
    bw2.RunWorkerCompleted += bw2_RunWorkerCompleted;

    bw1.RunWorkerAsync();//Start new thread. - Rebuild catalog.
}

void bw1_DoWork(object sender, DoWorkEventArgs e)
{
    //Rebuild catalog.
}

void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    bw2.RunWorkerAsync();//Generate text.
}

void bw2_DoWork(object sender, DoWorkEventArgs e)
{
    //Generate text.
}

void bw2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //Whatever...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM