Results 1 to 19 of 19

Thread: singleShot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    In other words, let's consider this, whenever a particular condition sets to TRUE, I need to start a timer for a specific amount of time and then it goes dim till the condition goes true again.
    I don't understand what you mean. If you mean that you want it to fire once then make it a single shot timer by calling setSingleShot(true).

    For this case, which one do yo think is better

    -connect the timer, set interval, set singleshot to true. and.. in this case I think everytime I need to activate the timer I should call restart

    the other case is to:
    -connect the timer, setinterval, and set the timer to start and stop when it times out.
    The first one.

    is there any way to wait for some period of time before executing the next instructions then? imagine I don't want to call any slots in some cases, I only want to delay.
    Call sleep().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    15
    Thanked 16 Times in 15 Posts

    Default Re: singleShot

    It's not a good idea to use sleep(), as it would suspend all Timers n connections for a while. is there any other way to do it?

    if I use this
    Qt Code:
    1. QTimer::singleShot(msec, &loop, NULL);
    2. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    will it stop the instruction execution or not.?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: singleShot

    is there any way to wait for some period of time before executing the next instructions then? imagine I don't want to call any slots in some cases, I only want to delay.
    That's what you wanted, wasn't it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    15
    Thanked 16 Times in 15 Posts

    Default Re: singleShot

    I don't know if you get my point or not. I cannot use sleep() as it will damage all the QTimers (suspends them all).
    I just want to delay for some seconds. And, I don't want to use as many slots to put the rest of my code inside it.

    sleep() suspends all running threads or timers. I don't want that, I only want to delay one particular thread.
    Last edited by saman_artorious; 9th June 2012 at 13:42.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: singleShot

    sounds like you are stuck on trying to implement YOUR solution to a problem we don't know anything about.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    15
    Thanked 16 Times in 15 Posts

    Default Re: singleShot

    consider the following:

    Qt Code:
    1. void FuelSystem::DSLFL_DataReceive(QByteArray data)
    2. {
    3. /* 4)Lower sensor 5)upper Sensor 6)water sensor */
    4.  
    5. QByteArray command_SV15;
    6. QByteArray command_HV14;
    7. QByteArray command_FTWL_SV9;
    8.  
    9. if(data.at(3) == FUEL_SYSTEM)
    10. {
    11. if(data.at(4) == 0x01)
    12. {
    13. FuelPump_start();
    14.  
    15. command_SV15.append(FUEL_SYSTEM);
    16. command_SV15.append(DSLFL_ST_SV_15);
    17. command_SV15.append(0x01);
    18.  
    19. emit DSLFL_SendToRS485(command_SV15);
    20. delay(_DELAY_MSEC);
    21. }
    22. else if(data.at(5) == 0x01)
    23. {
    24. FuelPump_stop();
    25.  
    26. command_HV14.append(FUEL_SYSTEM);
    27. command_HV14.append(DSLFL_HT_SV_14);
    28. command_HV14.append((char) 0x00);
    29.  
    30. emit DSLFL_SendToRS485(command_HV14);
    31. delay(_DELAY_MSEC);
    32. }
    33.  
    34. if((data.at(6) == 0x01) && (pumpStat == OFF))
    35. {
    36. command_FTWL_SV9.append(FUEL_SYSTEM);
    37. command_FTWL_SV9.append(DSLFLWaterEvacuate_SV_9);
    38. command_FTWL_SV9.append(0x01);
    39.  
    40. emit DSLFL_SendToRS485(command_FTWL_SV9);
    41. delay(_DELAY_MSEC);
    42.  
    43. waterEvacuateTimer->start();
    44. }
    45. }
    46. }
    47.  
    48. void FuelSystem::delay(int msec){
    49. QEventLoop loop;
    50. QTimer::singleShot(msec, &loop, NULL);
    51. loop.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 

    As I send a packet to PLC, I need to delay for some time, waiting for the result of PLC operations. Here, I cannot use sleep() as it is a system call and it will suspend all other
    QTimers and the whole program. what I want is to only delay the current particular thread or object for 2 seconds everytime I connect to PLC.

    I hope this is clear now. : )

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: singleShot

    so you are just dealing with asynch io. This is not new problem and isn't solved best with blocking calls. Where is your code that you say 'needs to wait'?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    sleep() suspends all running threads or timers. I don't want that, I only want to delay one particular thread.
    Sleep doesn't suspend any timers, those are handled by the operating system. Sleep suspends a thread. I have no idea why you want to delay anything at the places you marked in your code. Just put each if in a separate slot and upon ending one slot, start a timer to trigger the next one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: singleShot

    should he even be using timers? Looks like the code should be moved into slots, and called by something that is signalled when new data arrives.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by amleto View Post
    should he even be using timers?
    Probably not.
    Looks like the code should be moved into slots, and called by something that is signalled when new data arrives.
    That's right, provided he has some ability to be informed of data arrival.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    15
    Thanked 16 Times in 15 Posts

    Default Re: singleShot

    for example, if I want to command a valve to open via PLC. or let's say I want to switch on a pump, I need to delay for quite a small period of time, waiting for that valve to open in reality.
    then check via proximity switches whether it really got open or not after 2 seconds for example. That's why I need to delay for two seconds and the carry on with the rest of the instructions in that particular thread.
    if sleep() suspends threads then it's not a good idea to thread, I may use it in the above code to delay that thread, but it suspends all other threads as well.

    I have also used singleShot using SLOT, but in some cases I don not want to execute any SLOTS, just delaying for two sec.
    I think everything's now clear for you all. : )

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: singleShot

    Quote Originally Posted by saman_artorious View Post
    then check via proximity switches whether it really got open or not after 2 seconds for example.
    And if not?

    if sleep() suspends threads then it's not a good idea to thread,
    ???

    I may use it in the above code to delay that thread, but it suspends all other threads as well.
    Maybe you don't have other threads...

    I have also used singleShot using SLOT, but in some cases I don not want to execute any SLOTS, just delaying for two sec.
    Delay what?

    Usually if you want to delay something then you want this something to happen after that delay and not that you don't want anything to happen after that period of time.

    I'm leaning more and more to an opinion that you either have some misconception or misdesign in your program. To be honest, blocking anything in a reactive system such as one controlling some pump that can flood something seems to be a mistake on its own.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanked 2 Times in 2 Posts

    Default Re: singleShot

    Just my two cents: have you considered using a QSemaphore::tryAcquire() with timeout ?
    Qt Code:
    1. QSemaphore blocker;
    2. blocker.tryAcquire (1, delay);
    To copy to clipboard, switch view to plain text mode 
    That would block the calling thread and only it, with no interference with other threads, timers or whatever.
    Of course the thread would be dead to the outer world during the wait, so the GUI would better have to run somewhere else.

    As for polling an asynchronous device, I would suggest to create a dedicated thread instead of blocking the one issuing the command. But that's answering a question you did not ask, so...

Similar Threads

  1. QTimer::singleShot(0, ... confusion
    By bjoern83 in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2022, 10:30
  2. Replies: 3
    Last Post: 31st January 2010, 16:56
  3. Replies: 8
    Last Post: 10th December 2009, 10:06
  4. multiple QTimer::singleShot() calls?
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2009, 19:22
  5. Replies: 1
    Last Post: 6th April 2006, 12:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.