Versión imprimible       Enviar     
Evaluar y enviar comentarios
MSDN
MSDN Library
 ThreadPriority (Enumeración)
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2005/.NET Framework 2.0

Hay además otras versiones disponibles para:
ThreadPriority (Enumeración)
Especifica la prioridad de programación de Thread.

Espacio de nombres: System.Threading
Ensamblado: mscorlib (en mscorlib.dll)

Visual Basic (Declaración)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration ThreadPriority
Visual Basic (Uso)
Dim instance As ThreadPriority
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum ThreadPriority
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum class ThreadPriority
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum ThreadPriority
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public enum ThreadPriority
 Nombre de miembroDescripción
Compatible con .NET Compact FrameworkAboveNormalThread puede programarse después de los subprocesos con prioridad Highest y antes que los subprocesos con prioridad Normal
Compatible con .NET Compact FrameworkBelowNormalThread puede programarse después de los subprocesos con prioridad Normal y antes que los subprocesos con prioridad Lowest
Compatible con .NET Compact FrameworkHighestThread puede programarse antes que los subprocesos que tengan cualquier otra prioridad. 
Compatible con .NET Compact FrameworkLowestThread puede programarse después de los subprocesos que tengan cualquier otra prioridad. 
Compatible con .NET Compact FrameworkNormalThread puede programarse después de los subprocesos con prioridad AboveNormal y antes que los subprocesos con prioridad BelowNormal. Los subprocesos tienen prioridad Normal de forma predeterminada. 

ThreadPriority define el conjunto de todos los valores posibles para la prioridad de un subproceso. Las prioridades de los subproceso especifican la prioridad relativa de un subproceso frente a otro.

Cada subproceso tiene asignada una prioridad. Los subprocesos creados en el motor en tiempo de ejecución tienen asignada inicialmente la prioridad Normal, mientras que los subprocesos creados fuera del motor en tiempo de ejecución conservan su prioridad anterior cuando entran en el motor en tiempo de ejecución. Para obtener y establecer la prioridad de un subproceso, hay que obtener acceso a su propiedad Priority.

Los subprocesos se programan para que se ejecuten en función de su prioridad. El algoritmo de programación utilizado para determinar el orden de ejecución de los subprocesos varía en función de cada sistema operativo. El sistema operativo también puede ajustar la prioridad del subproceso de forma dinámica, en relación con el desplazamiento del foco de la interfaz de usuario entre segundo plano y primer plano.

La prioridad de un subproceso no afecta al estado del subproceso; el estado de un subproceso debe ser Running antes de que el sistema operativo pueda programarlo.

En el ejemplo de código siguiente se muestra el resultado de cambiar la prioridad de un subproceso. Se crean dos subprocesos y la prioridad de un subproceso se establece en BelowNormal. Ambos subprocesos incrementan una variable en un bucle while y se ejecutan durante un tiempo establecido.

Visual Basic
Option Explicit
Option Strict

Imports System
Imports System.Threading

Public Class Test

    <MTAThread> _
    Shared Sub Main()
        Dim priorityTest As New PriorityTest()

        Dim threadOne As Thread = _
            New Thread(AddressOf priorityTest.ThreadMethod)
        threadOne.Name = "ThreadOne"
        Dim threadTwo As Thread = _
            New Thread(AddressOf priorityTest.ThreadMethod)
        threadTwo.Name = "ThreadTwo"

        threadTwo.Priority = ThreadPriority.BelowNormal
        threadOne.Start()
        threadTwo.Start()

        ' Allow counting for 10 seconds.
        Thread.Sleep(10000)
        priorityTest.LoopSwitch = False
    End Sub

End Class

Public Class PriorityTest

    Dim loopSwitchValue As Boolean 

    Sub New()
        loopSwitchValue = True
    End Sub

    WriteOnly Property LoopSwitch As Boolean
        Set
            loopSwitchValue = Value
        End Set
    End Property

    Sub ThreadMethod()
        Dim threadCount As Long = 0

        While loopSwitchValue
            threadCount += 1
        End While
        
        Console.WriteLine("{0} with {1,11} priority " & _
            "has a count = {2,13}", Thread.CurrentThread.Name, _
            Thread.CurrentThread.Priority.ToString(), _
            threadCount.ToString("N0")) 
    End Sub

End Class
C#
using System;
using System.Threading;

class Test
{
    static void Main()
    {
        PriorityTest priorityTest = new PriorityTest();
        ThreadStart startDelegate = 
            new ThreadStart(priorityTest.ThreadMethod);

        Thread threadOne = new Thread(startDelegate);
        threadOne.Name = "ThreadOne";
        Thread threadTwo = new Thread(startDelegate);
        threadTwo.Name = "ThreadTwo";

        threadTwo.Priority = ThreadPriority.BelowNormal;
        threadOne.Start();
        threadTwo.Start();

        // Allow counting for 10 seconds.
        Thread.Sleep(10000);
        priorityTest.LoopSwitch = false;
    }
}

class PriorityTest
{
    bool loopSwitch;

    public PriorityTest()
    {
        loopSwitch = true;
    }

    public bool LoopSwitch
    {
        set{ loopSwitch = value; }
    }

    public void ThreadMethod()
    {
        long threadCount = 0;

        while(loopSwitch)
        {
            threadCount++;
        }
        Console.WriteLine("{0} with {1,11} priority " +
            "has a count = {2,13}", Thread.CurrentThread.Name, 
            Thread.CurrentThread.Priority.ToString(), 
            threadCount.ToString("N0")); 
    }
}
C++
using namespace System;
using namespace System::Threading;
ref class PriorityTest
{
private:
   bool loopSwitch;

public:
   PriorityTest()
   {
      loopSwitch = true;
   }


   property bool LoopSwitch 
   {
      void set( bool value )
      {
         loopSwitch = value;
      }

   }
   void ThreadMethod()
   {
      __int64 threadCount = 0;
      while ( loopSwitch )
      {
         threadCount++;
      }

      Console::WriteLine( "{0} with {1,11} priority "
      "has a count = {2,13}", Thread::CurrentThread->Name, Thread::CurrentThread->Priority.ToString(), threadCount.ToString(  "N0" ) );
   }

};

int main()
{
   PriorityTest^ priorityTest = gcnew PriorityTest;
   ThreadStart^ startDelegate = gcnew ThreadStart( priorityTest, &PriorityTest::ThreadMethod );
   Thread^ threadOne = gcnew Thread( startDelegate );
   threadOne->Name =  "ThreadOne";
   Thread^ threadTwo = gcnew Thread( startDelegate );
   threadTwo->Name =  "ThreadTwo";
   threadTwo->Priority = ThreadPriority::BelowNormal;
   threadOne->Start();
   threadTwo->Start();
   
   // Allow counting for 10 seconds.
   Thread::Sleep( 10000 );
   priorityTest->LoopSwitch = false;
}
J#
import System.*;
import System.Threading.*;
import System.Threading.Thread;

class Test
{
    public static void main(String[] args)
    {
        PriorityTest priorityTest = new PriorityTest();
        ThreadStart startDelegate = new ThreadStart(priorityTest.ThreadMethod);
        Thread threadOne = new Thread(startDelegate);

        threadOne.set_Name("ThreadOne");

        Thread threadTwo = new Thread(startDelegate);

        threadTwo.set_Name("ThreadTwo");
        threadTwo.set_Priority(ThreadPriority.BelowNormal);
        threadOne.Start();
        threadTwo.Start();

        // Allow counting for 10 seconds.
        Thread.Sleep(10000);
        priorityTest.set_LoopSwitch(false);
    } //main
} //Test

class PriorityTest
{
    private boolean loopSwitch;

    public PriorityTest()
    {
        loopSwitch = true;
    } //PriorityTest

    /** @property 
     */
    public void set_LoopSwitch(boolean value)
    {
        loopSwitch = value;
    } //set_LoopSwitch

    public void ThreadMethod()
    {
        long threadCount = 0;

        while (loopSwitch) {
            threadCount++;
        }

        Console.WriteLine("{0} with {1,11} priority " + "has a count = {2,13}",
            Thread.get_CurrentThread().get_Name(),
            Thread.get_CurrentThread().get_Priority().toString(),
            ((System.Int32)threadCount).ToString("N0"));
    } //ThreadMethod
} //PriorityTest

Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0

XNA Framework

Compatible con: 1.0
© 2009 Microsoft Corporation. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker