JUCE

- Main Page
- Related Pages
- Topics
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Loading...
Searching...
No Matches
Public Member Functions | List of all members
ThreadWithProgressWindow Class Reference
juce_gui_basics » windows

A thread that automatically pops up a modal dialog box with a progress bar and cancel button while it's busy running. More...

`#include <juce_ThreadWithProgressWindow.h>`

Inheritance diagram for ThreadWithProgressWindow:

## Public Member Functions
| ThreadWithProgressWindow (const String &windowTitle, bool hasProgressBar, bool hasCancelButton, int timeOutMsWhenCancelling=10000, const String &cancelButtonText=String(), Component *componentToCentreAround=nullptr)
| Creates the thread.
| ~ThreadWithProgressWindow () override
| Destructor.
bool | runThread (Priority priority=Priority::normal)
| Starts the thread and waits for it to finish.
void | launchThread (Priority priority=Priority::normal)
| Starts the thread and returns.
void | setProgress (double newProgress)
| The thread should call this periodically to update the position of the progress bar.
void | setStatusMessage (const String &newStatusMessage)
| The thread can call this to change the message that's displayed in the dialog box.
AlertWindow * | getAlertWindow () const noexcept
| Returns the AlertWindow that is being used.
virtual void | threadComplete (bool userPressedCancel)
| This method is called (on the message thread) when the operation has finished.
Public Member Functions inherited from Thread
| Thread (const String &threadName, size_t threadStackSize=osDefaultStackSize)
| Creates a thread.
virtual | ~Thread ()
| Destructor.
virtual void | run ()=0
| Must be implemented to perform the thread's actual code.
bool | startThread ()
| Attempts to start a new thread with default ('Priority::normal') priority.
bool | startThread (Priority newPriority)
| Attempts to start a new thread with a given priority.
bool | startRealtimeThread (const RealtimeOptions &options)
| Starts the thread with realtime performance characteristics on platforms that support it.
bool | stopThread (int timeOutMilliseconds)
| Attempts to stop the thread running.
bool | isThreadRunning () const
| Returns true if the thread is currently active.
void | signalThreadShouldExit ()
| Sets a flag to tell the thread it should stop.
bool | threadShouldExit () const
| Checks whether the thread has been told to stop running.
bool | waitForThreadToExit (int timeOutMilliseconds) const
| Waits for the thread to stop.
void | addListener (Listener *)
| Add a listener to this thread which will receive a callback when signalThreadShouldExit was called on this thread.
void | removeListener (Listener *)
| Removes a listener added with addListener.
bool | isRealtime () const
| Returns true if this Thread represents a realtime thread.
void | setAffinityMask (uint32 affinityMask)
| Sets the affinity mask for the thread.
bool | wait (double timeOutMilliseconds) const
| Suspends the execution of this thread until either the specified timeout period has elapsed, or another thread calls the notify() method to wake it up.
void | notify () const
| Wakes up the thread.
ThreadID | getThreadId () const noexcept
| Returns the ID of this thread.
const String & | getThreadName () const noexcept
| Returns the name of the thread.

## Additional Inherited Members
Public Types inherited from Thread
enum class | Priority { highest = 2 , high = 1 , normal = 0 , low = -1 , background = -2 }
| The different runtime priorities of non-realtime threads. More...
using | ThreadID = void*
| A value type used for thread IDs.
Static Public Member Functions inherited from Thread
static bool | launch (std::function< void()> functionToRun)
| Invokes a lambda or function on its own thread with the default priority.
static bool | launch (Priority priority, std::function< void()> functionToRun)
| Invokes a lambda or function on its own thread with a custom priority.
static bool | currentThreadShouldExit ()
| Checks whether the current thread has been told to stop running.
static void JUCE_CALLTYPE | setCurrentThreadAffinityMask (uint32 affinityMask)
| Changes the affinity mask for the caller thread.
static void JUCE_CALLTYPE | sleep (int milliseconds)
| Suspends the execution of the current thread until the specified timeout period has elapsed (note that this may not be exact).
static void JUCE_CALLTYPE | yield ()
| Yields the current thread's CPU time-slot and allows a new thread to run.
static ThreadID JUCE_CALLTYPE | getCurrentThreadId ()
| Returns an id that identifies the caller thread.
static Thread *JUCE_CALLTYPE | getCurrentThread ()
| Finds the thread object that is currently running.
static void JUCE_CALLTYPE | setCurrentThreadName (const String &newThreadName)
| Changes the name of the caller thread.
static void | initialiseJUCE (void *jniEnv, void *jContext)
| Initialises the JUCE subsystem for projects not created by the Projucer.
Static Public Attributes inherited from Thread
static constexpr size_t | osDefaultStackSize { 0 }
Protected Member Functions inherited from Thread
Priority | getPriority () const
| Returns the current priority of this thread.
bool | setPriority (Priority newPriority)
| Attempts to set the priority for this thread.

## Detailed Description

A thread that automatically pops up a modal dialog box with a progress bar and cancel button while it's busy running.

These are handy for performing some sort of task while giving the user feedback about how long there is to go, etc.

E.g.

class MyTask : public ThreadWithProgressWindow
{
public:
MyTask() : ThreadWithProgressWindow ("busy...", true, true)
{
}
void run()
{
for (int i = 0; i < thingsToDo; ++i)
{
// must check this as often as possible, because this is
// how we know if the user's pressed 'cancel'
if (threadShouldExit())
break;
// this will update the progress bar on the dialog box
setProgress (i / (double) thingsToDo);
// ... do the business here...
}
}
};
void doTheTask()
{
MyTask m;
if (m.runThread())
{
// thread finished normally..
}
else
{
// user pressed the cancel button..
}
}
ThreadWithProgressWindow
A thread that automatically pops up a modal dialog box with a progress bar and cancel button while it...
Definition juce_ThreadWithProgressWindow.h:86

See also
  Thread, AlertWindow

## Constructor & Destructor Documentation

## ◆ ThreadWithProgressWindow()

ThreadWithProgressWindow::ThreadWithProgressWindow | ( | const String & | windowTitle,
|  | bool | hasProgressBar,
|  | bool | hasCancelButton,
|  | int | timeOutMsWhenCancelling = 10000,
|  | const String & | cancelButtonText = String(),
|  | Component * | componentToCentreAround = nullptr )

Creates the thread.

Initially, the dialog box won't be visible, it'll only appear when the runThread() method is called.

Parameters

  windowTitle | the title to go at the top of the dialog box
  hasProgressBar | whether the dialog box should have a progress bar (see setProgress() )
  hasCancelButton | whether the dialog box should have a cancel button
  timeOutMsWhenCancelling | when 'cancel' is pressed, this is how long to wait for the thread to stop before killing it forcibly (see Thread::stopThread() )
  cancelButtonText | the text that should be shown in the cancel button (if it has one). Leave this empty for the default "Cancel"
  componentToCentreAround | if this is non-null, the window will be positioned so that it's centred around this component.

## ◆ ~ThreadWithProgressWindow()

ThreadWithProgressWindow::~ThreadWithProgressWindow | ( |  | ) |
| override

Destructor.

## Member Function Documentation

## ◆ runThread()

bool ThreadWithProgressWindow::runThread | ( | Priority | priority = Priority::normal | ) |

Starts the thread and waits for it to finish.

This will start the thread, make the dialog box appear, and wait until either the thread finishes normally, or until the cancel button is pressed.

Before returning, the dialog box will be hidden.

Parameters

  priority | the priority to use when starting the thread - see Thread::Priority for values

Returns
  true if the thread finished normally; false if the user pressed cancel

## ◆ launchThread()

void ThreadWithProgressWindow::launchThread | ( | Priority | priority = Priority::normal | ) |

Starts the thread and returns.

This will start the thread and make the dialog box appear in a modal state. When the thread finishes normally, or the cancel button is pressed, the window will be hidden and the threadComplete() method will be called.

Parameters

  priority | the priority to use when starting the thread - see Thread::Priority for values

## ◆ setProgress()

void ThreadWithProgressWindow::setProgress | ( | double | newProgress | ) |

The thread should call this periodically to update the position of the progress bar.

Parameters

  newProgress | the progress, from 0.0 to 1.0

See also
  setStatusMessage

## ◆ setStatusMessage()

void ThreadWithProgressWindow::setStatusMessage | ( | const String & | newStatusMessage | ) |

The thread can call this to change the message that's displayed in the dialog box.

## ◆ getAlertWindow()

AlertWindow * ThreadWithProgressWindow::getAlertWindow | ( |  | ) | const
| noexcept

Returns the AlertWindow that is being used.

## ◆ threadComplete()

virtual void ThreadWithProgressWindow::threadComplete | ( | bool | userPressedCancel | ) |
| virtual

This method is called (on the message thread) when the operation has finished.

You may choose to use this callback to delete the ThreadWithProgressWindow object.

The documentation for this class was generated from the following file:

- juce_ThreadWithProgressWindow.h