Implemented a better method to abort FileAnalyzer thread.

This commit is contained in:
LoRd_MuldeR 2011-05-16 21:02:24 +02:00
parent 489829fb37
commit 5cccd4de81
6 changed files with 40 additions and 12 deletions

View File

@ -30,7 +30,7 @@
#define VER_LAMEXP_MINOR_LO 2
#define VER_LAMEXP_TYPE Beta
#define VER_LAMEXP_PATCH 1
#define VER_LAMEXP_BUILD 528
#define VER_LAMEXP_BUILD 529
///////////////////////////////////////////////////////////////////////////////
// Tools versions

View File

@ -260,6 +260,7 @@ bool CueImportDialog::analyzeFiles(QStringList &files)
connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
connect(progress, SIGNAL(userAbort()), analyzer, SLOT(abortProcess()), Qt::DirectConnection);
progress->show(tr("Analyzing file(s), please wait..."), analyzer);
progress->close();
@ -286,8 +287,8 @@ void CueImportDialog::splitFiles(void)
CueSplitter *splitter = new CueSplitter(m_outputDir, baseName, m_model, m_fileInfo);
connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
connect(progress, SIGNAL(userAbort()), splitter, SLOT(abortProcess()), Qt::DirectConnection);
connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
connect(progress, SIGNAL(userAbort()), splitter, SLOT(abortProcess()), Qt::DirectConnection);
progress->show(tr("Splitting file(s), please wait..."), splitter);
progress->close();

View File

@ -457,6 +457,7 @@ void MainWindow::addFiles(const QStringList &files)
FileAnalyzer *analyzer = new FileAnalyzer(files);
connect(analyzer, SIGNAL(fileSelected(QString)), m_banner, SLOT(setText(QString)), Qt::QueuedConnection);
connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), m_fileListModel, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
connect(m_banner, SIGNAL(userAbort()), analyzer, SLOT(abortProcess()), Qt::DirectConnection);
m_banner->show(tr("Adding file(s), please wait..."), analyzer);

View File

@ -141,6 +141,7 @@ void WorkingBanner::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape)
{
qDebug("QT::KEY_ESCAPE pressed!");
emit userAbort();
}

View File

@ -42,10 +42,12 @@
FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
:
m_inputFiles(inputFiles),
m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe"))
m_mediaInfoBin(lamexp_lookup_tool("mediainfo.exe")),
m_abortFlag(false)
{
m_bSuccess = false;
m_bAborted = false;
if(m_mediaInfoBin.isEmpty())
{
qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
@ -64,6 +66,7 @@ FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
void FileAnalyzer::run()
{
m_bSuccess = false;
m_bAborted = false;
m_filesAccepted = 0;
m_filesRejected = 0;
@ -71,22 +74,23 @@ void FileAnalyzer::run()
m_filesDummyCDDA = 0;
m_inputFiles.sort();
GetAsyncKeyState(VK_ESCAPE);
m_abortFlag = false;
while(!m_inputFiles.isEmpty())
{
if(GetAsyncKeyState(VK_ESCAPE) & 0x0001)
{
MessageBeep(MB_ICONERROR);
qWarning("Operation cancelled by user!");
break;
}
QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
qDebug64("Analyzing: %1", currentFile);
emit fileSelected(QFileInfo(currentFile).fileName());
AudioFileModel file = analyzeFile(currentFile);
if(m_abortFlag)
{
MessageBeep(MB_ICONERROR);
m_bAborted = true;
qWarning("Operation cancelled by user!");
return;
}
if(file.fileName().isEmpty() || file.formatContainerType().isEmpty() || file.formatAudioType().isEmpty())
{
if(!PlaylistImporter::importPlaylist(m_inputFiles, currentFile))
@ -148,6 +152,13 @@ const AudioFileModel FileAnalyzer::analyzeFile(const QString &filePath)
while(process.state() != QProcess::NotRunning)
{
if(m_abortFlag)
{
process.kill();
qWarning("Process was aborted on user request!");
break;
}
if(!process.waitForReadyRead())
{
if(process.state() == QProcess::Running)
@ -445,6 +456,13 @@ void FileAnalyzer::retrieveCover(AudioFileModel &audioFile, const QString &fileP
while(process.state() != QProcess::NotRunning)
{
if(m_abortFlag)
{
process.kill();
qWarning("Process was aborted on user request!");
break;
}
if(!process.waitForReadyRead())
{
if(process.state() == QProcess::Running)

View File

@ -52,6 +52,9 @@ signals:
void fileSelected(const QString &fileName);
void fileAnalyzed(const AudioFileModel &file);
public slots:
void abortProcess(void) { m_abortFlag = true; }
private:
enum section_t
{
@ -83,5 +86,9 @@ private:
unsigned int m_filesRejected;
unsigned int m_filesDenied;
unsigned int m_filesDummyCDDA;
volatile bool m_abortFlag;
bool m_bAborted;
bool m_bSuccess;
};