From 7e87eeb2d412d9abeccb5aaba9335fc1ead201c5 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 15 May 2017 15:53:58 +0800 Subject: [PATCH] Use QThread::requestInterruption() instead of m_abort flag --- src/base/bittorrent/torrentcreatorthread.cpp | 22 +++++++++----------- src/base/bittorrent/torrentcreatorthread.h | 2 -- src/gui/torrentcreatordlg.cpp | 10 +-------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/base/bittorrent/torrentcreatorthread.cpp b/src/base/bittorrent/torrentcreatorthread.cpp index e68838197..3a3d7fce9 100644 --- a/src/base/bittorrent/torrentcreatorthread.cpp +++ b/src/base/bittorrent/torrentcreatorthread.cpp @@ -58,14 +58,13 @@ TorrentCreatorThread::TorrentCreatorThread(QObject *parent) : QThread(parent) , m_private(false) , m_pieceSize(0) - , m_abort(false) { } TorrentCreatorThread::~TorrentCreatorThread() { - m_abort = true; - wait(); + requestInterruption(); + wait(1000); } void TorrentCreatorThread::create(const QString &inputPath, const QString &savePath, const QStringList &trackers, @@ -80,7 +79,6 @@ void TorrentCreatorThread::create(const QString &inputPath, const QString &saveP m_comment = comment; m_private = isPrivate; m_pieceSize = pieceSize; - m_abort = false; start(); } @@ -90,11 +88,6 @@ void TorrentCreatorThread::sendProgressSignal(int numHashes, int numPieces) emit updateProgress(static_cast((numHashes * 100.) / numPieces)); } -void TorrentCreatorThread::abortCreation() -{ - m_abort = true; -} - void TorrentCreatorThread::run() { emit updateProgress(0); @@ -104,7 +97,8 @@ void TorrentCreatorThread::run() libt::file_storage fs; // Adding files to the torrent libt::add_files(fs, Utils::Fs::toNativePath(m_inputPath).toStdString(), fileFilter); - if (m_abort) return; + + if (isInterruptionRequested()) return; libt::create_torrent t(fs, m_pieceSize); @@ -125,7 +119,8 @@ void TorrentCreatorThread::run() t.add_tracker(tracker.trimmed().toStdString(), tier); newline = false; } - if (m_abort) return; + + if (isInterruptionRequested()) return; // calculate the hash for all pieces const QString parentPath = Utils::Fs::branchPath(m_inputPath) + "/"; @@ -136,7 +131,8 @@ void TorrentCreatorThread::run() t.set_comment(m_comment.toUtf8().constData()); // Is private ? t.set_priv(m_private); - if (m_abort) return; + + if (isInterruptionRequested()) return; // create the torrent and print it to out qDebug("Saving to %s", qPrintable(m_savePath)); @@ -152,6 +148,8 @@ void TorrentCreatorThread::run() if (outfile.fail()) throw std::exception(); + if (isInterruptionRequested()) return; + libt::bencode(std::ostream_iterator(outfile), t.generate()); outfile.close(); diff --git a/src/base/bittorrent/torrentcreatorthread.h b/src/base/bittorrent/torrentcreatorthread.h index 7c676adb9..d62af8011 100644 --- a/src/base/bittorrent/torrentcreatorthread.h +++ b/src/base/bittorrent/torrentcreatorthread.h @@ -46,7 +46,6 @@ namespace BitTorrent void create(const QString &inputPath, const QString &savePath, const QStringList &trackers, const QStringList &urlSeeds, const QString &comment, bool isPrivate, int pieceSize); - void abortCreation(); protected: void run(); @@ -66,7 +65,6 @@ namespace BitTorrent QString m_comment; bool m_private; int m_pieceSize; - bool m_abort; }; } diff --git a/src/gui/torrentcreatordlg.cpp b/src/gui/torrentcreatordlg.cpp index 05a619570..a8f8b76df 100644 --- a/src/gui/torrentcreatordlg.cpp +++ b/src/gui/torrentcreatordlg.cpp @@ -86,16 +86,8 @@ TorrentCreatorDlg::~TorrentCreatorDlg() { saveSettings(); - // End torrent creation thread - if (m_creatorThread) { - if (m_creatorThread->isRunning()) { - m_creatorThread->abortCreation(); - m_creatorThread->terminate(); - // Wait for termination - m_creatorThread->wait(); - } + if (m_creatorThread) delete m_creatorThread; - } delete m_ui; }