Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to pass Parameter to a Thread
To work with threads, add the following namespace in your code −
using System.Threading;
Firstly, you need to create a new thread in C# −
Thread thread = new Thread(threadDemo);
Above, threadDemo is our thread function.
Now pass a parameter to the thread −
thread.Start(str);
The parameter set above is −
String str = "Hello World!";
Example
Let us see the complete code to pass a parameter to a thread in C#.
using System;
using System.Threading;
namespace Sample {
class Demo {
static void Main(string[] args) {
String str = "Hello World!";
// new thread
Thread thread = new Thread(threadDemo);
// passing parameter
thread.Start(str);
}
static void threadDemo(object str) {
Console.WriteLine("Value passed to the thread: "+str);
}
}
}
Output
Value passed to the thread: Hello World!
Advertisements