Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Understanding Thread and Process and Thread Pool in C#

Tuesday 7 July 2015
|
Read: Completed in minutes

[Tips] Understanding Thread and Process and Thread Pool in C#

A thread and a process are both independent sequences of execution, but they have some key differences. Here are some of them:

  • A process has its own memory space, while a thread shares the memory space of its parent process.
  • A process can create multiple threads, but a thread cannot create other threads or processes.
  • A process can communicate with other processes using inter-process communication (IPC) mechanisms, such as pipes, sockets, or shared memory. A thread can communicate with other threads of the same process using shared variables or synchronization primitives, such as locks, semaphores, or condition variables.
  • A process is more expensive to create and terminate than a thread, as it involves allocating and deallocating memory and resources. A thread is cheaper to create and terminate, as it only requires a stack and a thread control block (TCB).
  • A process is more expensive to switch than a thread, as it involves saving and restoring the entire context of the process, including registers, program counter, stack pointer, etc. A thread is cheaper to switch, as it only requires saving and restoring a few registers and the stack pointer.

  • A process is isolated from other processes and protected by the operating system. A thread is not isolated from other threads and can be affected by their actions. For example, if one thread crashes or modifies a shared variable, it can cause problems for other threads.
  • These are some of the main differences between a thread and a process. Depending on the application and the operating system, one may be more suitable than the other. Threads are useful for parallelizing tasks within a single program, while processes are useful for running multiple programs concurrently.

    Thread vs Process:

    Understanding Thread and Process and Thread Pool in C#







    Overview:

    Today I'll show you how to working with Thread, Process and Thread Pool in C#.
    See.
    - How to create Thread
    - Property
    - Common Method
    - Foreground and Background Thread
    - Thread Pool
    Understanding Thread and Process and Thread Pool in C#






    Create Thread with source code CSharp (demo)

    This demo description about two function with params
    - display function: no params
    - display function: with 1 param (Objects)
    - showMsg: with param (string)

    Source Code:


    
    /* *************************************************************
     * Author:          Zidane (huuvi168@gmail.com)
     * Last Modified:   20150630
     * *************************************************************/
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ThreadDemo
    {
        public class clsThread
        {
            public static void run()
            {
                // Thread with no params
                Thread thread1 = new Thread(() => display());
                thread1.Start();
                
                // Thread with param: string
                Thread thread2 = new Thread(() => showMsg("[Learn-tech-tips] 
                                     Thread with one param(string)"));
                thread2.Start();            
    
                // Thread with param: Object
                clsStudent student = new clsStudent();
                student.Id = 168;
                student.Name = "[Learn-tech-tips] Thread with param(Objects)";            
                Thread thread3 = new Thread(() => display(student));
                thread3.Start();
                
            }     
    
            public static void display()
            {
                Console.WriteLine("[Learn-tech-tips] Thread with no params");
                Thread.Sleep(90000);
            }
    
            public static void showMsg(string message)
            {
                Console.WriteLine(message);
            }
    
            public static void display(clsStudent student)
            {
                Console.WriteLine(student.Id.ToString());
                Console.WriteLine(student.Name.ToString());
            }
        }
    
        public class clsStudent
        {
            private int id;
            private string name;
          
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public int Id
            {
                get { return id; }
                set { id = value; }
            }
        }    
    }
    

    Result:


    Understanding Thread and Process and Thread Pool in C#

    Using Common Method on Thread (demo)

    We have 3 function: After thread1, thread2 start finished, thread3 will be start

    Source code:

    
    /* *************************************************************
     * Author:          Zidane (huuvi168@gmail.com)
     * Last Modified:   20150630
     * *************************************************************/
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ThreadDemo
    {
        public class clsCommonMethod
        {
    
            public static void run()
            {
                Thread thread1 = new Thread(MethodA);
                Thread thread2 = new Thread(MethodB);
                Thread thread3 = new Thread(MethodC);
    
                thread1.Start();
                thread2.Start();
                            
                thread2.Join();
    
                // After thread2 done, thread3 can start
                thread3.Start();
            }
    
            public static void MethodA()
            {
                for (int i = 0; i < 100; i++) Console.Write("0");
            }
            public static void MethodB()
            {
                for (int i = 0; i < 100; i++) Console.Write("1");
            }
            public static void MethodC()
            {
                for (int i = 0; i < 100; i++) Console.Write("2");
            }
        }
    }
    

    Result:


    Understanding Thread and Process and Thread Pool in C#

    Thread Locking 

    If we don't use lock API, the index will be calculator don't correct.
    So we need using SynObj Object for locking calculator index.




    Source code:


    
    /* *************************************************************
     * Author:          Zidane (huuvi168@gmail.com)
     * Last Modified:   20150630
     * *************************************************************/
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ThreadPoolDemo
    {
        public class clsThreadLocking
        {
            static int index = 0;
            static object syncObj = new object();
    
            public static void run()
            {
                Thread thread1 = new Thread(inputIncrease);
                Thread thread2 = new Thread(inputDecrease);
    
                thread1.Start();
                thread2.Start();            
            }
    
            static void inputIncrease()
            {
                for (int i = 0; i < 80; i++)
                {
                    lock (syncObj)  // local obj
                    {
                        index++;
                        Consdeole.Write(index + "\t");                    
                    }
                }
            }
    
            static void inputDecrease()
            {
                for (int i = 0; i < 80; i++)
                {
                    lock (syncObj)
                    {                    
                        index--;
                        Console.Write(index + "\t");    
                    }
                }
            }
        }
    }
    
    


    View more Thread pool from this link :)

    Are you interested in topic Understanding Thread and Process and Thread Pool in C# from Webzone Tech Tips? If you have any thoughts or questions, please share them in the comment section below. I would love to hear from you and chat about it

    Webzone Tech Tips Zidane


🙇🏼 Your Feedback Is Valuable and Helpful to Us - Webzone - all things Tech Tips web development Zidane 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com