Solved

ThreadPool -  Need to pass an array of Function names and 'fire' them off.

Posted on 2004-09-23
8
324 Views
Last Modified: 2010-04-15
I have an array with a number of functions names that I need threaded, when a forms loads

LoadLookupData()
LoadWebData()
LoadPCData()

How do I pass these names to a threadpool and fire off the functions?
How do you pass in a string (the function name) and run it?

TIA
0
Comment
Question by:JElster
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12135307
Just use

ThreadPool.QueueUserWorkItem(new WaitCallback(LoadLookupData));

and do it for all the methods. You don't need to use reflection (converting the string to method) for that.
0
 
LVL 1

Expert Comment

by:KaitainHeavyIndustries
ID: 12135331
What you want is the InvokeMember method of a type, for example:

object result = yourObject.GetType().InvokeMember(FunctionName,BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, yourObjectip, args);
0
 
LVL 1

Author Comment

by:JElster
ID: 12135707
I want to loop through the array and call a single ThreadPool.QueueUserWorkItem and pass in the  function name and run it... not sure how I do that. .thanks
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12135986
Are the methods static, or instance members of a class?  Are they only in a specific class?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 50 total points
ID: 12136197
Actually, since you are loading a form, I will assume that the functions are instance members.  Also, I am assuming that they are parameterless (as your post shows).

Why don't you use something like this?

  System.Threading.Thread StartThreadFunction(string functionName)
  {
    System.Threading.Thread workThread;
    workThread = new System.Threading.Thread((System.Threading.ThreadStart)Delegate.CreateDelegate(typeof(System.Threading.ThreadStart), this, functionName));
    workThread.Start();
    return workThread;
  }

And then you can just call:

  StartThreadFunction(LoadLookupData);
  StartThreadFunction(LoadWebData);
  StartThreadFunction(LoadPCData);

or even do this:

  System.Threading.Thread lookupThread = StartThreadFunction(LoadLookupData);
  System.Threading.Thread webThread = StartThreadFunction(LoadWebData);
  System.Threading.Thread pcThread = StartThreadFunction(LoadPCData);

  lookupThread.Join();
  webThread.Join();
  pcThread.Join();

Which will start all three threads - run them concurrently, and then when all three have finished, continue execution on the main thread.

But if you just want to 'Fire and forget' a function with no parameters, use this:

  new System.Threading.Thread(new System.Threading.ThreadStart(LoadLookupData)).Start();
  // Note that LoadLookupData is 'hard-coded', not a variable string
0
 
LVL 1

Accepted Solution

by:
KaitainHeavyIndustries earned 55 total points
ID: 12136207
Do these functions all belong to the same class?  Are they static?  If not is the object instance you are calling these functions against contained in the class in which you are calling them?

Here is a complete example:

using System;
using System.Threading;
using System.Reflection;


namespace KHIExamples
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      public class Class1
      {
            
            [STAThread]
            static void Main(string[] args)
            {
                  Class1 c1 = new Class1();
                  Console.WriteLine("Done, look in the output window.");
                  Console.WriteLine("");
                  Console.WriteLine("Press Enter to end.");
                  Console.ReadLine();
            }

            public Class1()
            {

                  string[] functionNames = new string[2] {"Function1","Function2"};
                  this.runFunctions(functionNames);
            }

            public void runFunctions(string[] FunctionNames)
            {
                  foreach(string FunctionName in FunctionNames)
                  {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(executeFunction),FunctionName);
                  }
                  
            }


            public void executeFunction(Object FunctionName)
            {
                  Class2 c2 = new Class2();
                  
                  // You don't have any parameters in your example so you can just pass null
                  object[] args = null;

                  object result = c2.GetType().InvokeMember(FunctionName.ToString(),BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, c2, args);

            }
      }


      public class Class2
      {
            public bool Function1()
            {
                  System.Diagnostics.Debug.WriteLine("Function1 called.");
                  return true;
            }

            public void Function2()
            {
                  System.Diagnostics.Debug.WriteLine("Function2 called.");
                  return;
            }


      }
}
0
 
LVL 8

Assisted Solution

by:Razzie_
Razzie_ earned 20 total points
ID: 12136231
If you mean you have the name of the functions as strings you will need reflection (system.reflection)

Type myType =(typeof(MyClass));
MethodInfo mi = myType.GetMethod(LoadLookupData);
mi.Invoke(this, parameters);

and use that to invoke it in ThreadPool

0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12136301
KaitainHeavyIndustries, that's funny - we cross posted and asked the same two questions!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now