Skip to content

Commit

Permalink
Fix update progressinfo from thread (#124)
Browse files Browse the repository at this point in the history
* fix: progress dialog should not update gui in secondary thread

Co-authored-by: Bryn Lloyd <[email protected]>
  • Loading branch information
dyollb and dyollb authored Oct 13, 2021
1 parent 5a2bd1d commit 6f38924
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
9 changes: 0 additions & 9 deletions Core/Morpho.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
#include <itkImageRegionIteratorWithIndex.h>
#include <itkPasteImageFilter.h>

#ifndef NO_OPENMP_SUPPORT
# include <omp.h>
#endif

#include <boost/variant.hpp>

namespace iseg {
Expand Down Expand Up @@ -182,11 +178,6 @@ void MorphologicalOperation(iseg::SlicesHandlerInterface* handler, boost::varian
progress->SetNumberOfSteps(endslice - startslice);
}

#ifndef NO_OPENMP_SUPPORT
const auto guard = MakeScopeExit([nthreads = omp_get_num_threads()]() { omp_set_num_threads(nthreads); });
omp_set_num_threads(std::max<int>(2, std::thread::hardware_concurrency() / 2));
#endif

#pragma omp parallel for
for (std::int64_t slice = startslice; slice < endslice; ++slice)
{
Expand Down
19 changes: 13 additions & 6 deletions Interface/ProgressDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ProgressDialog::ProgressDialog(const char* msg, QWidget* parent /*= 0*/)

m_Count.store(0);

QObject_connect(this, SIGNAL(OnValueChanged(int)), this, SLOT(UpdateValue(int)));
QObject_connect(cancel_button, SIGNAL(clicked()), this, SLOT(Cancel()));
}

Expand All @@ -32,7 +33,7 @@ void ProgressDialog::SetNumberOfSteps(int N)
void ProgressDialog::Increment()
{
m_Count++;
m_Progress->setValue(m_Count);
OnValueChanged(m_Count);
}

bool ProgressDialog::WasCanceled() const
Expand All @@ -41,6 +42,17 @@ bool ProgressDialog::WasCanceled() const
}

void ProgressDialog::SetValue(int percent)
{
m_Count = percent;
OnValueChanged(percent);
}

void ProgressDialog::Cancel()
{
m_Canceled = true;
}

void ProgressDialog::UpdateValue(int percent)
{
if (m_Progress->value() != percent)
{
Expand All @@ -50,9 +62,4 @@ void ProgressDialog::SetValue(int percent)
}
}

void ProgressDialog::Cancel()
{
m_Canceled = true;
}

} // namespace iseg
4 changes: 4 additions & 0 deletions Interface/ProgressDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class ISEG_INTERFACE_API ProgressDialog
bool WasCanceled() const override;
void SetValue(int percent) override;

signals:
void OnValueChanged(int);

private slots:
void Cancel();
void UpdateValue(int);

private:
bool m_Canceled = false;
Expand Down

0 comments on commit 6f38924

Please sign in to comment.