Freigeben über


Thread.IsBackground-Eigenschaft

Ruft einen Wert ab, der angibt, ob es sich bei einem Thread um einen Hintergrundthread handelt, oder legt diesen fest.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Property IsBackground As Boolean
'Usage
Dim instance As Thread
Dim value As Boolean

value = instance.IsBackground

instance.IsBackground = value
public bool IsBackground { get; set; }
public:
property bool IsBackground {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_IsBackground ()

/** @property */
public void set_IsBackground (boolean value)
public function get IsBackground () : boolean

public function set IsBackground (value : boolean)

Eigenschaftenwert

true, wenn dieser Thread ein Hintergrundthread ist oder zu einem solchen wird, andernfalls false.

Ausnahmen

Ausnahmetyp Bedingung

ThreadStateException

Der Thread ist deaktiviert.

Hinweise

Ein Thread ist entweder ein Hintergrundthread oder ein Vordergrundthread. Hintergrundthreads sind mit Vordergrundthreads identisch. Hintergrundthreads verhindern jedoch nicht, dass ein Prozess beendet wird. Sobald alle zu einem Prozess gehörigen Vordergrundthreads beendet wurden, beendet die Common Language Runtime diesen Prozess. Alle verbleibenden Hintergrundthreads werden angehalten und nicht abgeschlossen.

Beispiel

Im folgenden Codebeispiel wird das Verhalten von Vordergrund- und Hintergrundthreads gegenübergestellt. Es werden ein Vordergrund- und ein Hintergrundthread erstellt. Der Vordergrundthread erhält den Prozess bis zum Abschluss seiner while-Schleife aufrecht. Nach Beendigung des Vordergrundthreads wird der Prozess beendet, bevor die while-Schleife des Hintergrundthreads abgeschlossen ist.

Imports System
Imports System.Threading

Public Class Test

    <MTAThread> _
    Shared Sub Main()
        Dim shortTest As New BackgroundTest(10)
        Dim foregroundThread As New Thread(AddressOf shortTest.RunLoop)
        foregroundThread.Name = "ForegroundThread"

        Dim longTest As New BackgroundTest(50)
        Dim backgroundThread As New Thread(AddressOf longTest.RunLoop)
        backgroundThread.Name = "BackgroundThread"
        backgroundThread.IsBackground = True

        foregroundThread.Start()
        backgroundThread.Start()
    End Sub

End Class

Public Class BackgroundTest

    Dim maxIterations As Integer 

    Sub New(maximumIterations As Integer)
        maxIterations = maximumIterations
    End Sub

    Sub RunLoop()
        Dim threadName As String = Thread.CurrentThread.Name

        For i As Integer = 0 To maxIterations
            Console.WriteLine("{0} count: {1}", _
                    threadName, i.ToString())
            Thread.Sleep(250)
        Next i

        Console.WriteLine("{0} finished counting.", threadName)
    End Sub

End Class
using System;
using System.Threading;

class Test
{
    static void Main()
    {
        BackgroundTest shortTest = new BackgroundTest(10);
        Thread foregroundThread = 
            new Thread(new ThreadStart(shortTest.RunLoop));
        foregroundThread.Name = "ForegroundThread";

        BackgroundTest longTest = new BackgroundTest(50);
        Thread backgroundThread = 
            new Thread(new ThreadStart(longTest.RunLoop));
        backgroundThread.Name = "BackgroundThread";
        backgroundThread.IsBackground = true;

        foregroundThread.Start();
        backgroundThread.Start();
    }
}

class BackgroundTest
{
    int maxIterations;

    public BackgroundTest(int maxIterations)
    {
        this.maxIterations = maxIterations;
    }

    public void RunLoop()
    {
        String threadName = Thread.CurrentThread.Name;
        
        for(int i = 0; i < maxIterations; i++)
        {
            Console.WriteLine("{0} count: {1}", 
                threadName, i.ToString());
            Thread.Sleep(250);
        }
        Console.WriteLine("{0} finished counting.", threadName);
    }
}
using namespace System;
using namespace System::Threading;
ref class BackgroundTest
{
private:
   int maxIterations;

public:
   BackgroundTest( int maxIterations )
   {
      this->maxIterations = maxIterations;
   }

   void RunLoop()
   {
      String^ threadName = Thread::CurrentThread->Name;
      for ( int i = 0; i < maxIterations; i++ )
      {
         Console::WriteLine( "{0} count: {1}", threadName, i.ToString() );
         Thread::Sleep( 250 );

      }
      Console::WriteLine( "{0} finished counting.", threadName );
   }

};

int main()
{
   BackgroundTest^ shortTest = gcnew BackgroundTest( 10 );
   Thread^ foregroundThread = gcnew Thread( gcnew ThreadStart( shortTest, &BackgroundTest::RunLoop ) );
   foregroundThread->Name =  "ForegroundThread";
   BackgroundTest^ longTest = gcnew BackgroundTest( 50 );
   Thread^ backgroundThread = gcnew Thread( gcnew ThreadStart( longTest, &BackgroundTest::RunLoop ) );
   backgroundThread->Name =  "BackgroundThread";
   backgroundThread->IsBackground = true;
   foregroundThread->Start();
   backgroundThread->Start();
}
import System.*;
import System.Threading.*;
import System.Threading.Thread;

class Test
{
    public static void main(String[] args)
    {
        BackgroundTest shortTest = new BackgroundTest(10);
        Thread foregroundThread = new Thread(new ThreadStart(shortTest.RunLoop));

        foregroundThread.set_Name("ForegroundThread");

        BackgroundTest longTest = new BackgroundTest(50);
        Thread backgroundThread = new Thread(new ThreadStart(longTest.RunLoop));

        backgroundThread.set_Name("BackgroundThread");
        backgroundThread.set_IsBackground(true);
        foregroundThread.Start();
        backgroundThread.Start();
    } //main
} //Test

class BackgroundTest
{
    private int maxIterations;

    public BackgroundTest(int maxIterations)
    {
        this.maxIterations = maxIterations;
    } //BackgroundTest

    public void RunLoop()
    {
        String threadName = Thread.get_CurrentThread().get_Name();

        for (int i = 0; i < maxIterations; i++) {
            Console.WriteLine("{0} count: {1}", threadName, String.valueOf(i));
            Thread.Sleep(250);
        }

        Console.WriteLine("{0} finished counting.", threadName);
    } //RunLoop
} //BackgroundTest

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

Thread-Klasse
Thread-Member
System.Threading-Namespace

Weitere Ressourcen

Vordergrund- und Hintergrundthreads