Now with all the WaitOne and ManualResetEvent stuff working (thanks!) I’ve got one last problem, that is running a function in Class A from a thread which is part of Class B – again allow me to illustrate …
Look at the function “DoIt(obejct param)” within class A, this needs to be called by class A (as shown) as well as by the thread in class B (as shown) … how can this be accomplished? Would some form of delegates help?
class A
{
private ManualResetEvent manualResetEvent;
int counter = 0;
public A()
{
manualResetEvent = new ManualResetEvent(false);
B child = new B(manualResetEvent);
if (manualResetEvent.WaitOne(1000, false))
{
... do the work I was waiting on ...
}
... do more work ...
// Call the function DoIt from within A //
DoIt(param)
}
// This is the function that needs to be called from A and thread in B
void DoIt(object param)
{
counter++;
... do something with param with is local to A ...
}
};
Class B
{
private ManualResetEvent manualResetEvent;
public B(ManualResetEvent mre)
{
manualResetEvent = mre;
Thread childThread = new Thread(new ThreadStart(Manage));
childThread.IsBackground = true;
childThread.Name = "NamedPipe Manager";
childThread.Start();
private void Manage()
{
... do some work ...
... call some functions ...
// Calling the function from Class A, obviouslly doesn't work as-is
DoIt(param);
manualResetEvent.Set();
... do more work ...
... call more functions ...
}
}
};
Any one have any suggestions on how I can accomplish this task in a thread-safe manner?
Any help would be much appreciated.
Thanks,
Given your current design the simplest thing would be to pass in A to your object B, just like your doing with the manualresetevent:
Of course you need to mark A.DoIt as public.