Parameters passed to the thread.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
 1 public class ThreadWithState {
 2         //Parameters to be passed.
 3         private string boilerplate;
 4         private int value;
 5         //ThroughThe constructorParameters.
 6         public ThreadWithState(string text, int number) {
 7             boilerplate = text;
 8             value = number;
 9         }
10         //To the thread of execution, the return type of void is to allow ThreadStart to call directly.
11         public void ThreadProc() {
12             //The task you want to perform, the only display parameters about incoming
13             Console.WriteLine(boilerplate, value);
14         }
15     }
16 
17 public class Example {
18         public static void Main() {
19             //Instantiate ThreadWithState class, providing parameters for the thread
20             ThreadWithState tws = new ThreadWithState(
21             "This report displays the number {0}.", 42);
22             // Create a task execution thread, and execute
23             Thread t = new Thread(new ThreadStart(tws.ThreadProc));
24             t.Start();
25             Console.WriteLine("Main thread does some work, then waits.");
26             t.Join();
27             Console.WriteLine(
28             "Independent task has completed; main thread ends.");
29         }
30     }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Marcy at November 13, 2013 - 6:19 AM