1

I have a task, but processes inside it don't run in parallel. Second one waits for the first one to compelete. Can you explain why and how can I correct this? I want both of them run at the same time.

And second question, Should I use task instead of threads?

Thanks in advance.

 new Task(() =>
            {
                Counter1();
                Counter2();
            }).Start();




    private void Counter2()
    {
        for (int i = 0; i < 30; i++)
        {
            Thread.Sleep(500);
            label2.Text = i.ToString();
        }
    }

    private void Counter1()
    {
        for (int i = 0; i < 30; i++)
        {
            Thread.Sleep(500);
            label3.Text = i.ToString();
            if(i == 15)
                Thread.Sleep(3000);
        }
    }
4
  • 1
    Tasks run regular methods. You want two tasks. Commented Jan 19, 2014 at 19:07
  • I wait something like that: Parallel.Invoke. Can't do it? Commented Jan 19, 2014 at 19:08
  • 1
    Yes; that would also work. Commented Jan 19, 2014 at 19:09
  • Can you please show me if I want to make it with Task how can I achieve this? Commented Jan 19, 2014 at 19:11

1 Answer 1

3

Use Parallel.Invoke and call Counter1() and Counter2() as shown in following example from MSDN (after updating the () anonymous lambda invocation to invoke your 2 methods.

        #region ParallelTasks
        // Perform three tasks in parallel on the source array
        Parallel.Invoke(() =>
                         {
                             Console.WriteLine("Begin first task...");
                             GetLongestWord(words);
                         },  // close first Action

                         () =>
                         {
                             Console.WriteLine("Begin second task...");
                             GetMostCommonWords(words);
                         }, //close second Action

                         () =>
                         {
                             Console.WriteLine("Begin third task...");
                             GetCountForWord(words, "species");
                         } //close third Action
                     ); //close parallel.invoke

        Console.WriteLine("Returned from Parallel.Invoke");
        #endregion
Sign up to request clarification or add additional context in comments.

2 Comments

I know, I can do it in this way but I want to achieve this with Task. Is there a way?
AFAIK, this is the only way. From the same MSDN article, see The Parallel.Invoke method provides a convenient way to run any number of arbitrary statements concurrently. Just pass in an Action delegate for each item of work. The easiest way to create these delegates is to use lambda expressions. The lambda expression can either call a named method or provide the code inline. The following example shows a basic Invoke call that creates and starts two tasks that run concurrently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.