PDA

View Full Version : start QTimer form C#



bibhukalyana
9th October 2013, 11:19
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.

wysota
9th October 2013, 11:32
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).

bibhukalyana
9th October 2013, 11:45
Thanks for reply.

I tried instance = QCoreApplication::instance();

But I am getting a null pointer.

wysota
9th October 2013, 14:25
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().

bibhukalyana
9th October 2013, 16:17
Can you please tell me how to create the instance ?

wysota
9th October 2013, 16:18
QCoreApplication app(argc, argv);

bibhukalyana
10th October 2013, 07:29
Thanks wysota,
I am able to create the instance.But my next problem is QCoreApplication::exec() is a blocking function(Is it true).



QCoreApplication app(argc,argv);
instance = QCoreApplication::instance();
if(instance)
{
instance->exec();//bolcking function
}

wysota
10th October 2013, 08:26
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.

bibhukalyana
10th October 2013, 09:10
I tried QCoreApplication::processEvents() 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::processEvents()

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

My qt version is 4.7.

wysota
10th October 2013, 09:26
How is memberfun2 "continuously" calling processEvents?

bibhukalyana
10th October 2013, 10:31
It is inside a while loop.



do
{
status = export_memberfun2();
}while(status != DONE)



export_memberfun2() is the exported function of dll.

wysota
10th October 2013, 11:23
So how does that differ from calling exec()?

bibhukalyana
10th October 2013, 12:03
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.

wysota
10th October 2013, 12:13
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.

bibhukalyana
10th October 2013, 13:24
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.

wysota
10th October 2013, 13:27
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.

bibhukalyana
10th October 2013, 13:47
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.