简体   繁体   English

在第一个方法完成线程功能后启动第二个方法 c#

[英]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).我可以看到第一个方法 (RebuildCatalog) 的状态。 If the status is "RebuildFinished" then I would like to start the second method (GenerateFullTextIndex).如果状态为“RebuildFinished”,那么我想启动第二种方法(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.好吧,如果你想使用多线程并在中组织你的调用,以便它们在另一个线程上执行,但按顺序执行,并且你正在使用.NET Framework 4.0> ,你可以使用Task Parallelism ,例如使用Task: :ContinueWith方法。

Example (preudocode from MSDN):示例(来自 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:现在,这两者都可以(而且可能应该)使用线程来确保应用程序的 UI 保持响应:

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.当您的重建(异步)方法完成时,它将抛出带有 AsyncCompletedEventArgs 的完成事件(您可以将其子类化以传递您的结果状态),然后您将从那里开始您的第二个方法。

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...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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