-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathProgram.cs
60 lines (48 loc) · 1.71 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System.Diagnostics;
using System.Threading;
namespace Passing_Parameters
{
public class Program
{
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number", 42);
// Create a thread to execute the task, and then...
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
// ...start the thread
t.Start();
Debug.WriteLine("Main thread does some work, then waits.");
t.Join();
Debug.WriteLine(
"Independent task has completed; main thread ends.");
Thread.Sleep(Timeout.Infinite);
}
// The ThreadWithState class contains the information needed for
// a task, and the method that executes the task.
public class ThreadWithState
{
// State information used in the task.
private readonly string _boilerplate;
private readonly int _numberValue;
// The constructor obtains the state information.
public ThreadWithState(string text, int number)
{
_boilerplate = text;
_numberValue = number;
}
// The thread procedure performs the task, such as formatting
// and printing a document.
public void ThreadProc()
{
Debug.WriteLine(
$"{_boilerplate} {_numberValue}.");
}
}
}
}