Class

Xojo.Threading.Thread


Warning

This item was deprecated in version 2020r2. Please use Thread as a replacement.

Description

Used to run code in the background. Xojo threads are co-operative (not pre-emptive).

Properties

Name

Type

Read-Only

Shared

Priority

Integer

StackSize

UInteger

State

ThreadStates

Methods

Name

Parameters

Returns

Shared

Resume

Run

Sleep

milliSeconds As Integer, wakeEarly As Boolean = False

Suspend

Events

Name

Parameters

Returns

Run

Enumerations

Xojo.Threading.Thread.ThreadStates

ThreadStates

These are the various states of a thread. (Running, Waiting, Suspended, Sleeping, NotRunning)

Enum

Description

Running

The thread is currently running.

Waiting

The thread is waiting to run.

Suspended

The thread is suspended.

Sleeping

The thread is sleeping.

NotRunning

The thread is not running.

Property descriptions


Xojo.Threading.Thread.Priority

Priority As Integer

Indicates the priority of the thread.

The main thread for the app has a priority of 5. You can alter the priority of your own threads to give them more or less time relative to the main thread. The default is also 5.

The higher the priority value, the more time the thread is allocated, in relation to the main thread. For example, if you set the Priority = 10, then your thread will run twice as often as the main thread (since 10 is 5*2). A Priority value that is too high might prevent other threads from running at all.

Set the thread to a low priority:

Thread1.Priority = Thread.PriorityLow

Xojo.Threading.Thread.StackSize

StackSize As UInteger

The size of the thread stack, in bytes.


Xojo.Threading.Thread.State

State As ThreadStates

Indicates the state of the thread using the ThreadStates enumeration.

This property is read-only.

Check the state of a thread:

Var status As Text
Select Case state
Case Thread.Running
  status = "Running"
Case Thread.Waiting
  status = "Waiting"
Case Thread.Sleeping
  status = "Waiting"
Case Thread.Suspended
  status = "Suspended"
Case Thread.NotRunning
  status = "Not running"
End Select

Method descriptions


Xojo.Threading.Thread.Resume

Resume

Wakes a thread that is sleeping or suspended so that it may continue running.

LongRunningThread.Resume

Xojo.Threading.Thread.Run

Run

Starts the thread and calls its Run event handler.


Xojo.Threading.Thread.Sleep

Sleep(milliSeconds As Integer, wakeEarly As Boolean = False)

Puts the thread to sleep for the specified amount of milliSeconds, optionally allowing it to be woken early.

If wakeEarly is True, a thread may be woken before milliSeconds is reached if there are no other threads to run.

LongRunningThread.Sleep(300)

LongRunningThread.Sleep(1000, True)

Xojo.Threading.Thread.Suspend

Suspend

Puts the thread in a suspended state where it will no longer run. You can allow the thread to continue by calling Resume.

When a thread is suspended, it is not allocated any processing time, regardless of its priority.

LongRunningThread.Suspend

' Other code goes here

LongRunningThread.Resume

Event descriptions


Xojo.Threading.Thread.Run

Run

Called when the thread is started by calling the Run method. The code you want to run in the thread should be in this event handler or should be called from it.

Compatibility

All project types on all supported operating systems.

See also

Object parent class; CriticalSection, Semaphore classes