How do I make a custom function that returns output and blocks until output is available? I’m thinking of something like Console.ReadLine(). Something like this:
var resp = Output(); //blocks until output is sent.
...
//returns a string once SendOutput is called and hands over the string.
public static string Output() { /* what goes here? */ }
//Is this function even needed? Can I just fire Output somehow?
private static string SendOutput(string msg) { /* what goes here? */ }
...
//Calls sendoutput with the string to send.
SendOutput(msg);
Basically I’m making a listener that is blocked until it gets data (like it would if calling console.readline), and I need the internal code to make the blocker.
What you want is for your blocking method call to be signalled when some other work has completed. A ManualResetEvent is a good way to achieve this behaviour; there are no loops, and the return is virtually instantaneous once the worker thread signals that it is complete.