- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the thread ID from a thread in C#?
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.
When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.
Example
class Program{ public static void Main(){ Thread thr; thr = Thread.CurrentThread; thr.Name = "Main thread"; Console.WriteLine("Name of current running " + "thread: {0}", Thread.CurrentThread.Name); Console.WriteLine("Id of current running " + "thread: {0}", Thread.CurrentThread.ManagedThreadId); Console.ReadLine(); } }
Output
Name of current running thread: Main thread Id of current running thread: 1
- Related Articles
- How to get current thread id in android?
- How to find the Current Context id of the Thread in C#?
- How to check whether a thread is a background thread or not in C#
- How to create a thread in C#?
- Main thread vs child thread in C#
- How to get current thread count in android?
- How to get current thread name in android?
- How to get current thread priority in android?
- How to get current thread state in android?
- How to get thread group information in android?
- How a thread can interrupt another thread in Java?
- Get current thread in Java
- How to catch a thread's exception in the caller thread in Python?
- How to get current thread is daemon in android?
- How to get current thread is interrupted in android?
