//这是socket模块里接收数据的回调,事件在这里引发
protected void ReceiveDataCallback(System.IAsyncResult ar)
{
    Socket sock=(Socket)ar.AsyncState;
    try
    {
int nRec=sock.EndReceive(ar);
if(nRec>0)
{
              string sReceived
=System.Text.Encoding.BigEndianUnicode.GetString(buff,0,nRec);
              //这里引发了数据接受的事件,主要就是这里
              if(this.ClientDataReceived!=null)
this.ClientDataReceived(this,
                      new ClientDataReceivedEventArgs(sReceived));
    this.SetupReceiveCallback(sock);
}
else
{
    if(this.ClientReceiveError!=null)
             this.ClientReceiveError(this,
                 new ClientReceiveErrorEventArgs("Connection Lost."));
             sock.Shutdown(SocketShutdown.Both);
    System.Threading.Thread.Sleep(10);
    sock.Close();
}
    }
    catch(Exception ex)
    {
if(this.ClientUnknowError!=null)
              this.ClientUnknowError(this,
                  new ClientUnknowErrorEventArgs(ex.Message));
    }
}接着是其他模块的:
这是接收到消息的事件处理程序
private void client_ClientDataReceived(ClientSocket   sender,ClientDataReceivedEventArgs e)
{
    decipher.BeginDecipher(temp);//这是消息解释类
    //这个类肯定没问题
}
//消息解释完了显示窗口
private void decipher_ChatterMessage(Eternal.Common.Decipher sender,Eternal.Common.Event.ChatterMessageEventArgs e)
{
    //这里的chatter时一个窗体,显示出来就死掉
    chatter=new Chatter(e.Message,ref this.client);
    chatter.Show();
}
救命阿,这是我的毕业设计

解决方案 »

  1.   

    Chatter是你自己定义的窗口类是吗?检查一下,Chatter的new方法是否有问题?
    把chatter=new Chatter(e.Message,ref this.client)换成显示一个空的FORM应该没问题吧?
    问题应该出在Chatter的new方法上了.
      

  2.   

    对的,chatter是自定义的窗口类
    问题是,这个窗口类在button的click事件中可以new,并且show
    一点问题都没有
    在datareceive里就不行了。
    因此我认为是我写的socket类和button类之间有一些不一样,是指button类的事件消息是正确的(废话)。但我这里的socket的事件消息肯定存在问题。这里是采用异步,后来我全部改成同步+多线程,问题依旧
    谢谢诸位大哥帮忙。要不我的毕业设计要完蛋了
      

  3.   

    COM控件要在 单线程单元中启动,在你程序的Main前面加上这个[STAThread]
      

  4.   

    用vsnet开发的啦
    main之前有[STAThread]