Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:08:03+00:00 2026-06-15T01:08:03+00:00

I got confused why when I used task the time of stopwatch will display

  • 0

I got confused why when I used task the time of stopwatch will display but when I used thread it will not. What’s wrong with my code? Am i missing something?

static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();

        //if I used this sw.Elapsed will display
        //Task t1 = Task.Factory.StartNew(runTask1);
        //Task t2 = Task.Factory.StartNew(runTask2);
        //Task.WaitAll(t1, t2);

        //if I used this sw.Elapsed will not display
        //Thread t1 = new Thread(runTask1);
        //Thread t2 = new Thread(runTask2);
        //t1.Start();
        //t2.Start();

        sw.Stop();

        Console.WriteLine(sw.Elapsed);

        Console.ReadLine();
    }

    public static void runTask1()
    {
        for (int x = 1; x <= 5000; x++)
        {
            Console.WriteLine("Run task tester 1");
        }
    }
    public static void runTask2()
    {
        for (int x = 1; x <= 5000; x++)
        {
            Console.WriteLine("Run task tester 2");
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:08:04+00:00Added an answer on June 15, 2026 at 1:08 am

    When you’re using tasks, you wait for them to complete their work before stopping the stopwatch and displaying the time. When you’re using threads you don’t wait for them to finish before displaying the result therefore it is printed at the top above the text from the threads.

    You want to wait for the threads to finish as well:

    static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
    
        // add Thread.Join at the end
        Thread t1 = new Thread(runTask1);
        Thread t2 = new Thread(runTask2);
        t1.Start();
        t2.Start();
        t1.Join();
        t2.Join();
    
        sw.Stop();
    
        Console.WriteLine(sw.Elapsed);
    
        Console.ReadLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code and got myself confused: I have a query that
I'm a bit confused at why I'm getting the ERROR 'data argument not used
read many posts about the difference between the 3 idioms. But got more confused,
I got some path while working on a project but not able to figure
I am trying to rotate vectors using matrices, but got confused. I thought all
I got confused with Git !! I have some files which have added and
I've got confused . We hear a lot about Provider in ASP.NET . Membership-Provider
today I got confused when doing a couple of <%=Html.LabelFor(m=>m.MyProperty)%> in ASP.NET MVC 2
I was experimenting with array of pointers address calculation arithmetic.I got confused with the
Today i was reading about pure function, got confused with its use: A function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.