Click here to Skip to main content
16,018,057 members
Home / Discussions / C#
   

C#

 
AnswerRe: Problem with response.binarywrite() Pin
Guffa28-Nov-06 3:03
Guffa28-Nov-06 3:03 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan28-Nov-06 17:16
Anish Gangadharan28-Nov-06 17:16 
AnswerRe: Problem with response.binarywrite() Pin
Guffa28-Nov-06 20:28
Guffa28-Nov-06 20:28 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan28-Nov-06 22:24
Anish Gangadharan28-Nov-06 22:24 
AnswerRe: Problem with response.binarywrite() Pin
Guffa28-Nov-06 22:33
Guffa28-Nov-06 22:33 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan28-Nov-06 23:20
Anish Gangadharan28-Nov-06 23:20 
AnswerRe: Problem with response.binarywrite() Pin
Guffa29-Nov-06 1:24
Guffa29-Nov-06 1:24 
QuestionHow do i fire a event from the AsycnCallBack right ? [modified] Pin
MarkPhB27-Nov-06 0:02
MarkPhB27-Nov-06 0:02 
Hi im writing a classes for a TcpConnection that works with the Socket-Mehtods.

In my class i wanna fire an event when i got Bytes and i do that with the BeginReceive- and EndReceive-Methods.

But when i fire an event from the AsyncCallBack-Methode that i have to set with the Socket.BeginReceive-Methode, then i got and InvalidOperationException when i handle
the event in an Form

Here the class example



<code>
   public class TcpConnection {

        public delegate void ReceiveDataEventHandler( byte[] e );
        public event ReceiveDataEventHandler ReceiveData;
        public Socket m_connectionSocket;
        public byte[] m_receiveBuffer;


        public TcpConnection() { 
            m_connectionSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        }


        public void startReceiving() {
            m_receiveBuffer = new byte[ m_connectionSocket.ReceiveBufferSize ];
            m_connectionSocket.BeginReceive( m_receiveBuffer, 0, m_receiveBuffer.Length,
                    SocketFlags.None, new AsyncCallback( receiveDataBytes ), m_connectionSocket );
        }


        private void receiveDataBytes( IAsyncResult asyncResult ) {

            Socket thisSocket = ( Socket ) asyncResult.AsyncState;

            int dataBufferLength = thisSocket.EndReceive( asyncResult );

            byte[] dataBuffer = new byte[ dataBufferLength ];

            Array.Copy( m_receiveBuffer, dataBuffer, dataBufferLength );

            if( ReceiveData != null ) {
                ReceiveData( dataBuffer );
            }

            thisSocket.BeginReceive( m_receiveBuffer, 0, m_receiveBuffer.Length, SocketFlags.None,
                     new AsyncCallback( receiveDataBytes ), thisSocket );

        }
    }  
</code>

and here the handling of the event in a form
<code>
    public partial class Form1 : Form {

        private Socket localSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );

        public Form1() {

            InitializeComponent();

            Socket newConnection = localSocket.Accept();

            TcpConnection myConnection = new TcpConnection( newConnection );
            myConnection.ReceiveData += new TcpConnection.ReceiveDataEventHandler( receiveDataEvent );

            myConnection.startReceiving();
        }
        
        private void receiveDataEvent( byte[] data ) {

            this.Text = System.Text.Encoding.ASCII.GetString( data ); // HERE I GOT THE INVALIDOPERATIONEXCEPTION
        }
    }
</code>

My question is now.....how do i fire the event right, so that i can access the form-elements normaly like in the other eventhandler-methods


-- modified at 11:01 Monday 27th November, 2006
AnswerRe: How do i fire a event from the AsycnCallBack right ? Pin
Stefan Troschuetz27-Nov-06 1:34
Stefan Troschuetz27-Nov-06 1:34 
GeneralRe: How do i fire a event from the AsycnCallBack right ? Pin
MarkPhB27-Nov-06 5:19
MarkPhB27-Nov-06 5:19 
GeneralRe: How do i fire a event from the AsycnCallBack right ? Pin
Stefan Troschuetz27-Nov-06 6:21
Stefan Troschuetz27-Nov-06 6:21 
QuestionDatagridview Pin
Skajus26-Nov-06 23:58
Skajus26-Nov-06 23:58 
QuestionCrystalReport 11.5 with VisualStudio.net 2005 Pin
bahaa_sa526-Nov-06 23:50
bahaa_sa526-Nov-06 23:50 
AnswerRe: CrystalReport 11.5 with VisualStudio.net 2005 Pin
eng_mohamed_hammad27-Nov-06 3:27
eng_mohamed_hammad27-Nov-06 3:27 
QuestionSend Html file to printer Pin
Support12326-Nov-06 23:48
Support12326-Nov-06 23:48 
Questionhow to show Records in a DataGrid ?? Pin
Ahsanzm26-Nov-06 23:20
Ahsanzm26-Nov-06 23:20 
AnswerRe: how to show Records in a DataGrid ?? Pin
Bassam Saoud27-Nov-06 2:24
Bassam Saoud27-Nov-06 2:24 
QuestionDatGridView ... Pin
IamHuM26-Nov-06 23:10
IamHuM26-Nov-06 23:10 
AnswerRe: DatGridView ... Pin
Drew McGhie27-Nov-06 5:16
Drew McGhie27-Nov-06 5:16 
GeneralRe: DatGridView ... Pin
IamHuM27-Nov-06 15:57
IamHuM27-Nov-06 15:57 
GeneralRe: DatGridView ... Pin
Drew McGhie29-Nov-06 10:21
Drew McGhie29-Nov-06 10:21 
QuestionMessage box Pin
Shriya Kapoor26-Nov-06 22:59
Shriya Kapoor26-Nov-06 22:59 
AnswerRe: Message box Pin
sTTorM26-Nov-06 23:20
sTTorM26-Nov-06 23:20 
GeneralRe: Message box Pin
Bhupi Bhai26-Nov-06 23:26
Bhupi Bhai26-Nov-06 23:26 
GeneralRe: Message box Pin
Shriya Kapoor26-Nov-06 23:47
Shriya Kapoor26-Nov-06 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.