Results 1 to 17 of 17

Thread: start QTimer form C#

  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default start QTimer form C#

    Hi everyone,
    I have a c++ DLL and inside the DLL one QTimer is present. I am trying to start the timer by calling a function from C# application. But it is not working.

    Is it true that QTimer requires an event loop to work ? Is C# application has an event loop ? How I will make it work ?

    Please help me.

    thanks.

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

    Default Re: start QTimer form C#

    Quote Originally Posted by bibhukalyana View Post
    Is it true that QTimer requires an event loop to work ?
    Yes.
    Is C# application has an event loop ?
    This needs to be a Qt event loop, not just any event loop.

    How I will make it work ?
    You need an instance of QCoreApplication and a call to its exec() method (which blocks until you quit the event loop).
    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.


  3. #3
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    Thanks for reply.

    I tried instance = QCoreApplication::instance();

    But I am getting a null pointer.

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

    Default Re: start QTimer form C#

    If you didn't create an instance of that class then there is no instance to be returned. QCoreApplication is not a classic singleton, it does not create an instance inside instance(), you need to construct the instance yourself and then it can be returned via instance().
    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.


  5. #5
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    Can you please tell me how to create the instance ?

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

    Default Re: start QTimer form C#

    Qt Code:
    1. QCoreApplication app(argc, argv);
    To copy to clipboard, switch view to plain text mode 
    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.


  7. #7
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    Thanks wysota,
    I am able to create the instance.But my next problem is QCoreApplication::exec() is a blocking function(Is it true).

    Qt Code:
    1. QCoreApplication app(argc,argv);
    2. instance = QCoreApplication::instance();
    3. if(instance)
    4. {
    5. instance->exec();//bolcking function
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: start QTimer form C#

    Well... that's the way it is supposed to work Instead of calling exec(), you can periodically call QCoreApplication::processEvents() from your C# event processing code. This will make Qt process its pending events, including timers.
    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
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    I tried QCoreApplication:rocessEvents() instead of QCoreApplication::exec(), but still it is not working. I tried QCoreApplication::hasPendingEvents() and there was no pending events.

    I think if I will not call exec then no event loop will be created.

    Please check what i am doing..

    In constructor : creating instance of QCoreApplication.

    In memberfun1 : starting timer

    In memberfun2 : calling QCoreApplication:rocessEvents()

    memberfun2 is calling continuously from c# which is inside a thread.

    My qt version is 4.7.
    Last edited by bibhukalyana; 10th October 2013 at 08:18.

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

    Default Re: start QTimer form C#

    How is memberfun2 "continuously" calling processEvents?
    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
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    It is inside a while loop.

    Qt Code:
    1. do
    2. {
    3. status = export_memberfun2();
    4. }while(status != DONE)
    To copy to clipboard, switch view to plain text mode 

    export_memberfun2() is the exported function of dll.

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

    Default Re: start QTimer form C#

    So how does that differ from calling exec()?
    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
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    I did not get you.
    My while loop is in different thread.So it will block that thread.

    constructor and export_memberfun1 are in GUI thread. So the Qtimer is also in GUI thread. The QTimer instance is in GUI thread so i can start it from GUI thread.For that reason i am tyring to start timer in my 1st function and also i have to check the status. So i am using the second function inside a thread.

    If I will call exec()/processEvent() in GUI then it will block my application and i need to check it continously without blocking GUI.

    The same thing is working fine in Qt application. The difference is in Qt application instead of while loop i am using a timer of 0 for checking the status.

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

    Default Re: start QTimer form C#

    Quote Originally Posted by bibhukalyana View Post
    I did not get you.
    My while loop is in different thread.So it will block that thread.
    So call exec() in this thread. Note that QCoreApplication instance as well as any QObject instances need to be created in the same thread.
    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.


  15. #15
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    Ok I got it.
    You are telling that there will be 3 thread, gui thread , exec thread and one more for checking status.

    Actually i was using timer for polling purpose.
    Now i have 3 option.
    1st 3 thread; 2nd i will mearge last 2 thread and use a blocking function; 3rd instead of using timer inside dll i will use it in my application side.

    If you know a better way of polling data then please suggest me.
    Thanks.

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

    Default Re: start QTimer form C#

    Quote Originally Posted by bibhukalyana View Post
    You are telling that there will be 3 thread, gui thread , exec thread and one more for checking status.
    I'm not telling you anything. I told you that if you want QTimer to work, you need a running event loop.

    Actually i was using timer for polling purpose.
    Now i have 3 option.
    1st 3 thread; 2nd i will mearge last 2 thread and use a blocking function; 3rd instead of using timer inside dll i will use it in my application side.

    If you know a better way of polling data then please suggest me.
    I would basically ask you why are you trying to merge C# and C++/Qt worlds and can't you use timers offered by your C# environment.
    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.


  17. #17
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: start QTimer form C#

    I want to create a intermediate DLL which will communicate with hardware so it will be easy to write in C++ and it will be platform independent.

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. QTimer unnestable with every time i stop/start it
    By aguayro in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2012, 17:55
  3. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  4. Replies: 2
    Last Post: 11th February 2011, 19:48
  5. QTimer don't start
    By Qn00b in forum Qt Programming
    Replies: 13
    Last Post: 7th February 2011, 23:02

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.