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

  • Home
  • SEARCH
  • 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 8308327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:46:36+00:00 2026-06-08T18:46:36+00:00

I am trying to get 2 threads running in the background to perform tasks.

  • 0

I am trying to get 2 threads running in the background to perform tasks. I have to create the threads sequentially and proceed with the program execution. But the second thread must execute it’s work only when the first finishes. Also, One more clarification. I am looking to have this solution on a WPF application. There is no UI feedback needed. All I need is a status update from the first task. I agree if we do all in one thread it will be fine. But we want to have the second thread which does more things seperately even if the user leaves the screen which created this thread.

Here is the sample:

class Program
{

    static string outValue;
    static bool _isFinished = false;

    static void Main(string[] args)
    {
        ThreadStart thread1 = delegate()
        {
            outValue = AnotherClass.FirstLongRunningTask();
            // I need to set the _isFinished after the long running finishes..
            // I cant wait here because I need to kick start the next thread and move on.
            // 
        };
        new Thread(thread1).Start();

        ThreadStart thread2 = delegate()
        {
            while (!_isFinished)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Inside the while loop...");
            }
            if (!string.IsNullOrEmpty(outValue))
            {
                // This should execute only if the _isFinished is true...
                AnotherClass.SecondTask(outValue);    
            }

        };
        new Thread(thread2).Start();

        for (int i = 0; i < 5000; i++)
        {
            Thread.Sleep(500);
            Console.WriteLine("I have to work on this while thread 1 and thread 2 and doing something ...");    
        }    
        Console.ReadLine();

    }



}


public class AnotherClass
{
    public static string FirstLongRunningTask() 
    {
        Thread.Sleep(6000);
        return "From the first long running task...";
    }

    public static void SecondTask(string fromThread1) 
    {
        Thread.Sleep(1000);
        Console.WriteLine(fromThread1);
    }
}

Where do I set the _isFinished?
I can’t use BackgroundWorker threads. Any help is appreciated.

  • 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-08T18:46:38+00:00Added an answer on June 8, 2026 at 6:46 pm

    If a thread can only start when another one finishes, you have a very simple solution: execute the entire code on the first thread.

    You can use Task.ContinueWith to queue up more work for the same Task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the boost threads, but when running the program i get
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
I have 2 threads running a chat program such that one is a server
I am trying to get stack trace for a process running with multiple threads,
I'm trying to get a UPC-NAS Benchmark (compiled for 256 threads) running on a
I have been trying to get a project of mine to run but I
Having looked at other similar threads, I still can't get pycrypto running. I'm trying
I'm trying to get 2 tasks to run in my Dynamic C under Micrium
I am trying to perform an operation on a background thread. In the past
We have an application with multiple subsystems running in different threads (ie one thread

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.