13#ifndef ML_BACKGROUND_TASK_H
14#define ML_BACKGROUND_TASK_H
22#include <ThirdPartyWarningsDisable.h>
23#include <boost/thread/mutex.hpp>
24#include <boost/thread/condition.hpp>
25#include <ThirdPartyWarningsRestore.h>
32class BackgroundTaskMessage;
33class BackgroundTaskStatusInformation;
67 virtual void run() = 0;
255 friend struct BackgroundTaskManagerWorkerFunc;
264 void aboutToFinish();
274 void setStatus(
Status status);
277 void setStatusIfNotCanceledOrSuspended(
Status status);
280 void setStatusNoLock(
Status status);
283 void runTaskFinishedCallback();
286 std::string _statusString;
288 mutable boost::mutex _statusMonitor;
289 boost::condition _statusNoLongerSuspended;
300 void* _taskFinishedCBUserData;
309 std::vector<Observer*> _observers;
#define MLBACKGROUNDTASKS_EXPORT
Project global and OS specific declarations.
the BackgroundTaskManager is the central singleton that manages running background tasks
The base class of all background messages.
Abstract interface to observe the BackgroundTask.
virtual void logMessage(BackgroundTask *task, double elapsedSeconds, const std::string &message)=0
called when a task logs additional messages (for verbose logging)
virtual ~Observer()
virtual destructor
virtual void statusStringChanged(BackgroundTask *task, double elapsedSeconds, const std::string &status)=0
called when the status string has changed
virtual void statusChanged(BackgroundTask *task, double elapsedSeconds, BackgroundTask::Status status)=0
called when the status has changed
Base class for a task that is started in an extra thread.
void setTaskFinishedCB(BackgroundTaskFinishedCB *callback, void *userdata)
set this directly after initialization of the task (from the GUI thread) this callback will be called...
void suspend()
suspend task, so that it will wait until resume() is called (threadsafe).
void sendStatusStringChangedToObservers()
send the status string change to all observers
void cancel()
cancel the task (sets status to canceled) (threadsafe, typically called from GUI)
void setSynchronousCancelField(NotifyField *field)
set a cancel field that is used in synchronous mode to check if cancel was pressed
void removeObserver(Observer *observer)
remove the given observer (threadsafe) (ownership stays with caller)
void resume()
resume task after it was suspended (will only do something if current status() is Suspended) (threads...
virtual ~BackgroundTask()
void sendMessageToGUI(BackgroundTaskMessage *message)
send the message to the GUI (ownership of message is passed to the method) and it sets the sender of ...
BackgroundTask(void *owner)
Create a new task with given owner (the owner can be used to associate the task with a given class in...
void waitAndExecuteNextMessage()
execute the next message (waits if no messages are available and processes the first message that is ...
void * owner() const
get the owner of the task (only call from GUI thread!)
void addObserver(Observer *observer)
add the given observer (threadsafe) (ownership stays with caller)
void setVerbose(bool flag)
set if task uses verbose logging (call this ONLY on setup of the task, not while it is already runnin...
void getStatusInformation(BackgroundTaskStatusInformation &info)
get the complete status information (threadsafe)
bool shouldStop()
returns if the task should stop its work because it has been canceled.
std::string statusString() const
get current status string (threadsafe)
MLuint32 taskId() const
get the unique id of this task, the id of a task never changes
void setSynchronous(bool flag)
set if task is handled synchronous (call this ONLY on setup of the task, not while it is already runn...
void sendStatusChangedToObservers()
send the status change to all observers
bool hasOwner() const
check if owner was deleted (only call from GUI thread!)
virtual void setProgress(float progress)
update the progress while processing (from run() method only)
void sendLogMessageToObservers(const std::string &msg)
send the status string change to all observers
bool isVerbose() const
check if task uses verbose logging
bool isCanceled() const
check if the task was canceled (threadsafe, typically called from GUI)
float progress() const
get current progress (threadsafe)
Status status() const
returns the status of the task (threadsafe)
double idleTime() const
returns the idle time (in seconds) of the task (which it e.g. waited for getTile to complete),...
bool isSynchronous() const
check if task is synchronous (which means it is NOT run in an extra thread)
void setOwnerWasDeleted()
tell the task that it's owner was deleted (only call from GUI thread!), this also cancels the task
Status
Defines current status of the task.
void logMessage(const std::string &message)
log a message (only logged if verbose logging is turned on) (threadsafe)
virtual void setStatusString(const std::string &status)
update the status string while processing (from run() method only)
virtual void run()=0
run method that needs to be reimplemented (called from worker thread)
void addIdleTime(double timeInSecs)
add given timeInSecs as spent idle time
bool executeNextMessage()
execute the next message (does not block if no message is available) It returns true if there was a m...
double runningTime() const
returns the running time (in seconds) of the task (after it has finished or canceled,...
Field without value for notifications.
Class to measure precise time intervals.
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
void BackgroundTaskFinishedCB(void *data, BackgroundTask *task)