# Visual C++ threading



## FerrisWheel (Jan 13, 2010)

Following the threading tutorials given on MSDN, I am unable to create a thread in visual c++. I have the code:

```
System::Threading::ThreadStart^ threaddelegate = gcnew System::Threading::ThreadStart( &Form1::listen );
System::Threading::Thread^ lThread = gcnew System::Threading::Thread( threaddelegate );
lThread->Start();
```
Gives me an error that ThreadStart requires two parameters, I look and find that in the examples, the tutorial uses an instance of the same class that contains the function. Trying to input a Form or the instance of Form1 gives an error. What do I place in the first parameter?
The tutorial


----------



## JimmySeal (Sep 25, 2007)

According to this page, older versions of the .NET framework don't support the one-parameter ThreadStart() and you should pass null as the first parameter when you don't need it.

http://msdn.microsoft.com/en-us/library/system.threading.threadstart.aspx


----------



## FerrisWheel (Jan 13, 2010)

I tried the code:

```
System::Threading::ThreadStart^ threaddelegate = gcnew System::Threading::ThreadStart( NULL, &Form1::listen );
```
But it gave me the errors:

```
error C2440: 'initializing' : cannot convert from 'int' to 'Client::Form1 ^'
error C3754: delegate constructor: member function 'Client::Form1::listen' cannot be called on an instance of type 'int'
```


----------



## JimmySeal (Sep 25, 2007)

Is listen() a static method?
Is Form1 a class or an object?


----------



## FerrisWheel (Jan 13, 2010)

Form1 is a class, listen() is not a static method.


----------



## JimmySeal (Sep 25, 2007)

If listen() is not static, you must pass an instance of Form1 as the first parameter.


----------



## FerrisWheel (Jan 13, 2010)

And how would I do this? I'm using managed Visual C++ code. The declaration of the class uses:

```
public ref class Form1 : public System::Windows::Forms::Form
	{
```


----------



## JimmySeal (Sep 25, 2007)

I've never used ThreadStart myself, but if you are invoking it from within an instance of Form1, I'd reckon you could just pass it the *this* pointer.


----------



## FerrisWheel (Jan 13, 2010)

That worked just fine, thanks alot.


----------

