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

Qt Code:
  1. void SMS::SkypeCallInNewThread()
  2. {
  3. // create new thread (on heap) and start it
  4. SkypeThread = new MyThread(this);
  5. SkypeThread->number = "echo123";
  6. SkypeThread->start(); // after this, thread's run() method starts
  7. connect(SkypeThread, SIGNAL(finished()), this, SLOT(SkypeThreadFinished()));
  8.  
  9. }
  10.  
  11. void SMS::SkypeThreadFinished()
  12. {
  13. qDebug() << "Skype thread finished";
  14. SkypeThread->deleteLater();
  15. }
To copy to clipboard, switch view to plain text mode 

myThread.h

Qt Code:
  1. #ifndef MYTHREAD_H
  2. #define MYTHREAD_H
  3.  
  4. #include <QtGui>
  5. #include <QTimer>
  6. #import "Skype4COM.dll"
  7.  
  8. using namespace SKYPE4COMLib;
  9.  
  10. class MyThread : public QThread
  11. {
  12. Q_OBJECT
  13.  
  14. public:
  15. MyThread(QObject *parent);
  16. void run();
  17. QString number;
  18.  
  19. private slots:
  20. void cancelCallfromSkype();
  21.  
  22. private:
  23. ISkypePtr pSkype;
  24. ICallPtr pCall;
  25.  
  26. };
  27.  
  28.  
  29. #endif
To copy to clipboard, switch view to plain text mode 


mythread.cpp

Qt Code:
  1. #include "mythread.h"
  2.  
  3. MyThread::MyThread(QObject *parent)
  4. : QThread(parent)
  5. {
  6. }
  7.  
  8. void MyThread::run()
  9. {
  10. QByteArray ba = number.toLatin1();
  11. _bstr_t s = ba.data();
  12. try{
  13. CoInitialize(NULL);
  14. pSkype.CreateInstance(__uuidof(SKYPE4COMLib::Skype));
  15. pSkype->Attach(6,VARIANT_TRUE);
  16. pCall = pSkype->PlaceCall(s, L"", L"", L"");
  17. }
  18. catch(...){
  19. qDebug() << "exception....";
  20. }
  21. QTimer *exitTimer = new QTimer();
  22. connect(exitTimer, SIGNAL(timeout()), this, SLOT(cancelCallfromSkype()));
  23. exitTimer->setSingleShot(true);
  24. exitTimer->start(5000);
  25. exec();
  26. }
  27.  
  28. void MyThread::cancelCallfromSkype()
  29. {
  30. try{
  31. qDebug() << "Stopping the call";
  32. pCall->Finish();
  33. qDebug() << "Call stopped...";
  34. }
  35. catch(...){
  36. qDebug() << "Exception....";
  37. }
  38. pCall = NULL;
  39. pSkype = NULL;
  40. CoUninitialize();
  41. quit();
  42. }
To copy to clipboard, switch view to plain text mode 

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?