user4355390
user4355390

Reputation:

Block Task.Run method until done

I've got some methods that I need to run, one of them should run as a different Thread so I use a Task.Run with lambda, though I want the next method to start just after the Task finishes.

for example I want LastJob() will start after MoreWork() is done:

public void DoSomeWork()
{
    Task.Run(() => MoreWork());
    LastJob();
}

Upvotes: 1

Views: 497

Answers (2)

Anders
Anders

Reputation: 1643

Coy could use the ContinueWith method, like this:

public void DoSomeWork()
{
    var task = Task.Run(() => MoreWork());
    task.ContinueWith(() => LastJob()) ;
}

Upvotes: 0

Sagiv b.g
Sagiv b.g

Reputation: 31024

you can use the async and await key words link

public async void DoSomeWork()
{
    await Task.Run(() => MoreWork());
    LastJob();
}

Upvotes: 1

Related Questions