Signal threads in created Assembly

  • Thread starter Thread starter eusebiu
  • Start date Start date
E

eusebiu

Hello....
On the main thread of an application I create a number(20) of
BackgroundWorkers that have on their DoWork event a method that
creates an Assembly using this class :
public class InvokeDynamicMethod
{
public void InvokeMethod(string assemblyName, string
className, string methodName, out object result, params object[]
arguments)
{
Assembly assembly = Assembly.LoadFrom(assemblyName);

// Walk through each type in the assembly
foreach (Type type in assembly.GetTypes())
{
// Pick up a class
if (type.IsClass == true && type.Name == className)
{
// create an instance of the object
object ibaseObject =
Activator.CreateInstance(type);

// Dynamically Invoke the
Method
result = type.InvokeMember(methodName,
BindingFlags.Default | BindingFlags.InvokeMethod,
null, ibaseObject,
arguments);

return;
}
}

result = null;
}
}
The method that takes care of the DoWork event is :
void Form1_DoWork(object sender, DoWorkEventArgs e)
{
object result;
InvokeDynamicMethod met = new InvokeDynamicMethod();

met.InvokeMethod(Assembly.GetAssembly(this.GetType()).Location,
"Form1", "DoMyWork", out result,
3, sender);
}
and DoMyWork is a method inside the Form1 class.
public void DoMyWork(params object[] pars)
{
//do some work here...in the new generated assembly
}

The problem is that I want to block the calling thread in my
dynamically generated method(the BackgroundWorker that creates the
assembly) and from the main thread(the application thread) to signal
all my BackgroundWorkers to continue their work.

Can someone help me with this?
Thanks
 
Not sure if this is what you are looking for but below is the code snippet
which shows how to wait and signal threads
/****************************/
using System;
using System.Threading;

namespace TestHarness
{
class Program
{
//create an auto reset wait event handle
public AutoResetEvent _workerthreadEvent = new AutoResetEvent(false);
public AutoResetEvent _mainthreadEvent = new AutoResetEvent(false);
private bool _isRunning = true;

public bool Running
{ get { return _isRunning; } set { _isRunning = value; } }

static void Main(string[] args)
{
Program program = new Program();
//create a background thread which will be blocked on _restevent
Thread backgroundThread = new Thread(new
ThreadStart(program.DoSomeWork));
//start the thread
backgroundThread.Start();
//main thread sleeps for 5 seconds
Thread.Sleep(5000);
//signal the worker thread
program.SignalWorkerThread();
Console.WriteLine("Main Thread has signaled the worked thread");
//wait on signal from work thread
program.WaitForSignalFromWorkThread();
//set running to false to terminate the worker thread
program.Running = false;
program.SignalWorkerThread();
}

public void SignalWorkerThread()
{
_workerthreadEvent.Set();
}

public void WaitForSignalFromWorkThread()
{
_mainthreadEvent.WaitOne();
}

public void DoSomeWork()
{
while (_isRunning)
{
//wait on event handle to be signaled
_workerthreadEvent.WaitOne();
Console.WriteLine("Worker thread Signaled");
_mainthreadEvent.Set();
}
}

}
}

eusebiu said:
Hello....
On the main thread of an application I create a number(20) of
BackgroundWorkers that have on their DoWork event a method that
creates an Assembly using this class :
public class InvokeDynamicMethod
{
public void InvokeMethod(string assemblyName, string
className, string methodName, out object result, params object[]
arguments)
{
Assembly assembly = Assembly.LoadFrom(assemblyName);

// Walk through each type in the assembly
foreach (Type type in assembly.GetTypes())
{
// Pick up a class
if (type.IsClass == true && type.Name == className)
{
// create an instance of the object
object ibaseObject =
Activator.CreateInstance(type);

// Dynamically Invoke the
Method
result = type.InvokeMember(methodName,
BindingFlags.Default | BindingFlags.InvokeMethod,
null, ibaseObject,
arguments);

return;
}
}

result = null;
}
}
The method that takes care of the DoWork event is :
void Form1_DoWork(object sender, DoWorkEventArgs e)
{
object result;
InvokeDynamicMethod met = new InvokeDynamicMethod();

met.InvokeMethod(Assembly.GetAssembly(this.GetType()).Location,
"Form1", "DoMyWork", out result,
3, sender);
}
and DoMyWork is a method inside the Form1 class.
public void DoMyWork(params object[] pars)
{
//do some work here...in the new generated assembly
}

The problem is that I want to block the calling thread in my
dynamically generated method(the BackgroundWorker that creates the
assembly) and from the main thread(the application thread) to signal
all my BackgroundWorkers to continue their work.

Can someone help me with this?
Thanks
 
Not quite...
The problem is that the a worker thread becomes the main thread into
the assembly that he creates.
I want to stop him inside the method that he invokes(the method is
inside the newlly created assembly) and signal him from outside the
assembly to continue his work(from the main thread that started the
application).
 
I used shared memory...
The key is to have something that all thouse threads can read and the
main thread can write...
 
Back
Top