Click here to Skip to main content
15,898,987 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i do two task at one time in console application c#

for example:-

i want to do some other task with this method being performed while the other task is been completed.

C#
public void task(string[] args)
        {
            string str = "";
            
            for (;;)
            {
                for (int i = 0; i < 3; i++)
                {
                    str += ".";
                    Console.Write("\rPerforming Some Task{0} ", str);
                    if (i <= 2)
                        Thread.Sleep(1000);
                    if (i == 2)
                    {
                        Console.Write("\b\b\b  ");
                        str = "";
                    }
                }
            }
        }


please help
Posted

At the most basic level you can use a new Thread, or you can use BackgroundWorker, it all depends on what level of communication and control you need between the main thread and the child thread. Multi-threading is a fairly indepth subject so you might need to read up about it. Most problems with multi-threading involve synchronising the threads, or one thread telling another thread to stop running, when you need to "join" the other thread etc. Here is a very basic example though.

C#
public static void DoSomething()
{
    for (int i = 0; i < 5; i++)
    {
        Thread.Sleep(500);
        Console.WriteLine("DoSomething {0}", i);
    }
}

public static void task(string[] args)
{
    Thread t = new Thread(new ThreadStart(DoSomething));
    t.Start();

    string str = "";
            
    for (;;)
    {
        for (int i = 0; i < 3; i++)
        {
            str += ".";
            Console.Write("\rPerforming Some Task{0} ", str);
            if (i <= 2)
                Thread.Sleep(1000);
            if (i == 2)
            {
                Console.Write("\b\b\b  ");
                str = "";
            }
        }
    }
}


Here is a solution that keeps all processing on child threads

C#
class Program
{
    private static bool running = false;

    static void Main(string[] args)
    {
        Thread t = new Thread(new ParameterizedThreadStart(task));
        t.Start(args);

        // wait for the [RETURN] key to be pressed
        Console.ReadLine();

        // set flag that tells the other threads to stop
        running = false;
            
        // wait for t to finish
        t.Join();
    }

    public static void DoSomething()
    {
        for (int i = 0; i < 5; i++)
        {
            Thread.Sleep(500);
            Console.WriteLine("DoSomething {0}", i);
        }
    }

    public static void task(object a)
    {
        running = true;
        string[] args = (string[])a;

        Thread t = new Thread(new ThreadStart(DoSomething));
        t.Start();

        string str = "";
            
        while (running)
        {
            for (int i = 0; i < 3; i++)
            {
                str += ".";
                Console.Write("\rPerforming Some Task{0} ", str);
                if (i <= 2)
                    Thread.Sleep(1000);
                if (i == 2)
                {
                    Console.Write("\b\b\b  ");
                    str = "";
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Palash Sachan 11-Dec-15 3:57am    
this thread.sleep pauses the whole program for some time and then do the work..is there something instead of pausing the whole program stop the working method only
F-ES Sitecore 11-Dec-15 4:09am    
The sleep in the task method will pause the whole program if you call task from the "Main" function. If you don't want that to happen then you start your "task" method on a new thread also. I'll update my solution with an example.
Palash Sachan 11-Dec-15 4:54am    
thanks a lot sir..this is working very fine :)
BillWoodruff 11-Dec-15 9:58am    
+5
To do that, you need to look at Threading: and that's not trivial, particularly with Console apps, where many calls tend to be blocking.

Have a look at the BackgroundWorker class[^] - it's one of the simple ways to get started.
 
Share this answer
 
Comments
Palash Sachan 11-Dec-15 3:45am    
sir, will this work on console application??
F-ES Sitecore 11-Dec-15 3:51am    
Yes, it's part of the .net framework, it'll work with console apps, desktop apps, services, asp.net pages etc.
 
Share this answer
 
Comments
Palash Sachan 11-Dec-15 4:55am    
read it..thanks for the reply :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900