How to run 3 methods one after another in c# using threading?

我与影子孤独终老i 提交于 2019-12-11 04:43:24

问题


I've three methods i.e. Method1,Method2 & Method3.

  • Method1 is for downloading images from one site
  • Method2 is for downloading images from 2nd site
  • Method3 is for comparing images

Method1 gets the list of image urls that is being added to list according to product id one by one i.e. there's a loop on a collection of multiple product ids then according to each product id I get a collection of images that I add to a list.
Method1 downloads the images according to that list.

Method2 is same as Method1 but downloads the images from second site.

Method3 compares the images that are downloaded according to each product id of both sites.

What I want is that Method1 downloads the collection of images then runs Method2 which downloads the images of second site and then Method2 runs Method3 which compares the images.
I am using threading to download and compare these images but the image comparison couldn't compare the images because it compares the images while the images are being downloaded.


回答1:


What you could do is:

private Thread tMethod1 = new Thread(runMethod1);
private Thread tMethod2 = new Thread(runMethod2);
private Thread tMethod3 = new Thread(runMethod3);


private void runThreads();
{
    tMethod1.Start(); //starts method 1
    tMethod2.Start(); //starts method 2
    tMethod1.Join();  //waits for method 1 to finish
    tMethod2.Join();  //waits for method 2 to finish
    tMethod3.Start(); //starts method 3
    tMethod3.Join();  //waits for method 3 to finish
}

private void runMethod1()
{
    Method1();
}
private void runMethod2()
{
    Method2();
}
private void runMethod3()
{
    Method3();
}

This will run Method1 and Method2 simultaniously and waits for those to finish before the Method3 is started.
It's a little work around, but works.




回答2:


I think first two methods can be called asynchronously, so you can write something like this:

Task<List<Image>>[] tasks = new Task<int>[2];
tasks[0] = new Task<List<Image>>(() => Method1());
tasks[1] = new Task<List<Image>>(() => Method2());

var continuation = Task.Factory.ContinueWhenAll(
                   tasks,
                   (antecedents) =>
                   {
                        Method3(tasks[0].Result,tasks[1].Result);
                   });
tasks[0].Start();
tasks[1].Start();
var comparisonResult = continuation.Result;



回答3:


Assuming that you are using .NET 4.5 (you didn't specify a version in your tags) you can use async and await. Here is a simplified example:

public async void DownloadImages()
{
    // Do something...

    await Method1();
    await Method2();
    await Method3();

    // Do something else...
}



回答4:


Sounds like you have a multiple-producer-single-consumer problem, with the downloading tasks being producers and giving images to the consumer task to be compared. There is high potential for parallelism here:

Download task 1
                 \
                    Compare task
                 /
Download task 2   

You can achieve parallelism both between the first two tasks that can run in parallel as well as with the comparison task using a pipeline model.

But how to implement all this? What you can do is start 3 parallel tasks, one for each of the above. Each of the two download tasks will own a blocking collection (BlockingCollection) and put images as they get downloaded. Assuming corresponding images (with the same product id) arrive in order, the compare task can then wait on both collections and compare the images as they arrive.




回答5:


Why not just run the methods all in one thread?

private void Thread_DoStuff(){
Method1();
Method2();
Method3();
}

Alternatively, you can use threadpools. Further information about threadpools can be found here.. This can be achieved in ThreadPool by using WaitHandle.WaitAll(); then proceed to compare after the two threads have finished




回答6:


Try with Task and ContinueWith method. It's going to create a new thread and call each method sequential.

You can find more details about that in this article.



来源:https://stackoverflow.com/questions/13508146/how-to-run-3-methods-one-after-another-in-c-sharp-using-threading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!