make another safety thread while asychronous operation running!!!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

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

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    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 ;
    try
    {
    // 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:
    //if(szData=="<execute>")
    //{ProcesS.Start("cmd.exe");}
    //==============================================================
    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;
    workerSocket.Send(byData);

    // 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";
    AppendToRichEditControl(msg);

    // 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;
    UpdateClientListControl();
    }
    else
    {
    MessageBox.Show (se.Message );
    }
    }
    }

    //===================================================================
    my question is haw i can make safety thread while asynchronous progress run.
    (IF POSIBLE TO PAUSE THE ASYNC OPERATION!!!!)
    dont ask me to use BackgroundWorker component to run the Process.Start();
    please another way. thanks verymuch...........

    HERE IS MY CODE CLIENT-SERVER
    http://all-uneed.net/wp-content/uplo...ynchronous.rar

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

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

    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
    Posts
    3

    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:
    http://all-uneed.net/wp-content/uplo...ynchronous.rar

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

    thanks


    myg3nx

  4. #4
    Join Date
    Feb 2009
    Location
    Atlanta, GA
    Posts
    17

    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
    Posts
    3

    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
    Location
    Bengaluru, India
    Posts
    422

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center