Sorry if this is a silly question, but i'd just like to be certain.

I start my app, and then immediately start a new thread to carry out some tasks while my 'main thread' (if that is the term to use) is doing something else.

Do all method calls from the new thread i started, execute from that thread?

Recommended Answers

All 6 Replies

No, only the method you declate in the new thread creation.

Do all method calls from the new thread i started, execute from that thread?

Anything done on the new thread is done on the new thread. If the method you use to start the thread makes calls to other methods, they all run on the new thread.

Hmm, conflicting answers there.

From which thread is method1() executing in the code below?
I'm hoping its the spawned thread.

namespace nspace
{
    public class Program
    {
        public static void Main(string[] args)
        {

            Thread thread = new Thread(new ThreadStart(newthread));
            thread.Start();
            while (true)
            {
                Thread.Sleep(10);
            }
        }
        public static void newthread()
        {
            method1();
        }
        public static void method1()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("method1");
                Thread.Sleep(100);
            }

            Console.ReadKey();
        }
    }
}

As I said, newthread is calling the method so it's running on newthread's thread.

It is executing from a newly created thread. But this kind of coding is useless. Why would you want to call a method from a method, and without doing anything on the 1st one. Ok, mabye this is just an example code, made for testings only.

Yes, just example.

Thank you very much guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.