How to use basic threading in C#

2 Answers

0 votes
using System;
using System.Threading;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(new ThreadStart(MethodA));
            Thread thread2 = new Thread(new ThreadStart(MethodB));

            thread1.Start();
            thread2.Start();
        }
        static void MethodA()
        {
            Thread.Sleep(1000);
            Console.WriteLine("MethodA");
        }

        static void MethodB()
        {
            Thread.Sleep(2000);
            Console.WriteLine("MethodB");
        }
    }
}

/*
run:

MethodA
MethodB

*/

 



answered Jan 20, 2017 by avibootz
0 votes
using System;
using System.Threading;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(new ThreadStart(MethodA));
            Thread thread2 = new Thread(new ThreadStart(MethodB));

            thread1.Start();
            thread2.Start();

            thread1.Abort();
        }
        static void MethodA()
        {
            Thread.Sleep(1000);
            Console.WriteLine("MethodA");
        }

        static void MethodB()
        {
            Thread.Sleep(2000);
            Console.WriteLine("MethodB");
        }
    }
}

/*
run:

MethodB

*/

 



answered Jan 20, 2017 by avibootz

Related questions

1 answer 155 views
155 views asked Apr 28, 2017 by avibootz
1 answer 189 views
189 views asked Apr 25, 2017 by avibootz
1 answer 168 views
168 views asked Apr 25, 2017 by avibootz
1 answer 166 views
166 views asked Feb 17, 2017 by avibootz
2 answers 79 views
2 answers 175 views
...