Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-23
8
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 200 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 220 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 80 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 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