QThread + QTimer + skype4Com throws exception
Hello, fellow programmers. I`m trying the next thing:
I`m importing SKypeAPI dll Skype4Com and i`m trying to make a call in a new thread, wait 5 secs and finish the call and close the thread. The program is approved as a plugin before placing the call while the program starts. The problem is:
when i click "CALL" and point the mouse pointer outside the GUI.. when the timer kicks finishing the call -> the program freezes and throws some exception... BUT if i make the call and point the mouse to the GUI and click on some tab or even point a tab... when the call finishes, thread exits without freezing the main program. Threads and events are new for me and i`m struggling... do you know what am i missing? Here is some code:
sms.cpp -> from here i call the new thread
Code:
void SMS::SkypeCallInNewThread()
{
// create new thread (on heap) and start it
SkypeThread = new MyThread(this);
SkypeThread->number = "echo123";
SkypeThread->start(); // after this, thread's run() method starts
connect(SkypeThread, SIGNAL(finished()), this, SLOT(SkypeThreadFinished()));
}
void SMS::SkypeThreadFinished()
{
qDebug() << "Skype thread finished";
SkypeThread->deleteLater();
}
myThread.h
Code:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QtGui>
#include <QTimer>
#import "Skype4COM.dll"
using namespace SKYPE4COMLib;
{
Q_OBJECT
public:
void run();
private slots:
void cancelCallfromSkype();
private:
ISkypePtr pSkype;
ICallPtr pCall;
};
#endif
mythread.cpp
Code:
#include "mythread.h"
MyThread
::MyThread(QObject *parent
){
}
void MyThread::run()
{
_bstr_t s = ba.data();
try{
CoInitialize(NULL);
pSkype.CreateInstance(__uuidof(SKYPE4COMLib::Skype));
pSkype->Attach(6,VARIANT_TRUE);
pCall = pSkype->PlaceCall(s, L"", L"", L"");
}
catch(...){
qDebug() << "exception....";
}
connect(exitTimer, SIGNAL(timeout()), this, SLOT(cancelCallfromSkype()));
exitTimer->setSingleShot(true);
exitTimer->start(5000);
exec();
}
void MyThread::cancelCallfromSkype()
{
try{
qDebug() << "Stopping the call";
pCall->Finish();
qDebug() << "Call stopped...";
}
catch(...){
qDebug() << "Exception....";
}
pCall = NULL;
pSkype = NULL;
CoUninitialize();
quit();
}
When executing pCall->Finish() and the mouse pointer is outside the GUI... main program freezes and throws an exception. When i place the call and point mouse to the GUI and click something... pCall->Finish() executes and thread is exiting. Any idea?
Re: QThread + QTimer + skype4Com throws exception
OK i think i fixed it with:
Code:
connect(exitTimer, SIGNAL(timeout()), this, SLOT(cancelCallfromSkype()), Qt::DirectConnection);
Making more tests... not sure but now it is not freezing. If someone got something about the code... pls write... i`m not sure if it is the correct way working with COM, threads, events and exceptions.