PDA

View Full Version : QThread + QTimer + skype4Com throws exception



sadjoker
22nd December 2008, 14:13
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



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


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QtGui>
#include <QTimer>
#import "Skype4COM.dll"

using namespace SKYPE4COMLib;

class MyThread : public QThread
{
Q_OBJECT

public:
MyThread(QObject *parent);
void run();
QString number;

private slots:
void cancelCallfromSkype();

private:
ISkypePtr pSkype;
ICallPtr pCall;

};


#endif


mythread.cpp


#include "mythread.h"

MyThread::MyThread(QObject *parent)
: QThread(parent)
{
}

void MyThread::run()
{
QByteArray ba = number.toLatin1();
_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....";
}
QTimer *exitTimer = new QTimer();
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?

sadjoker
22nd December 2008, 14:24
OK i think i fixed it with:


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.