TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ThreadStart and ParameterizedThreadStart

Use ThreadStart and ParameterizedThreadStart to make a new thread.
ThreadStart. This specifies a target method. It does not support any arguments to the method. We use ThreadStart to begin running a thread.Threads
We use the ThreadStart type. We create an instance of it and pass that to the Thread constructor. We also use ParameterizedThreadStart.
This example C# program creates an array of 4 different threads. It starts a parameterless method on each thread. It then joins the threads together in a sequential order.Thread Join

Next: ThreadStart is created with a constructor that receives a target function name. This target method must not receive any parameters.

Then: The ThreadStart instance is passed as an argument to the Thread constructor.

Constructor
C# program that uses ThreadStart using System; using System.Threading; class Program { static void Main() { // Create an array of Thread references. Thread[] array = new Thread[4]; for (int i = 0; i < array.Length; i++) { // Start the thread with a ThreadStart. ThreadStart start = new ThreadStart(Start); array[i] = new Thread(start); array[i].Start(); } // Join all the threads. for (int i = 0; i < array.Length; i++) { array[i].Join(); } Console.WriteLine("DONE"); } static void Start() { // This method has no formal parameters. Console.WriteLine("Start()"); } } Output Start() Start() Start() Start() DONE
ParameterizedThreadStart. This starts a thread with a target method. We can then pass this instance to the Thread constructor. The target method is then invoked upon starting the Thread.

Example: We create 4 ParameterizedThreadStarts, then start and join 4 threads. Start() is executed 4 different times on separate threads.

Tip: With ParameterizedThreadStart, you pass a function name as the argument. This is an object type that you can later cast.

Info: The threads were called in a non-sequential order. There is a pause before the runtime can receive a thread.

Note: You can't count on the order of execution exactly here. A thread may take longer for reasons beyond your control.

C# program that uses ParameterizedThreadStart using System; using System.Threading; class Program { static void Main() { // Create an array of Thread references. Thread[] array = new Thread[4]; for (int i = 0; i < array.Length; i++) { // Start the thread with a ParameterizedThreadStart. ParameterizedThreadStart start = new ParameterizedThreadStart(Start); array[i] = new Thread(start); array[i].Start(i); } // Join all the threads. for (int i = 0; i < array.Length; i++) { array[i].Join(); } Console.WriteLine("DONE"); } static void Start(object info) { // This receives the value passed into the Thread.Start method. int value = (int)info; Console.WriteLine(value); } } Output 1 0 2 3 DONE
No parameters. What if you want to start a thread but don't need to pass any parameters to it? You don't need to pass the null literal.

Instead: You can use the ThreadStart type, which (fortunately) is detailed also on this page.

A summary. ThreadStart enables you to start a thread and pass no arguments to the target method. For parameterless target methods, this type is ideal.
ParameterizedThreadStart gives you the ability to pass an argument of any type to a specific method on a thread. We can process many different data values on different threads.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf