hustino siat

hustino siat

  • NA
  • 68
  • 7.6k

Multi Threading and threadpriority problems

Nov 29 2021 3:31 PM

THis is my code about multithreading...

namespace Threads
{
    public partial class FrmbasicThread : Form
    {  
        public FrmbasicThread()
        {
            InitializeComponent();    
        }
        private Thread ThreadA, ThreadB, ThreadC, ThreadD;

        private void button1_Click(object sender, EventArgs e)
        {

            Console.WriteLine("------------------");
            Console.WriteLine(string.Format("-Before Starting Thread-"));
            ThreadStart aa = new ThreadStart(Name1);

            ThreadA = new Thread(aa);
            ThreadA.Name = "Thread A Process";     

            ThreadB = new Thread(new ThreadStart(Name2));
            ThreadB.Name = "Thread B Process";

            ThreadC = new Thread(new ThreadStart(Name3));
            ThreadC.Name = "Thread C Process";

            ThreadD = new Thread(new ThreadStart(Name4));
            ThreadD.Name = "Thread D Process";


            ThreadA.Start();
            ThreadB.Start();
            ThreadC.Start();
            ThreadD.Start();

            ThreadA.Join();
            ThreadB.Join();
            ThreadC.Join();
            ThreadD.Join();

            ThreadA.Priority = ThreadPriority.Highest;
            ThreadB.Priority = ThreadPriority.Normal;
            ThreadC.Priority = ThreadPriority.AboveNormal;
            ThreadD.Priority = ThreadPriority.BelowNormal;

            Console.WriteLine(string.Format("-After Starting Thread-"));

            lblResult.Text = "-After Starting Thread-";             
        }
        static void Name1()
        {

            for (int i = 0; i <= 2; i++)
            {
                Console.WriteLine("Name of Thread " + Thread.CurrentThread.Name+ " = " + i);
                Thread.Sleep(500);             
            }
        }
        static void Name3()
        {
            for (int i = 0; i <= 6; i++)
            {
                Console.WriteLine("Name of Thread " + Thread.CurrentThread.Name + " = " + i);
                Thread.Sleep(500);
            }          
        }
        static void Name2()
        {
            for (int i = 0; i <= 2; i++)
            {
                Console.WriteLine("Name of Thread " + Thread.CurrentThread.Name + " = " + i);
                Thread.Sleep(500);
            }
        }
        static void Name4()
        {
            for (int i = 0; i <= 6; i++)
            {
                Console.WriteLine("Name of Thread " + Thread.CurrentThread.Name + " = " + i);
                Thread.Sleep(1500);
            }
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

but the error im getting is this...

 


Answers (2)