Csharp/CSharp Tutorial/Thread/Background Thread — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Move thread to background

using System;
using System.Threading;
class MainClass
{
  public static void DoCount()
  {
    for ( int i = 0; true; i++ )
    {
      System.Console.WriteLine( "{0} reached {1}", Thread.CurrentThread.Name, i );
    }
  }
  static void Main(string[] args)
  {
    Thread[] t = new Thread[ 10 ];
    for( int k = 0; k < t.Length; k++ )
    {
      t[ k ] = new Thread( new ThreadStart( DoCount ) );
      t[ k ].Name = "Thread " + k;
      t[ k ].IsBackground = true;
      t[ k ].Start();
    }
    Thread.Sleep( 1000 );
    System.Console.WriteLine( "Exiting main thread" );
  }
}
Thread 0 reached 0
Thread 0 reached 1
Thread 0 reached 2
Thread 0 reached 3
Thread 0 reached 4
Thread 0 reached 5
Thread 0 reached 6
Thread 0 reached 7
Thread 0 reached 8
Thread 0 reached 9
Thread 0 reached 10
Thread 0 reached 11
Thread 0 reached 12
Thread 0 reached 13
Thread 0 reached 14
Thread 0 reached 15
Thread 0 reached 16
Thread 0 reached 17
Thread 0 reached 18
Thread 0 reached 19
Thread 0 reached 20
Thread 0 reached 21
Thread 0 reached 22
Thread 0 reached 23
Thread 0 reached 24
Thread 0 reached 25
Thread 0 reached 26
Thread 0 reached 27
Thread 0 reached 28
Thread 0 reached 29
Thread 0 reached 30
Thread 1 reached 0
Thread 1 reached 1
Thread 1 reached 2
Thread 1 reached 3
Thread 1 reached 4
Thread 1 reached 5
Thread 1 reached 6
Thread 1 reached 7
Thread 1 reached 8
Thread 1 reached 9
Thread 1 reached 10
Thread 1 reached 11
Thread 1 reached 12
Thread 1 reached 13
Thread 1 reached 14
Thread 1 reached 15
Thread 1 reached 16
Thread 1 reached 17
Thread 1 reached 18
Thread 1 reached 19
Thread 1 reached 20
Thread 1 reached 21
Thread 1 reached 22
Thread 1 reached 23
Thread 1 reached 24
Thread 1 reached 25
Thread 1 reached 26
Thread 1 reached 27
Thread 1 reached 28
Thread 1 reached 29
Thread 1 reached 30
Thread 1 reached 31
Thread 1 reached 32
Thread 1 reached 33
Thread 1 reached 34
Thread 1 reached 35
Thread 1 reached 36
Thread 1 reached 37
Thread 1 reached 38
Thread 1 reached 39
Thread 1 reached 40
Thread 1 reached 41
Thread 1 reached 42
Thread 1 reached 43
Thread 1 reached 44
Thread 1 reached 45
Thread 1 reached 46
Thread 1 reached 47
Thread 1 reached 48
Thread 1 reached 49
Thread 1 reached 50
Thread 1 reached 51
Thread 1 reached 52
Thread 1 reached 53
Thread 1 reached 54
Thread 1 reached 55
Thread 1 reached 56
Thread 1 reached 57
Thread 1 reached 58
Thread 1 reached 59
Thread 1 reached 60
Thread 1 reached 61
Thread 1 reached 62
Thread 1 reached 63
Thread 1 reached 64
Thread 1 reached 65
Thread 1 reached 66
Thread 1 reached 67
Thread 1 reached 68
Thread 1 reached 69
Thread 1 reached 70
Thread 1 reached 71
Thread 1 reached 72
Thread 1 reached 73
Thread 1 reached 74
Thread 1 reached 75
Thread 1 reached 76
Thread 1 reached 77
Thread 1 reached 78
Thread 1 reached 79
Thread 1 reached 80
Thread 1 reached 81
Thread 1 reached 82
Thread 1 reached 83
Thread 1 reached 84
Thread 1 reached 85
Thread 1 reached 86
Thread 1 reached 87
Thread 1 reached 88
Thread 1 reached 89
Thread 1 reached 90
Thread 1 reached 91
Thread 1 reached 92
Thread 1 reached 93
Thread 1 reached 94
Thread 1 reached 95
Thread 1 reached 96
Thread 1 reached 97
Thread 1 reached 98
Thread 1 reached 99
Thread 1 reached 100
Thread 1 reached 101
Thread 1 reached 102
Thread 1 reached 103
Thread 1 reached 104
Thread 1 reached 105
Thread 1 reached 106
Thread 1 reached 107
Thread 1 reached 108
Thread 1 reached 109
Thread 1 reached 110
Thread 1 reached 111
Thread 1 reached 112
Thread 1 reached 113
Thread 1 reached 114
Thread 1 reached 115
Thread 1 reached 116
Thread 1 reached 117
Thread 1 reached 118
Thread 1 reached 119
Thread 1 reached 120
Thread 1 reached 121
Thread 1 reached 122
Thread 1 reached 123
Thread 1 reached 124
Thread 1 reached 125
Thread 1 reached 126
Thread 1 reached 127
Thread 1 reached 128
Thread 1 reached 129
Thread 1 reached 130
Thread 1 reached 131
Thread 1 reached 132
Thread 1 reached 133
Thread 1 reached 134
Thread 1 reached 135
Thread 1 reached 136
Thread 1 reached 137
Thread 1 reached 138
Thread 1 reached 139
Thread 1 reached 140
Thread 1 reached 141
Thread 1 reached 142
Thread 1 reached 143
Thread 1 reached 144
Thread 1 reached 145
Thread 1 reached 146
Thread 1 reached 147
Thread 1 reached 148
Thread 1 reached 149
Thread 1 reached 150
Thread 1 reached 151
Thread 1 reached 152
Thread 1 reached 153
Thread 1 reached 154
Thread 1 reached 155
Thread 1 reached 156
Thread 1 reached 157
Thread 1 reached 158
Thread 1 reached 159
Thread 1 reached 160
Thread 1 reached 161
Thread 1 reached 162
Thread 1 reached 163
Thread 1 reached 164
Thread 1 reached 165
Thread 1 reached 166
Thread 1 reached 167
Thread 1 reached 168
Thread 1 reached 169
Thread 1 reached 170
Thread 1 reached 171
Thread 1 reached 172
Thread 1 reached 173
Thread 1 reached 174
Thread 1 reached 175
Thread 1 reached 176
Thread 1 reached 177
Thread 1 reached 178
Thread 1 reached 179
Thread 1 reached 180
Thread 1 reached 181
Thread 1 reached 182
Thread 1 reached 183
Thread 1 reached 184
Thread 1 reached 185
Thread 1 reached 186
Thread 1 reached 187
Thread 1 reached 188
Thread 1 reached 189
Thread 1 reached 190
Thread 1 reached 191
Thread 1 reached 192
Thread 1 reached 193
Thread 1 reached 194
Thread 1 reached 195
Thread 1 reached 196
Thread 1 reached 197
Thread 1 reached 198
Thread 1 reached 199
Thread 1 reached 200
Thread 1 reached 201
Thread 1 reached 202
Thread 1 reached 203
Thread 0 reached 31
Thread 2 reached 0
Thread 2 reached 1
Thread 2 reached 2
Thread 2 reached 3
Thread 2 reached 4
Thread 2 reached 5
Thread 2 reached 6
Thread 2 reached 7
Thread 2 reached 8
Thread 2 reached 9
Thread 2 reached 10
Thread 2 reached 11
Thread 2 reached 12
Thread 2 reached 13
Thread 2 reached 14
Thread 2 reached 15
Thread 2 reached 16
Thread 3 reached 0
Thread 3 reached 1
Thread 3 reached 2
Thread 3 reached 3
Thread 3 reached 4
Thread 3 reached 5
Thread 3 reached 6
Thread 3 reached 7
Thread 3 reached 8
Thread 3 reached 9
Thread 3 reached 10
Thread 3 reached 11
Thread 3 reached 12
Thread 3 reached 13
Thread 3 reached 14
Thread 3 reached 15
Thread 3 reached 16
Thread 4 reached 0
Thread 4 reached 1
Thread 4 reached 2
Thread 4 reached 3
Thread 4 reached 4
Thread 4 reached 5
Thread 4 reached 6
Thread 4 reached 7
Thread 4 reached 8
Thread 4 reached 9
Thread 4 reached 10
Thread 4 reached 11
Thread 4 reached 12
Thread 4 reached 13
Thread 4 reached 14
Thread 4 reached 15
Thread 4 reached 16
Thread 4 reached 17
Thread 4 reached 18
Thread 4 reached 19
Thread 4 reached 20
Thread 4 reached 21
Thread 4 reached 22
Thread 4 reached 23
Thread 4 reached 24
Thread 4 reached 25
Thread 4 reached 26
Thread 4 reached 27
Thread 4 reached 28
Thread 4 reached 29
Thread 4 reached 30
Thread 4 reached 31
Thread 4 reached 32
Thread 4 reached 33
Thread 4 reached 34
Thread 5 reached 0
Thread 5 reached 1
Thread 5 reached 2
Thread 5 reached 3
Thread 5 reached 4
Thread 5 reached 5
Thread 5 reached 6
Thread 5 reached 7
Thread 5 reached 8
Thread 5 reached 9
Thread 5 reached 10
Thread 5 reached 11
Thread 5 reached 12
Thread 5 reached 13
Thread 5 reached 14
Thread 5 reached 15
Thread 5 reached 16
Thread 5 reached 17
Thread 5 reached 18
Thread 5 reached 19
Thread 5 reached 20
Thread 5 reached 21
Thread 5 reached 22
Thread 5 reached 23
Thread 5 reached 24
Thread 5 reached 25
Thread 5 reached 26
Thread 5 reached 27
Thread 5 reached 28
Thread 5 reached 29
Thread 5 reached 30
Thread 5 reached 31
Thread 6 reached 0
Thread 6 reached 1
Thread 6 reached 2
Thread 6 reached 3
Thread 6 reached 4
Thread 6 reached 5
Thread 6 reached 6
Thread 6 reached 7
Thread 6 reached 8
Thread 6 reached 9
Thread 6 reached 10
Thread 6 reached 11
Thread 6 reached 12
Thread 6 reached 13
Thread 6 reached 14
Thread 6 reached 15
Thread 6 reached 16
Thread 7 reached 0
Thread 7 reached 1
Thread 7 reached 2
Thread 7 reached 3
Thread 7 reached 4
Thread 7 reached 5
Thread 7 reached 6
Thread 7 reached 7
Thread 7 reached 8
Thread 7 reached 9
Thread 7 reached 10
Thread 7 reached 11
Thread 7 reached 12
Thread 7 reached 13
Thread 7 reached 14
Thread 7 reached 15
Thread 7 reached 16
Thread 7 reached 17
Thread 7 reached 18
Thread 7 reached 19
Thread 7 reached 20
Thread 7 reached 21
Thread 7 reached 22
Thread 7 reached 23
Thread 7 reached 24
Thread 7 reached 25
Thread 7 reached 26
Thread 7 reached 27
Thread 7 reached 28
Thread 7 reached 29
Thread 7 reached 30
Thread 7 reached 31
Thread 7 reached 32
Thread 7 reached 33
Thread 7 reached 34
Thread 8 reached 0
Thread 8 reached 1
Thread 8 reached 2
Thread 8 reached 3
Thread 8 reached 4
Thread 8 reached 5
Thread 8 reached 6
Thread 8 reached 7
Thread 8 reached 8
Thread 8 reached 9
Thread 8 reached 10
Thread 8 reached 11
Thread 8 reached 12
Thread 8 reached 13
Thread 8 reached 14
Thread 8 reached 15
Thread 8 reached 16
Thread 9 reached 0
Thread 9 reached 1
Thread 9 reached 2
Thread 9 reached 3
Thread 9 reached 4
Thread 9 reached 5
Thread 9 reached 6
Thread 9 reached 7
Thread 9 reached 8
Thread 9 reached 9
Thread 9 reached 10
Thread 9 reached 11
Thread 9 reached 12
Thread 9 reached 13
Thread 9 reached 14
Thread 9 reached 15
Thread 9 reached 16
Thread 9 reached 17
Thread 9 reached 18
Thread 9 reached 19
Thread 9 reached 20
Thread 9 reached 21
Thread 9 reached 22
Thread 9 reached 23
Thread 9 reached 24
Thread 9 reached 25
Thread 9 reached 26
Thread 9 reached 27
Thread 9 reached 28
Thread 9 reached 29
Thread 9 reached 30
Thread 9 reached 31
Thread 9 reached 32
Thread 9 reached 33
Thread 9 reached 34
Thread 9 reached 35
Thread 9 reached 36
Thread 9 reached 37
Thread 9 reached 38
Thread 9 reached 39
Thread 9 reached 40
Thread 9 reached 41
Thread 9 reached 42
Thread 9 reached 43
Thread 9 reached 44
Thread 9 reached 45
Thread 9 reached 46
Thread 9 reached 47
Thread 9 reached 48
Thread 9 reached 49
Thread 9 reached 50
Thread 9 reached 51
Thread 9 reached 52
Thread 0 reached 32
Thread 0 reached 33
Thread 0 reached 34
Thread 0 reached 35
Thread 0 reached 36
Thread 0 reached 37
Thread 0 reached 38
Thread 0 reached 39
Thread 0 reached 40
Thread 0 reached 41
Thread 0 reached 42
Thread 0 reached 43
Thread 0 reached 44
Thread 0 reached 45
Thread 0 reached 46
Thread 0 reached 47
Thread 0 reached 48
Thread 1 reached 204
Thread 1 reached 205
Thread 1 reached 206
Thread 1 reached 207
Thread 1 reached 208
Thread 1 reached 209
Thread 1 reached 210
Thread 1 reached 211
Thread 1 reached 212
Thread 1 reached 213
Thread 1 reached 214
Thread 1 reached 215
Thread 1 reached 216
Thread 1 reached 217
Thread 1 reached 218
Thread 1 reached 219
Thread 1 reached 220
Thread 1 reached 221
Thread 1 reached 222
Thread 1 reached 223
Thread 1 reached 224
Thread 1 reached 225
Thread 1 reached 226
Thread 1 reached 227
Thread 1 reached 228
Thread 1 reached 229
Thread 1 reached 230
Thread 1 reached 231
Thread 1 reached 232
Thread 1 reached 233
Thread 1 reached 234
Thread 1 reached 235
Thread 1 reached 236
Thread 1 reached 237
Thread 1 reached 238
Thread 2 reached 17
Thread 2 reached 18
Thread 2 reached 19
Thread 2 reached 20
Thread 2 reached 21
Thread 2 reached 22
Thread 2 reached 23
Thread 2 reached 24
Thread 2 reached 25
Thread 2 reached 26
Thread 2 reached 27
Thread 2 reached 28
Thread 2 reached 29
Thread 2 reached 30
Thread 2 reached 31
Thread 2 reached 32
Thread 2 reached 33
Thread 2 reached 34
Thread 2 reached 35
Thread 2 reached 36
Thread 2 reached 37
Thread 2 reached 38
Thread 2 reached 39
Thread 2 reached 40
Thread 2 reached 41
Thread 2 reached 42
Thread 2 reached 43
Thread 2 reached 44
Thread 2 reached 45
Thread 2 reached 46
Thread 2 reached 47
Thread 2 reached 48
Thread 2 reached 49
Thread 2 reached 50
Thread 2 reached 51
Thread 3 reached 17
Thread 3 reached 18
Thread 3 reached 19
Thread 3 reached 20
Thread 3 reached 21
Thread 3 reached 22
Thread 3 reached 23
Thread 3 reached 24
Thread 3 reached 25
Thread 3 reached 26
Thread 3 reached 27
Thread 3 reached 28
Thread 3 reached 29
Thread 3 reached 30
Thread 3 reached 31
Thread 3 reached 32
Thread 3 reached 33
Thread 4 reached 35
Thread 4 reached 36
Thread 4 reached 37
Thread 4 reached 38
Thread 4 reached 39
Thread 4 reached 40
Thread 4 reached 41
Thread 4 reached 42
Thread 4 reached 43
Thread 4 reached 44
Thread 4 reached 45
Thread 4 reached 46
Thread 4 reached 47
Thread 4 reached 48
Thread 4 reached 49
Thread 4 reached 50
Thread 4 reached 51
Thread 4 reached 52
Thread 4 reached 53
Thread 4 reached 54
Thread 4 reached 55
Thread 4 reached 56
Thread 4 reached 57
Thread 4 reached 58
Thread 4 reached 59
Thread 4 reached 60
Thread 4 reached 61
Thread 4 reached 62
Thread 4 reached 63
Thread 4 reached 64
Thread 4 reached 65
Thread 4 reached 66
Thread 4 reached 67
Thread 5 reached 32
Thread 5 reached 33
Thread 5 reached 34
Thread 5 reached 35
Thread 5 reached 36
Thread 5 reached 37
Thread 5 reached 38
Thread 5 reached 39
Thread 5 reached 40
Thread 5 reached 41
Thread 5 reached 42
Thread 5 reached 43
Thread 5 reached 44
Thread 5 reached 45
Thread 5 reached 46
Thread 5 reached 47
Thread 5 reached 48
Thread 5 reached 49
Thread 5 reached 50
Thread 5 reached 51
Thread 5 reached 52
Thread 5 reached 53
Thread 5 reached 54
Thread 5 reached 55
Thread 5 reached 56
Thread 5 reached 57
Thread 5 reached 58
Thread 5 reached 59
Thread 5 reached 60
Thread 5 reached 61
Thread 5 reached 62
Thread 5 reached 63
Thread 5 reached 64
Thread 5 reached 65
Thread 5 reached 66
Thread 6 reached 17
Thread 6 reached 18
Thread 6 reached 19
Thread 6 reached 20
Thread 6 reached 21
Thread 6 reached 22
Thread 6 reached 23
Thread 6 reached 24
Thread 6 reached 25
Thread 6 reached 26
Thread 6 reached 27
Thread 6 reached 28
Thread 6 reached 29
Thread 6 reached 30
Thread 6 reached 31
Thread 6 reached 32
Thread 6 reached 33
Thread 6 reached 34
Thread 6 reached 35
Thread 6 reached 36
Thread 6 reached 37
Thread 6 reached 38
Thread 6 reached 39
Thread 6 reached 40
Thread 6 reached 41
Thread 6 reached 42
Thread 6 reached 43
Thread 6 reached 44
Thread 6 reached 45
Thread 6 reached 46
Thread 6 reached 47
Thread 6 reached 48
Thread 6 reached 49
Thread 6 reached 50
Thread 6 reached 51
Exiting main thread

Set IsBackground to true

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
public class Printer {
    public void PrintNumbers() {
        Console.WriteLine("-> {0} is executing PrintNumbers()", Thread.CurrentThread.Name);
        Console.Write("Your numbers: ");
        for (int i = 0; i < 10; i++) {
            Console.Write(i + ", ");
            Thread.Sleep(2000);
        }
        Console.WriteLine();
    }
}
class Program {
    static void Main(string[] args) {
        Printer p = new Printer();
        Thread bgroundThread = new Thread(new ThreadStart(p.PrintNumbers));
        bgroundThread.IsBackground = true;
        bgroundThread.Start();
    }
}

Test foreground / worker behaviors

using System;
using System.Threading;
class MainClass
{
  static void MyThreadProc()
  {
    Thread.CurrentThread.Name = "TheSecondaryThread";
    Thread secondaryThread = Thread.CurrentThread;
    Console.WriteLine("Name? {0}", secondaryThread.Name);
    Console.WriteLine("Alive? {0}", secondaryThread.IsAlive);
    Console.WriteLine("Priority? {0}", secondaryThread.Priority);      
    Console.WriteLine("State? {0}", secondaryThread.ThreadState);
    Console.WriteLine();
    for(int i = 0; i < 1000; i ++)
    {
      Console.WriteLine("Value of i is: {0}", i);
      Thread.Sleep(5);
    }
  }
  [MTAThread]
  static void Main(string[] args)
  {
    Thread secondaryThread = new Thread(new ThreadStart(MyThreadProc));
    secondaryThread.Priority = ThreadPriority.Highest;
    
    secondaryThread.IsBackground = true;
    secondaryThread.Start();
  }
}
Name? TheSecondaryThread
Alive? True
Priority? Highest
State? Background
Value of i is: 0