Passing Methods
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Passing Methods

  1. #1
    Join Date
    Jun 2010
    Posts
    56

    Passing Methods

    I am completely lost on how to accomplish what I am doing but ill try to explain here:

    I have a class which I create 4 objects from.

    Code:
    CalcPrime[] calcPrimeObjects = new CalcPrime[NUMBER_OF_THREADS];
    
                for (int x = 0; x < NUMBER_OF_THREADS; x++)
                {
                    calcPrimeObjects[x] = new CalcPrime(STARTING_NUMBER, NUMBER_OF_NUMBERS_TO_CHECK, CONSOLE_OUTPUT);
                    calcPrimeObjects[x].DeclareStacks();
                }
    What happens here is that I create all four objects and then run a single method for each object that basically declare all initial values for it.

    From here I create a new object from a class that handles my threading.

    Code:
                ThreadDeclaration threadDeclare = new ThreadDeclaration(
                    NUMBER_OF_THREADS,
                    CONSOLE_OUTPUT,
                    calcPrimeObjects);
    As you see I am passing my array of objects along to the thread object.

    Within the thread class I have the following

    Code:
                oThread = new Thread[numThreads];            
                for (int x = 0; x < numThreads; x++)
                {
                    //oThread[x] = new Thread(objects[x]);
                }
    This is where I would assign the method to execute to the thread objects. Problem is that you dont have access to that method.

    What I am thinking is that I have to create a method that accepts a method and pass it along to start the threads. Or somehow pass along the method reference along with the objects earlier at the constructor.

    Sorry if I have the terminology wrong kind of new to this all.

  2. #2
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Passing Methods

    Hi 45 (may I be so bold as to call you '45' ?)

    There was some difficulty in understanding the details of your app, but I built what I thought might be a reasonable model of what you're doing, and it seems to be working successfully.

    I too, have a CalcPrimesObject ... mine has a max number of primes desired and a range (lowLimit, highLimit) of integers in which to find primes. My object also has a List<int> to hold the 'n' primes found.

    the code to create and initialize the CalcPrime[] is called by the 'button1_Click' event, and is as follows ...
    Code:
            // ***************************************
            private void beginTheBeguine() {
    
                // initialize the CalcPrime object array with four new objects
                CalcPrimeObjects[0] = new CalcPrime(1, 1000, 5);
                CalcPrimeObjects[1] = new CalcPrime(1001, 3000, 3);
                CalcPrimeObjects[2] = new CalcPrime(3001, 5000, 2);
                CalcPrimeObjects[3] = new CalcPrime(5001, 9000, 11);
    
                // Then ready the threading process ...
                threadStuff pThreading = new threadStuff(NUM_THREADS);
    
     
                // then light'em off ...
                pThreading.lightEmUp(CalcPrimeObjects);
    
            }// end function Form1.beginTheBeguine
            // ***************************************
    Note (above) that I have made the threading into a separate class as I think you said that you had done. I init the class, then call a class function "Light'emUp" which will create the threads and light'em off. I pass the CalcPrime[] to the function so that ultimately, each thread will have
    its CalcPrime object with which to work.



    Below is the code associated with threading. The important thing to keep in mind is that although it may not look like it, I am, in effect, using the ParameterizedThreadStart when creating the thread. Why does it not appear so, you ask ?

    Well 'cuz C# permits one to simply enter the method name (in this case, "threadProcessor") and the compiler fixes it up for ya. And the parameter I pass ('parameter', as in parameterizedThreadStart) is declared in the pThread.Start(...) statement.

    If you're hard-core old-school, I think one may still use the ParameterizedThreadStart explicitly, but I'm basically just too lazy for that.

    I think the argument MUST be passed as an object. Note that here I am passing each thread its own unique CalcPrime object.


    Code:
            // *******************************************************
            public void lightEmUp(CalcPrime[] arrayOfCalcObjects) {
                Thread pThread;
    
    
                for (int i = 0; i < threadCount; i++) {
                    threadArray[i] = (pThread = new Thread(threadProcessor));
                    pThread.Start((object)arrayOfCalcObjects[i]);
                }
            
            }// end function 'threadStuff.lightEmUp'
            // *******************************************************
    
    
    
    
            // *******************************************************
            private void threadProcessor(object calcPrimeObject) {
                CalcPrime calculationObject = (CalcPrime)calcPrimeObject;
                int[] result;
                string resultsString;
    
                result = calculationObject.return_N_PrimesWithinRange();
    
                resultsString = calculationObject.primeCount.ToString() + " " +
                                calculationObject.LowLimit.ToString() + " " +
                                calculationObject.HighLimit.ToString() + "\n" +
                                result.Length.ToString();
                MessageBox.Show(resultsString);
            
            }// end function 'threadStuff.threadProcessor'
            // *******************************************************
    Note above: the receiving end of the thread.Start() function is "Thread Processor", and it expects an object argument, here, named CalcPrimeObject. The argument is cast as the CalcPrime that it truly is.

    I then use that object to reference the class method "return_N_PrimesWithinRange" (here come the 'n' primes in the range "calculationObject.LowLimit" to "calculationObject.HighLimit").


    Of course there are many ways to achieve the same (or similar) result; this is just one of the many ways, and certainly not the best but it might work well enuf for ya. Seems ta be workin' OK here.
    Last edited by ThermoSight; December 23rd, 2010 at 12:41 AM.

  3. #3
    Join Date
    Jun 2010
    Posts
    56

    Re: Passing Methods

    I think I understand what you are doing. Ill see if I can apply this to my code.

    Here is an explanation behind the application.

    The application will calculate prime numbers based on a set range. So for example start at 2 and go to 1000. It will then take this range and based on how many threads you specified lets say 4, it will start 4 separate threads. The threads will then go through the range of 2 to 1000 and calculate if a number is prime or not. Now this CalcPrim class is where all the work is done and within this class is a locked method that accesses an array of all the numbers to be processed and basically flags them to show that they were already processed. Since its a locked method only one thread can check if the number it has will be processed or not, if it hasn't been processed it marks it as a 1 which means its to be processed. So when the next thread comes through and it has the same number it will already know its been processed and move on to the next one. So far from the logic I put in place this is working and no number is processed more then once.

    Now when all this is done the there is a static array that will contain the range 2-1000 but they will be either 1 or 0. We then take this array and start creating a BMP file that will map out the distribution of primes like this:

    http://upload.wikimedia.org/wikipedi..._primorial.png

    Now the problem is that when I built this I built it to work not to be clean and nice. I have a finished build of the program but the classes are interconnected and in reality everything should be in 1 class in its current state. What I am trying to do is break up the key components into separate classes because as I expanded the program to do a Ulam Spiral and inheritance broke completely.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Passing Methods

    Can i introduce you to your new best friend, Parallel.ForEach

    http://msdn.microsoft.com/en-us/library/dd460720.aspx
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center