1

Hey i'm trying to multithread my program in c++/cli but i'm having problems with creating the threads the code im using is:

private: Void startThread() {
    MoveProj.Velocity = Variables.Velocity;
    MoveProj.ProjectilePos = Projectile1.ProjectilePos;
    Thread^ MotionThread1 = gcnew Thread(gcnew ParameterizedThreadStart(MoveProj, MotionThread::MoveProjectile));
    Thread^ MainThread = gcnew Thread(gcnew ThreadStart());
}

but i'm getting the errors

Error   44  error C3350: 'System::Threading::ParameterizedThreadStart' : a delegate constructor expects 2 argument(s)   c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h   344
Error   89  error C3350: 'System::Threading::ParameterizedThreadStart' : a delegate constructor expects 2 argument(s)   c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h   344
Error   45  error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s)    c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h   345
Error   90  error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s)    c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h   345
Error   43  error C3867: 'MotionThread::MoveProjectile': function call missing argument list; use '&MotionThread::MoveProjectile' to create a pointer to member c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h   344
Error   88  error C3867: 'MotionThread::MoveProjectile': function call missing argument list; use '&MotionThread::MoveProjectile' to create a pointer to member c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h   344

any help with this would be a great help as its for my college(British so senior year for americnas i think) computing project and my tutor wants it in relatively soon.

gjones
  • 367
  • 3
  • 13

1 Answers1

3

The error message is telling you what to do:

function call missing argument list; 
    use '&MotionThread::MoveProjectile' to create a pointer to member
         ^ 

Therefore, here's the correct syntax:

Thread^ MotionThread1 = gcnew Thread(
    gcnew ParameterizedThreadStart(MoveProj, &MotionThread::MoveProjectile));
                                             ^

For the other one, you're currently trying to create a delegate, without telling it what method the delegate should point to. Try something like this:

Thread^ MainThread = gcnew Thread(gcnew ThreadStart(this, &MyClass::MainMethod));
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^

Edit

I didn't read through your full code. If you expect people to take their time to help you, you need to take some time & spend the effort to distill it down to just what's needed.

I will, however, comment on the errors you're getting.

error C2440: 'initializing' : 
    cannot convert from 'MotionThread' to 'MotionThread ^'

You've got a variable somewhere that's a reference type, but you're using it without the ^. This is valid C++/CLI, but none of the managed APIs will work with that easily. Switch the member to a ^ and use gcnew.

error C3352: 'float Allformvariables::CalcCurrentVelocity(System::Object ^)' : 
    the specified function does not match the delegate type 'void (void)'

As the error message says: You're trying to construct a delegate that doesn't take any parameters and returns void, and the method you're passing doesn't match that. Either fix the method or switch to a different delegate type.

error C3754: delegate constructor: member function 'MotionThread::MoveProjectile' 
    cannot be called on an instance of type 'MotionThread'

I have a feeling this one will go away when you add the missing ^ that I mentioned above.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • well i've tried 'Thread^ MotionThread1 = gcnew Thread( gcnew ParameterizedThreadStart(MoveProj, &MotionThread::MoveProjectile));' but it throws up this error 'Error 87 error C3352: 'void MotionThread::MoveProjectile(System::Drawing::Point,System::Drawing::Point)' : the specified function does not match the delegate type 'void (System::Object )' c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 344' so i'm not sure what i'm doing wrong. and with the second part it has the same error as above when i try it like that. Thanks for help btw – gjones Apr 11 '13 at 18:20
  • okay i've now put all my code into a gist file https://gist.github.com/anonymous/5365228 i've had some suggestions from other forums which im going to try. thank you again for helping – gjones Apr 11 '13 at 18:29
  • https://gist.github.com/welsh4evr/5365988 codes changed :P and also getting a few different errors now https://gist.github.com/welsh4evr/5a61b32a2c499791a2c7 – gjones Apr 11 '13 at 18:48
  • Got this to work now, no longer multithreading now using a gameloop instead (well more of an event driven update cycle) – gjones Apr 14 '13 at 07:39