algo_thread.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:5k
- /*
- * ===========================================================================
- * PRODUCTION $Log: algo_thread.hpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 19:46:37 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_CORE___ALGO_THREAD__HPP
- #define GUI_CORE___ALGO_THREAD__HPP
- /* $Id: algo_thread.hpp,v 1000.2 2004/06/01 19:46:37 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio
- *
- * File Description:
- *
- */
- #include <corelib/ncbithr.hpp>
- #include <corelib/ncbi_system.hpp>
- #include <gui/core/algo.hpp>
- #include <gui/dialogs/progress/progress_dlg_ex.hpp>
- BEGIN_NCBI_SCOPE
- template <typename PluginType>
- class CThreadedAlgorithm : public CAlgorithm<PluginType>
- {
- public:
- CThreadedAlgorithm();
- virtual ~CThreadedAlgorithm();
- // for manipulating our progress messages
- void AddProgressMessage(const string& str);
- void SetCompleted(int pct);
- // we overload RunCommand() to set up our thread and run
- virtual void RunCommand(objects::CPluginMessage& msg);
- // internal hook - RunCommand() re-routes to use this instead
- virtual void RunThreaded(objects::CPluginMessage& msg) = 0;
- protected:
- // determine if we've been interrupted
- bool x_IsInterrupted() const;
- /// our internal thread class
- class CWorkerThread : public CThread
- {
- public:
- CWorkerThread(CThreadedAlgorithm& parent,
- objects::CPluginMessage& msg)
- : m_Parent(parent)
- , m_Message(msg)
- , m_Completed(false)
- {
- }
- void* Main()
- {
- m_Parent.RunThreaded(m_Message);
- m_Completed = true;
- return (void*)NULL;
- }
- bool IsCompleted(void) const
- {
- return m_Completed;
- }
- private:
- CThreadedAlgorithm& m_Parent;
- objects::CPluginMessage& m_Message;
- bool m_Completed;
- };
- /// a progress dialog
- CProgressDlgEx m_Progress;
- // flag to monitor user interruption via "cancel"
- bool m_IsInterrupted;
- };
- template <typename PluginType>
- inline
- CThreadedAlgorithm<PluginType>::CThreadedAlgorithm()
- {
- }
- template <typename PluginType>
- inline
- CThreadedAlgorithm<PluginType>::~CThreadedAlgorithm()
- {
- }
- template <typename PluginType>
- inline
- void CThreadedAlgorithm<PluginType>::AddProgressMessage(const string& str)
- {
- m_Progress.SetTitle(str);
- }
- template <typename PluginType>
- inline
- void CThreadedAlgorithm<PluginType>::SetCompleted(int pct)
- {
- m_Progress.SetPctCompleted(pct);
- }
- template <typename PluginType>
- inline
- void CThreadedAlgorithm<PluginType>::RunCommand(objects::CPluginMessage& msg)
- {
- m_Progress.CenterOnActive();
- m_Progress.Show();
- #ifdef NCBI_THREADS
- try {
- CRef<CWorkerThread> thread(new CWorkerThread(*this, msg));
- thread->Run();
- while ( !thread->IsCompleted() && m_Progress.IsShown()) {
- Fl::wait(0.05f);
- }
- m_IsInterrupted = true;
- if (thread->IsCompleted()) {
- thread->Join();
- }
- thread.Reset();
- }
- catch (...) {
- }
- #else
- // FIXME: figure out why my stupid window won't update...
- Fl::wait(0.05f);
- RunThreaded(msg);
- #endif
- m_Progress.Hide();
- }
- template <typename PluginType>
- inline
- bool CThreadedAlgorithm<PluginType>::x_IsInterrupted(void) const
- {
- return m_IsInterrupted;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: algo_thread.hpp,v $
- * Revision 1000.2 2004/06/01 19:46:37 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4
- *
- * Revision 1.4 2004/04/26 17:32:32 ucko
- * Fix for current progress dialog API.
- *
- * Revision 1.3 2003/12/22 19:10:46 dicuccio
- * Center the progress dialog over the active window
- *
- * Revision 1.2 2003/12/04 18:06:10 dicuccio
- * Added primitive interruption handler. Added Fl::wait() to update the GUI
- *
- * Revision 1.1 2003/11/26 17:04:47 dicuccio
- * Added CThreadedAlgorithm
- *
- * ===========================================================================
- */
- #endif // GUI_CORE___ALGO_THREAD__HPP