Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

Signal threads in created Assembly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
eusebiu - 03 Dec 2007 21:51 GMT
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
Prashant - 04 Dec 2007 09:16 GMT
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();
           }
       }
           
   }
}

> Hello....
> On the main thread of an application I create a number(20) of
[quoted text clipped - 55 lines]
> Can someone help me with this?
> Thanks
eusebiu - 04 Dec 2007 12:21 GMT
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).
eusebiu - 04 Dec 2007 18:58 GMT
I used shared memory...
The key is to have something that all thouse threads can read and the
main thread can write...
Prashant - 05 Dec 2007 12:08 GMT
Just curious, did you need to share the data between process or app domain

thanks

> I used shared memory...
> The key is to have something that all thouse threads can read and the
> main thread can write...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.