CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009

    Exclamation make another safety thread while asychronous operation running!!!

    hi, need help.
    i have made my client server application asynchronous, thats work well,
    when i have modified the code i got an error. the error say
    "InvalidOperationException". here is a half of my code:
    public void OnDataReceived(IAsyncResult asyn)
    SocketPacket socketData = (SocketPacket)asyn.AsyncState ;
    // Complete the BeginReceive() asynchronous call by EndReceive() method
    // which will return the number of characters written to the stream
    // by the client
    int iRx = socketData.m_currentSocket.EndReceive (asyn);
    char[] chars = new char[iRx + 1];
    // Extract the characters as a buffer
    System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
    int charLen = d.GetChars(socketData.dataBuffer,
    0, iRx, chars, 0);

    System.String szData = new System.String(chars);
    /// uncomment the code bellow toget INVALID_OPERATION_EXCEPTION:
    string msg = "" + socketData.m_clientNumber + ":";
    AppendToRichEditControl(msg + szData);

    // Send back the reply to the client
    string replyMsg = "Server Reply:" + szData.ToUpper();
    // Convert the reply to byte array
    byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg);

    Socket workerSocket = (Socket)socketData.m_currentSocket;

    // Continue the waiting for data on the Socket
    WaitForData(socketData.m_currentSocket, socketData.m_clientNumber );

    catch (ObjectDisposedException )
    System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
    catch(SocketException se)
    if(se.ErrorCode == 10054) // Error code for Connection reset by peer
    string msg = "Client " + socketData.m_clientNumber + " Disconnected" + "\n";

    // Remove the reference to the worker socket of the closed client
    // so that this object will get garbage collected
    m_workerSocketList[socketData.m_clientNumber - 1] = null;
    MessageBox.Show (se.Message );

    my question is haw i can make safety thread while asynchronous progress run.
    dont ask me to use BackgroundWorker component to run the Process.Start();
    please another way. thanks verymuch...........


    COMPILE USING "csc <thesourcecode>.cs" at cmd

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: make another safety thread while asychronous operation running!!!

    The problem is that you can't directly modify a UI component from a thread that didn't create the component.

    See Control.InvokeRequired in Msdn for some sample code.

  3. #3
    Join Date
    Jul 2009

    Smile Re: make another safety thread while asychronous operation running!!!

    hi arjay thanks for reply, i know that all windows form locate in System.Windows.Form is not safety thread. i have use invokerequired to update my richtextboxt for receiving message.my question is not to update UI. my question is how that the System.Diagnostics.Process.Start("cmd.exe"); work well, is there any Invoke function for Process class. we know that, all memer in Process class is not quarintee safe thread. please need help

    please look again my full code here:

    i have modified the server.cs only on OnDataReceived(IAsyncResult asyn) function with
    Process.Start(); method.



  4. #4
    Join Date
    Feb 2009
    Atlanta, GA

    Re: make another safety thread while asychronous operation running!!!

    If you declare an instance of a class inside of a thread then it is thread safe. It might not be thread safe if you call it from ANOTHER class but from what you said that doesn't appear to be the case. I'm at a loss for what the issue is. Could you please clarify?
    Scott Knake
    Custom Software Development
    Apex Software, Inc.

  5. #5
    Join Date
    Jul 2009

    Red face Re: make another safety thread while asychronous operation running!!!

    sorry for late replay....
    here is clarify for u..............................
    when we make an asynchronous thread, the tread is running in separate main thread(GUI thread).
    note that :
    the thread is safety if we access static member in any class
    (ex: Process.start()

    otherwise if we make new instance from any class while async operation running
    (ex: DirectoryInfo xxx=new DirectoryInfo("c:/")
    if we compile the source and any instance member we have made, the compiler is fine.
    but when compiler running the command like code above, it rise an error...

  6. #6
    Join Date
    May 2009
    Bengaluru, India

    Re: make another safety thread while asychronous operation running!!!

    since your are trying to create a new process( thread ) from another thread it might be giving this error as it might not be supported in .NET frame work, unlike in MFC ...

    try creating a instance of the ProcesS class in this thread and then use the static member - Start ...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.