Solved

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

Posted on 2004-09-23
8
337 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Industry Leaders: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

628 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