Qt-solutions Archive, April 2006
AW: AW: QtLockedFile does not work
Message 1 in thread
Hi
File a bug report - I was able to reproduce your behaviour (XP, 4.1.2) with an "all in one" program (I was planning on using the solution also, but maybe I don't after this...)
Also the same behaviour appeared when not blocking (nothing got appended to the file)
#include <QtGui/QApplication>
#include <QDebug>
#include <QProcess>
#include "qtlockedfile.h"
int main(int argc, char *argv[])
{
if (argc > 1)
{
qDebug() << "Locking file...";
QtLockedFile lf("test.log");
if (lf.open( QIODevice::ReadWrite|QIODevice::Append ))
{
qDebug() << lf.lockMode();
if (lf.lock( QtLockedFile::WriteLock, false ))
{
qDebug() << "Locked file" << lf.lockMode();
QString message = QString( "[%1] hello world\n" ).arg( argv[1] );
lf.write(message.toLatin1().constData(), qstrlen(message.toLatin1().constData()));
lf.flush();
}
qDebug() << "Unlocking file...";
lf.unlock();
qDebug() << "Unlocked file" << lf.lockMode();
lf.close();
}
}
else
{
for(int i = 0; i < 2; i++ )
{
QProcess::startDetached( "TestProject.exe", QStringList() << QString::number(i) );
}
}
return 0;
}
Regards,
Michael
-----Ursprüngliche Nachricht-----
Von: Falko Buttler [mailto:falko.buttler@xxxxxxxx]
Gesendet: Freitag, 07. April 2006 10:12
An: qt-solutions@xxxxxxxxxxxxx
Betreff: Re: AW: QtLockedFile does not work
Hello Michael,
no you have to use this order. If you first lock and then open you get
an debug message of QtLockedFile that the file is not opened yet.
But thanks for your help.
Regards,
Falko
Michael.Soukup@.com schrieb:
> Hi
>
> Just a guess as I did not use this
> Try to lock the file _before_ open()ing it
>
> Michael
>
> -----Ursprüngliche Nachricht-----
> Von: Falko Buttler [mailto:falko.buttler@.de]
> Gesendet: Freitag, 07. April 2006 09:37
> An: qt-solutions@xxxxxxxxxxxxx
> Betreff: QtLockedFile does not work
>
> Hello,
>
> does anybody of you use the QtLockedFile solution for Qt 4?
> Under Windows XP it does not work correct until there are parallel
> processes who want to access the file.
>
> Here my small example code:
>
> #include "qtlockedfile.h"
>
> #include <QtDebug>
> #include <QString>
>
> #ifdef Q_OS_WIN32
> // for getting process id under windows
> #include <process.h>
> #else
> // for getting process id under unix systems
> #include <unistd.h>
> #endif
>
> int main( int, char** )
> {
> qDebug() << "Locking file...";
>
> QtLockedFile lf("test.log");
> lf.open( QIODevice::WriteOnly|QIODevice::Append );
>
> lf.lock( QtLockedFile::WriteLock, true );
>
> qDebug() << "Locked file";
>
> QString message = QString( "[%1] hello world\n" ).arg( getpid() );
> lf.write(message.toLatin1().constData(),
> qstrlen(message.toLatin1().data()));
> lf.flush();
>
> qDebug() << "Unlocking file...";
> lf.unlock();
>
> qDebug() << "Unlocked file";
>
> return 0;
> }
>
>
> Here the code to force parallel processes to access the file:
>
> #include <QProcess>
>
> int main( int, char** )
> {
> for( int i = 0; i < 10; i++ ) {
> QProcess::startDetached( "lockedFileWriter.exe" );
> }
>
> return 0;
> }
>
>
> If you do that, there are 10 processes which all freeze and wait for a
> unlocked file .... forever.
>
> What am I doing wrong? I am sure someone at Trolltech has tested this
> and it is a fault on my side but I cannot find one myself.
>
> Regards,
> Falko
>
--
[ signature omitted ]