-1

I am practicing multitasking and tried this code but it doesn't work as I expected it to.

I was expecting for the the workTask to run first printing "working..." throughout the loop then continue with newTask and print "moreWorkThread: working..." throughout the loop. At first I thought "workThread: Done" will be printed at the end but then I understood from output that it can run anytime between the other ones.

I don't understand the rest of the behaviour at all.
newTask doesn't run at all and the loop in someWork never gets completed either.

Can somebody please explain it to me? I want to know why this code is behaving so and what mods can I make to give expected output

  • run someWork
  • complete the loop
  • continueWith moreWork
  • complete loop.

    public static void Main() {  
        Task workTask = new Task(someWork);
        workTask.Start();
    
        Console.WriteLine("WorkThread: Done!");
    
        Task newTask = workTask.ContinueWith(moreWork);
    
    }
    
    static public void someWork()
    {
        for(int i = 0; i < 16; i++)
        {
            Console.WriteLine("WorkThread: working...");
        }
    }
    static public void moreWork(Task task)
    {
        for(int i = 0; i < 8; i++)
        {
            Console.WriteLine("moreWorkThread: working...");
        }
    }
    

sample output:

WorkThread: Done!

WorkThread: working...

WorkThread: working...

René Vogt
  • 43,056
  • 14
  • 77
  • 99
Mark S
  • 347
  • 1
  • 4
  • 12
  • 6
    Welcome to Stack Overflow. Unfortunately you haven't told us what you expected or what you saw, which makes it very hard to "explain" anything... but you've included a (nearly) complete program, which is a good start. Just clarify what you expected, and I'm sure we can help. – Jon Skeet Mar 11 '16 at 20:49
  • sorry...editing now.... – Mark S Mar 11 '16 at 20:50
  • One thing to note is that the main thread does not wait for the thread-pool threads to complete before existing. This means that the application will terminate before anything useful happens. Try putting Console.ReadLine at the end of the `main` method. – Yacoub Massad Mar 11 '16 at 20:51
  • If you're expecting workthread done! to be the last output, that's not the case because once your workTask starts, the rest of your code will keep executing at the same time (that's the point of multi-threading). So your other thread starts, and then you immedially say "Done!" even though it's still processing the thread. – dustinroepsch Mar 11 '16 at 20:52
  • that much i understood from the output...i was dumb to put it there anyway...i shouldve known that while putting it there...but I dont understand the rest of the output...loops dont complete at all...moreWork doesn't even get executed??? – Mark S Mar 11 '16 at 21:02
  • Though not exactly a duplicate, it will nonetheless give you an idea - http://stackoverflow.com/questions/5209591/are-tasks-created-as-background-threads , https://msdn.microsoft.com/en-us/library/h339syd0(v=vs.110).aspx. – Eugene Podskal Mar 11 '16 at 21:11
  • Yacoub Massad 's comment solved the issue. I understand what was wrong with the program now...Thanks a lot – Mark S Mar 11 '16 at 21:22

1 Answers1

1
var task = Task.Run(() =>
        {
            someWork();
        });
        await task;

        Console.WriteLine("WorkThread: Done!");
        await task.ContinueWith((t) => {morework() } );

You'd better use async await. Await will wait for somework to finish executing first before you say "done".

Jay
  • 92
  • 3
  • your approach seems much better...but whats the difference between task.Run() and task.Start()? – Mark S Mar 11 '16 at 21:27
  • I think you already know the answer, but to explain a bit, Task.Run just queues a task in the threadpool and the processor will decide when it will execute the task that you queued. Task.Start() will force your task to execute. – Jay Mar 11 '16 at 21:55