0

I'm developing a program written in qt5, where i need to mount some devices (USB DRIVE) and show in a GUI the contents of that device. I'm using QProcess() to mount the device, and after that i explore the folder where the device should be mounted. Unfortunately only few files can be found.

I tryed to add a QThread::sleep() after the mount command, and this is actually work for small devices. So it seems that the correctness of my approach depends on how much i wait for the system to actually mount the device.

pr3.start("mount foo foo");
pr3.waitForFinished(-1);
QThread::sleep(6); //This is a patch but it fail depending on the amount of file stored in the device

Is there any way to be sure when the QProcess("mount") has finished correctly? I tryed the wawtforfinished(-1) method of QProcess but it doesn't seems to work.

Thanks

Talkanian
  • 19
  • 1
  • 5
  • One thing that needs to be done in any case is to check the return code of the mount command that was run. Did the mount exit successfully, in other words, what is the return code? – Ton van den Heuvel Oct 01 '19 at 10:33
  • Possible duplicate of [Checking whether qprocess has finished](https://stackoverflow.com/questions/38232236/checking-whether-qprocess-has-finished) – Mohammad Kanan Oct 01 '19 at 12:16
  • And check this [read QProcess output to string](https://stackoverflow.com/questions/17344807/read-qprocess-output-to-string) – Mohammad Kanan Oct 01 '19 at 12:18
  • Hi, i tried to connect the process to the finish signal... The fact is that the signal is called instantanely and not when the files are mounted correctly. I think that is like in linux. The "mount" command, automatically gives the console back. Even if you need a "sync" command to make everything properly. i tried to do another process with sync but is just useless – Talkanian Oct 01 '19 at 13:33
  • @Talkanian, I think also you have to create _argument list_ for the process and concatenate `foo` and `foo` as arguments .. or .. put your mount command with arguments in a script that you invoke as a `QProcess` ,, either way , pass a single command as `QProcess` .. , arguments as argument list. – Mohammad Kanan Oct 02 '19 at 06:14
  • @Mohammad A Kanan, "foo foo" is just for instance.. Iv tried it even as arguments. The system assume that the mount went correctly but the script need more time to find the files mounted. – Talkanian Oct 02 '19 at 09:16
  • You need a practical setup, you can interface your process command line output ... if that output does not give the indication you are after.. you can design your own script that mounts .. checks .. and reports to `QProcess` via its standard output .. invoke the script as process .. – Mohammad Kanan Oct 02 '19 at 09:21
  • _The script needs more time to find the files mounted_ .. looks like you need another process in a loop to keep checking successful mount .. exit when mount is good ! – Mohammad Kanan Oct 02 '19 at 09:25

2 Answers2

1

First thing, I would advise you to use the following signal finished() and connect it to a slot that checks the exit code and the exit status instead of using waitForFinished().
Indeed, the documentation states about waitForFinished() that:

Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.

Of course it assumes you have an event loop.


Other thing, your mount foo foo command seems wrong. The first parameter is the device itself and the second one is the directory where to mount the device. You cannot give twice the same parameter.


Please let me know if this does not help to solve your issue.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
  • Hi, i tried to connect the process to the finish signal... The fact is that the signal is called instantanely and not when the files are mounted correctly. I think that is like in linux. The "mount" command, automatically gives the console back. Even if you need a "sync" command to make everything properly. i tried to do another process with sync but is just useless. – Talkanian Oct 01 '19 at 13:33
  • @Talkanian What is the exit status ? The signal is emitted when the process is finished. Same thing in bash, a function returns when finished. If the mount returns right after the call and is not performed, then I suspect there was an error. You have to check for errors, if you don't, you cannot infer anything. – Fareanor Oct 01 '19 at 14:08
  • @Fereanor I got QProcess::ExitStatus(NormalExit) , with exit code = 0. So i think everything is fine.... Beacouse... as i said... if i put a sleep and wait... The drive seems mounted fine. – Talkanian Oct 01 '19 at 14:21
  • @Talkanian Ok, it seems strange. When I have time, I'll do some testing. – Fareanor Oct 02 '19 at 06:51
0

I tryed the wawtforfinished(-1) method of QProcess but it doesn't seems to work.

That is big red flag something strange is going on.

When starting child processes programaticly a lot of error handling and reporting is typically in order.

Also this could include, but is not limited to, that the mount command is not working as expected, etc.

darune
  • 10,480
  • 2
  • 24
  • 62