Skip to content

Commit

Permalink
JLatentPG: do nothing unless thread is interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
jafl committed May 17, 2024
1 parent 1079bf9 commit c4cc478
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
19 changes: 14 additions & 5 deletions libjcore/code/JLatentPG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ JLatentPG::ProcessBeginning
void
JLatentPG::StartInternalProcess()
{
if (!JIsInteractiveThread())
{
return;
}

const ProcessType type = GetCurrentProcessType();
if (type == kFixedLengthProcess)
{
Expand Down Expand Up @@ -185,7 +190,7 @@ JLatentPG::IncrementProgress
bool result = true;

itsCounter++;
if (!pgRunning && (TimeToStart() || !message.IsEmpty()))
if (JIsInteractiveThread() && !pgRunning && (TimeToStart() || !message.IsEmpty()))
{
StartInternalProcess();

Expand All @@ -196,7 +201,7 @@ JLatentPG::IncrementProgress

itsCounter = 0;
}
else if (pgRunning && (TimeToUpdate() || !message.IsEmpty()))
else if (JIsInteractiveThread() && pgRunning && (TimeToUpdate() || !message.IsEmpty()))
{
result = itsPG->IncrementProgress(message,
GetCurrentStepCount() - itsPG->GetCurrentStepCount());
Expand All @@ -215,7 +220,8 @@ JLatentPG::IncrementProgress
bool
JLatentPG::ProcessContinuing()
{
return !itsPG->ProcessRunning() || itsPG->ProcessContinuing();
return (!JIsInteractiveThread() ||
!itsPG->ProcessRunning() || itsPG->ProcessContinuing());
}

/******************************************************************************
Expand All @@ -239,7 +245,7 @@ JLatentPG::ProcessFinished()
{
JProgressDisplay::ProcessFinished();

if (itsPG->ProcessRunning())
if (JIsInteractiveThread() && itsPG->ProcessRunning())
{
itsPG->ProcessFinished();
}
Expand All @@ -253,7 +259,10 @@ JLatentPG::ProcessFinished()
void
JLatentPG::DisplayBusyCursor()
{
itsPG->DisplayBusyCursor();
if (JIsInteractiveThread())
{
itsPG->DisplayBusyCursor();
}
}

/******************************************************************************
Expand Down
27 changes: 27 additions & 0 deletions libjcore/code/jGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static JWebBrowser* theWebBrowser = nullptr;

static std::function<void(const std::function<void()>&)>* theTaskScheduler = nullptr;

static thread_local bool theInteractiveThreadFlag = false;
static std::recursive_mutex theTempDirChangeMutex;

/******************************************************************************
Expand Down Expand Up @@ -325,6 +326,32 @@ JScheduleTask
}
}

/******************************************************************************
Interactive thread
There is typically only one interactive thread in a program, because
it gets too confusing otherwise.
This is used by certain objects, e.g., JLatentPG, to decide whether
or not to display anything.
******************************************************************************/

bool
JIsInteractiveThread()
{
return theInteractiveThreadFlag;
}

void
JSetThreadIsInteractive
(
const bool interactive
)
{
theInteractiveThreadFlag = interactive;
}

/******************************************************************************
JGetTemporaryDirectoryChangeMutex
Expand Down
3 changes: 3 additions & 0 deletions libjcore/code/jGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ bool JGetDataDirectories(const JUtf8Byte* signature, const JUtf8Byte* dirName,
void JSetTaskScheduler(const std::function<void(const std::function<void()>&)>& sched);
void JScheduleTask(const std::function<void()>& f);

bool JIsInteractiveThread();
void JSetThreadIsInteractive(const bool interactive = true);

std::recursive_mutex& JGetTemporaryDirectoryChangeMutex();

/******************************************************************************
Expand Down
1 change: 1 addition & 0 deletions libjx/code/JXApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ JXApplication::JXApplication
itsIsQuittingFlag(false),
itsUrgentTaskChannel(nullptr)
{
JSetThreadIsInteractive();
theUIThreadFlag = true;

std::cout << std::boolalpha; // since it will only be used for debugging
Expand Down
4 changes: 1 addition & 3 deletions libjx/code/JXProgressDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ JXProgressDisplay::ProcessBeginning
const bool origModal
)
{
const bool modal = modal && IsWorkerFiber();
const bool modal = origModal && JXApplication::IsWorkerFiber();

itsCancelFlag = false;
JProgressDisplay::ProcessBeginning(processType, stepCount, message,
Expand Down Expand Up @@ -355,8 +355,6 @@ JXProgressDisplay::Receive
{
if (sender == itsCancelButton && message.Is(JXButton::kPushed))
{
auto* info = dynamic_cast<const JXButton::Pushed*>(&message);
assert( info != nullptr );
itsCancelFlag = true;

// must be last since it could delete us
Expand Down

0 comments on commit c4cc478

Please sign in to comment.