CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Multithreaded consumer

    Hi. I have a C# WebService with a WebMethod that returns an array of (WebService-defined) objects. If I create a consumer exe and call the method, everything is as expected.
    If I then create a thread within my exe and call the WebMethod from there, my thread dies. No exception, it just dies. At one point I had a few unhandled socket exceptions mentioned in the Debug window, but something's changed and they're not appearing now.

    Also, I can't step into the web service code from the new thread, but I can from the normal thread...

    Here's the consumer code:
    Code:
           private void GetInfoButton_Click(object sender, EventArgs e)
           {
               if (CreateReference())
               {
                   NetQCws.DynamicHostInfo[] infos = m_WS.GetDynamicHostInfo();
    
                   foreach (NetQCws.DynamicHostInfo inf in infos)
                       Debug.WriteLine(string.Format("ST: {0}@{1} last seen {2}", inf.Hostname, inf.IP_Address, inf.LastSeen.ToString("T")));
               }
    
               // and now threaded
               System.Threading.Thread thr = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
               thr.Name = "info worker thread";
               thr.Start();
           }
    
           private void ThreadProc()
           {
               NetQCws.Service svc = new NetQCClient.NetQCws.Service();
    
               try
               {
                   NetQCws.DynamicHostInfo[] infos = svc.GetDynamicHostInfo();
                   foreach (NetQCws.DynamicHostInfo inf in infos)
                   {
                       Debug.WriteLine("blah");
                   }
               }
               catch (Exception ex)
               {
                   Debug.WriteLine(string.Format("ERROR: {0}", ex.Message));
               }
    
           }
    Any clues?

    Thanks for your time,
    Toot
    Some cause happiness wherever they go; others, whenever they go.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Multithreaded consumer

    For the normal function, you use m_WS. For the thread, you create a new webservice. Are you missing some initializing functions?

    Check if that line even succeeds (creating the service in the thread), maybe that line is failing.

  3. #3
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Re: Multithreaded consumer

    Quote Originally Posted by Tischnoetentoet
    For the normal function, you use m_WS. For the thread, you create a new webservice. Are you missing some initializing functions?

    Check if that line even succeeds (creating the service in the thread), maybe that line is failing.
    Nothing missing and after the new, there is definitely an object. Also tried using the member but no difference. It really does seem to be the fact that it's on a different thread; almost as if the proxy (or debugger???) is only allowing it to run on the GUI thread, for whatever reason...???
    Some cause happiness wherever they go; others, whenever they go.

  4. #4
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Multithreaded consumer

    I think I see what's wrong. The thread goes out of scope, so it is closed.

    Maybe I am thinking wrong, but try making the thread object a class object instead of a function object.

  5. #5
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Re: Multithreaded consumer

    Quote Originally Posted by Tischnoetentoet
    I think I see what's wrong. The thread goes out of scope, so it is closed.

    Maybe I am thinking wrong, but try making the thread object a class object instead of a function object.
    Tried that: no difference.... keep the ideas rolling!

    Cheers,
    T
    Some cause happiness wherever they go; others, whenever they go.

  6. #6
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Multithreaded consumer

    But the thread just quits?

  7. #7
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Re: Multithreaded consumer

    Yup - there's a bit of a debugger hang where nothing seems to happen for a while, then the thread's gone & I see a "The thread 0xnnnn has exited with code 0 (0x0)." in the output window (I know it's the same thread because I've put a "thread 0xnnnn starting" message at the top of the thread proc).
    Some cause happiness wherever they go; others, whenever they go.

  8. #8
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Multithreaded consumer

    Just for your information: you can give threads ID's so you can recognize them

    You stepped through the thread code using F10 (Step)? Which line in the ThreadProc makes the thread close?

  9. #9
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Re: Multithreaded consumer

    What attribute of Thread is that??? I know you can give them names, didn't realise you could assign an ID too.

    Anyhow, stepping over or into any WebMethod on my WebService blows the thread. So in the code above, the first line in the try block in the ThreadProc method:
    Code:
    NetQCws.DynamicHostInfo[] infos = svc.GetDynamicHostInfo();
    Cheers,
    T
    Some cause happiness wherever they go; others, whenever they go.

  10. #10
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Multithreaded consumer

    And if you don't call the first function (outside the thread), does it still quit?

    And about the thread ID, I was talking about the name property :S, my fault!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured