Help - Search - Members - Calendar
Full Version: c# using skype4com in threads
Skype Community > English > Development, Betas and Skype Garage > Skype Public API
Dr.O
Hello,
I hope someone can help me soving following problem. I have written a short demo-project which demonstrates the "System.InvalidCastException" I get when I try to send a message in a thread. Hope sombody can help me to fix that error. Here is the code (windows-application, visual c# 2005 professional):
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SKYPE4COMLib;
using System.Threading;
using System.Collections;

namespace ChatThreadTest
{
    public partial class Form1 : Form
    {
        private Skype skype = new Skype();
        private Chat chat;
        private string strMessage;
        private Thread starter;
        private int threadCounter = 0;
        private Hashtable htbThread = new Hashtable();

        public Form1()
        {
            InitializeComponent();
            skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                if (!skype.Client.IsRunning)
                {
                    skype.Client.Start(false, true);
                }
            }
            catch (Exception)
            {
                // All Skype Logic uses TRY for safety
            }

            try
            {
                skype.Attach(7, false);
            }
            catch (Exception)
            {
                //MessageBox.Show("Konnte nicht verbinden", "Verbindungsproblem", MessageBoxButtons.RetryCancel);

            }
        }


        void skype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
        {


            if (pMessage.Status == TChatMessageStatus.cmsReceived || pMessage.Status==TChatMessageStatus.cmsSent)
            {
                chat = skype.get_Chat(pMessage.Chat.Name);
                strMessage = pMessage.Body.ToLower();
                
                switch (strMessage)
                {
                    case "testthread":
                    {
                        threadCounter++;
                        htbThread.Add(threadCounter,new Thread(new ParameterizedThreadStart(startThread)));
                        starter = (Thread) htbThread[threadCounter];
                        starter.Start(threadCounter);
                            break;
                    }
                }
                //chat.SendMessage(strCommand);
            }
        }


        private void startThread(object asdf)
        {
            
            while (true)
            {
                chat.SendMessage(((int)asdf).ToString());
                Thread.Sleep(5000);
            }
        }

    }
}






to get the exception start debugging and then write or receive in a chatroom: "testThread"

this is the exception detail:
CODE

System.InvalidCastException wurde nicht behandelt.
  Message="Das COM-Objekt des Typs \"SKYPE4COMLib.ChatClass\" kann nicht in den Schnittstellentyp \"SKYPE4COMLib.IChat\" umgewandelt werden. Dieser Vorgang konnte nicht durchgeführt werden, da der QueryInterface-Aufruf an die COM-Komponente für die Schnittstelle mit der IID \"{8C24106C-3368-4580-93E5-5ED4ACCDE306}\" aufgrund des folgenden Fehlers nicht durchgeführt werden konnte: Bibliothek nicht registriert. (Ausnahme von HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))."
  Source="Interop.SKYPE4COMLib"
  StackTrace:
       bei SKYPE4COMLib.ChatClass.SendMessage(String MessageText)
       bei SkypeChatBot.Form1.startThread(Object asdf) in C:\Dokumente und Einstellungen\Otis\Eigene Dateien\Visual Studio 2005\Projects\SkypeChatBot\SkypeChatBot\Form1.cs:Zeile 87.
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart(Object obj)


Someone knows what to do?
TheUberOverlord
QUOTE(Dr.O @ Thu May 24 2007, 15:01) [snapback]399029[/snapback]

Hello,
I hope someone can help me soving following problem. I have written a short demo-project which demonstrates the "System.InvalidCastException" I get when I try to send a message in a thread. Hope sombody can help me to fix that error. Here is the code (windows-application, visual c# 2005 professional):
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SKYPE4COMLib;
using System.Threading;
using System.Collections;

namespace ChatThreadTest
{
    public partial class Form1 : Form
    {
        private Skype skype = new Skype();
        private Chat chat;
        private string strMessage;
        private Thread starter;
        private int threadCounter = 0;
        private Hashtable htbThread = new Hashtable();

        public Form1()
        {
            InitializeComponent();
            skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                if (!skype.Client.IsRunning)
                {
                    skype.Client.Start(false, true);
                }
            }
            catch (Exception)
            {
                // All Skype Logic uses TRY for safety
            }

            try
            {
                skype.Attach(7, false);
            }
            catch (Exception)
            {
                //MessageBox.Show("Konnte nicht verbinden", "Verbindungsproblem", MessageBoxButtons.RetryCancel);

            }
        }
        void skype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
        {
            if (pMessage.Status == TChatMessageStatus.cmsReceived || pMessage.Status==TChatMessageStatus.cmsSent)
            {
                chat = skype.get_Chat(pMessage.Chat.Name);
                strMessage = pMessage.Body.ToLower();
                
                switch (strMessage)
                {
                    case "testthread":
                    {
                        threadCounter++;
                        htbThread.Add(threadCounter,new Thread(new ParameterizedThreadStart(startThread)));
                        starter = (Thread) htbThread[threadCounter];
                        starter.Start(threadCounter);
                            break;
                    }
                }
                //chat.SendMessage(strCommand);
            }
        }
        private void startThread(object asdf)
        {
            
            while (true)
            {
                chat.SendMessage(((int)asdf).ToString());
                Thread.Sleep(5000);
            }
        }

    }
}

to get the exception start debugging and then write or receive in a chatroom: "testThread"

this is the exception detail:
CODE

System.InvalidCastException wurde nicht behandelt.
  Message="Das COM-Objekt des Typs \"SKYPE4COMLib.ChatClass\" kann nicht in den Schnittstellentyp \"SKYPE4COMLib.IChat\" umgewandelt werden. Dieser Vorgang konnte nicht durchgeführt werden, da der QueryInterface-Aufruf an die COM-Komponente für die Schnittstelle mit der IID \"{8C24106C-3368-4580-93E5-5ED4ACCDE306}\" aufgrund des folgenden Fehlers nicht durchgeführt werden konnte: Bibliothek nicht registriert. (Ausnahme von HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))."
  Source="Interop.SKYPE4COMLib"
  StackTrace:
       bei SKYPE4COMLib.ChatClass.SendMessage(String MessageText)
       bei SkypeChatBot.Form1.startThread(Object asdf) in C:\Dokumente und Einstellungen\Otis\Eigene Dateien\Visual Studio 2005\Projects\SkypeChatBot\SkypeChatBot\Form1.cs:Zeile 87.
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart(Object obj)


Someone knows what to do?


What Threading Model are your using MTA or STA ;-)
Dr.O
I think its a multi-threading model, but I'm not 100% shure. In my demo project it should start a new thread everytime "threadstart" is written. These Thread Objects are stored in a Hashtable to manage it later. This was an idea of me because at runtime I don't how many threads will be started. Then instead of giving the object "asdf" an Integer number I would give the thread an object where I store the reference to the "Chat chat".

But I don't know what this "InvalidCastException" means. If I do chat.sendMessage in the Form1, then it works.
In the exception details is something written like "a library is not registered". But better you test the Program on your VisualStudio installation to have a non german exception detail.
Btw this damn Exception is also thrown when I do this:
I add a new Class to the Project (same Namespace) and make there a class and give it the Chat chat reference. When trying to send a Message from this class the same problem.
worried.png
Dr.O
speechless.png
Dr.O
Yesterday I have updated from Skype 3.2.0.145 to build 152...
No idea why but it works now
muscle.png
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.