1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Visual C++ threading

Discussion in 'Software Development' started by FerrisWheel, Mar 31, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. FerrisWheel

    FerrisWheel Thread Starter

    Joined:
    Jan 13, 2010
    Messages:
    24
    Following the threading tutorials given on MSDN, I am unable to create a thread in visual c++. I have the code:
    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
     
  2. JimmySeal

    JimmySeal

    Joined:
    Sep 25, 2007
    Messages:
    290
  3. FerrisWheel

    FerrisWheel Thread Starter

    Joined:
    Jan 13, 2010
    Messages:
    24
    I tried the code:
    Code:
    System::Threading::ThreadStart^ threaddelegate = gcnew System::Threading::ThreadStart( NULL, &Form1::listen );
    But it gave me the errors:
    Code:
    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'
     
  4. JimmySeal

    JimmySeal

    Joined:
    Sep 25, 2007
    Messages:
    290
    Is listen() a static method?
    Is Form1 a class or an object?
     
  5. FerrisWheel

    FerrisWheel Thread Starter

    Joined:
    Jan 13, 2010
    Messages:
    24
    Form1 is a class, listen() is not a static method.
     
  6. JimmySeal

    JimmySeal

    Joined:
    Sep 25, 2007
    Messages:
    290
    If listen() is not static, you must pass an instance of Form1 as the first parameter.
     
  7. FerrisWheel

    FerrisWheel Thread Starter

    Joined:
    Jan 13, 2010
    Messages:
    24
    And how would I do this? I'm using managed Visual C++ code. The declaration of the class uses:
    Code:
    public ref class Form1 : public System::Windows::Forms::Form
    	{
     
  8. JimmySeal

    JimmySeal

    Joined:
    Sep 25, 2007
    Messages:
    290
    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.
     
  9. FerrisWheel

    FerrisWheel Thread Starter

    Joined:
    Jan 13, 2010
    Messages:
    24
    That worked just fine, thanks alot.
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Visual threading
  1. Bqwaszx
    Replies:
    1
    Views:
    418
  2. stevezal
    Replies:
    0
    Views:
    516
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/913949

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice