Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting rid of this error...

Getting rid of this error...

Scheduled Pinned Locked Moved C / C++ / MFC
helpregex
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pl_kode
    wrote on last edited by
    #1

    I am doin the following...

    class ThreadTest
    {
    public:
    ThreadTest();
    void end();
    DWORD WINAPI Write_data(LPVOID);
    DWORD ThreadID;
    void run();
    private:
    HANDLE hThread;
    bool keepRunning;
    };

    ThreadTest::ThreadTest()
    {
    keepRunning = true;
    }

    void ThreadTest::run()
    {
    hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Write_data,NULL,0,&ThreadID);
    }

    void ThreadTest::end()
    {
    keepRunning = false;
    }

    DWORD WINAPI ThreadTest::Write_data(LPVOID lpParam)
    {
    while(keepRunning)
    {
    printf("I'm a thread running");
    Sleep(1000);
    }
    return 0;
    }

    void main()
    {
    ThreadTest *t = new ThreadTest();
    t->run();
    Sleep(10000);
    t->end();
    }

    On compiling I get... error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Please help me out with this....THANKS

    C 2 Replies Last reply
    0
    • P pl_kode

      I am doin the following...

      class ThreadTest
      {
      public:
      ThreadTest();
      void end();
      DWORD WINAPI Write_data(LPVOID);
      DWORD ThreadID;
      void run();
      private:
      HANDLE hThread;
      bool keepRunning;
      };

      ThreadTest::ThreadTest()
      {
      keepRunning = true;
      }

      void ThreadTest::run()
      {
      hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Write_data,NULL,0,&ThreadID);
      }

      void ThreadTest::end()
      {
      keepRunning = false;
      }

      DWORD WINAPI ThreadTest::Write_data(LPVOID lpParam)
      {
      while(keepRunning)
      {
      printf("I'm a thread running");
      Sleep(1000);
      }
      return 0;
      }

      void main()
      {
      ThreadTest *t = new ThreadTest();
      t->run();
      Sleep(10000);
      t->end();
      }

      On compiling I get... error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Please help me out with this....THANKS

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The function you pass to the CreateThread function can't be a member function of a class (the prototype is different). So, either use a non-member function or a static function. In any case, you won't be able to access the non-static member variables. To solve this problem, pass the this pointer in the lpParameter parameter of CreateThread. It will be passed to your function and you'll be able to cast it back to your class pointer there and call a public method from there.

      Cédric Moonen Software developer
      Charting control [v1.4]

      1 Reply Last reply
      0
      • P pl_kode

        I am doin the following...

        class ThreadTest
        {
        public:
        ThreadTest();
        void end();
        DWORD WINAPI Write_data(LPVOID);
        DWORD ThreadID;
        void run();
        private:
        HANDLE hThread;
        bool keepRunning;
        };

        ThreadTest::ThreadTest()
        {
        keepRunning = true;
        }

        void ThreadTest::run()
        {
        hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Write_data,NULL,0,&ThreadID);
        }

        void ThreadTest::end()
        {
        keepRunning = false;
        }

        DWORD WINAPI ThreadTest::Write_data(LPVOID lpParam)
        {
        while(keepRunning)
        {
        printf("I'm a thread running");
        Sleep(1000);
        }
        return 0;
        }

        void main()
        {
        ThreadTest *t = new ThreadTest();
        t->run();
        Sleep(10000);
        t->end();
        }

        On compiling I get... error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Please help me out with this....THANKS

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        By the way, instead of using sleep, you could be much more efficient when you want to stop the thread if you use events (using CreateEvent function). When you want to stop your thread, you can signal the event. In your thread function, you can wait on the event instead of sleeping (and provide a timeout of 1000 msec) and check the return of the WaitForSingleObject to see if it is a timeout (in which case you continue your loop) or if the event has been signaled (in which case you exit your loop). This way, your thread will exit immediately and not wait until your Sleep finished.

        Cédric Moonen Software developer
        Charting control [v1.4]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups