0

I have a windows forms application that at startup starts up a new thread with a socket connection that listens for clients.

When this socket receives a value from the client, this value should be used on the main parent thread, while the child thread keeps running the socket.

This is because:

  • When the socket receives the value, it calls an eventhandler and a long chain of methods after that. These methods adds the received value to existing data structures and changes the UI.
  • The object that I need to add this value to seems to be null within the child thread. So I guess I instead need to pass the value back to the parent thread - without stopping the child thread.
  • Just returning the value form the thread don't seem to be a solution because this would stop the socket from running.

How can I pass a value from the child thread to the parent thread without stopping the child thread?

nbjsys
  • 1
  • 1
  • "The object that I need to add this value to seems to be null within the child thread. So I guess I instead need to pass the value back to the parent thread" I don't quite understand this scenario. Why exactly should an object that is created on the main thread be suddenly `null` in a second thread? This sounds like the problem is somewhere else – Mong Zhu Oct 25 '19 at 13:45
  • "How can I pass a value from the child thread to the parent thread without stopping the child thread?" you could use an event. – Mong Zhu Oct 25 '19 at 13:46
  • Okay. So different threads do have access to the same instances of objects? Thanks – nbjsys Oct 25 '19 at 13:53
  • "So different threads do have access to the same instances of objects? " yes they do, this is why you can get a [Cross-thread operation not valid Exception](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – Mong Zhu Oct 25 '19 at 13:57
  • We figured out the hard way that we need to throw a Cross Thread Exception, to avoid people skimping on Invokes: https://stackoverflow.com/a/14703806/3346583 | The fact that the thread stopped without you knowing why, indicates that your Exception handling does not expose the Exceptions of the Thread properly. This is a serious mistake. I have to Artikles on that matter that I link often: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET – Christopher Oct 25 '19 at 14:05
  • "value to seems to be null within the child thread" how did you test it to arive at this conclusion? or is this merely an assumption? – Mong Zhu Oct 25 '19 at 14:08
  • Thanks Mong Zhu for leading me in the right direction. I found the problem somewhere else in the code. – nbjsys Oct 31 '19 at 12:50

2 Answers2

0

Assign the value in static in child thread and fetch it from main thread

Somnath Ghosh
  • 138
  • 1
  • 5
0

The way I've done this in the past is to pass the variable you want to be changed to the child thread on startup, along with an AutoResetEvent that can be used to signal the parent that the child has a new value.

public class CommunicationsBlock {
   public AutoResetEvent ValueReady {get; set;}
   public object NewValue {get; set;}
}

(in parent)

var cb = new CommunicationsBlock {
   ValueReady = new AutoResetEvent(false)
};

var child = new Thread(ChildThread);
child.Start(cb);
while (true)
{
   if (cb.ValueReady.WaitOne(TimeSpan.FromMilliseconds(10)))
   {
      // We have a new value
   }
}

(child thread)
private static void ChildThread(object state)
{
   var cb = (CommunicationsBlock) state;
   while (true)
   {
      // some stuff
      if (readytosend)
      {
         cb.NewValue = new object();
         cb.ValueReady.Set();
      }
   }
}
rpeh
  • 17
  • 4