My thread class not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: My thread class not working

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    11

    Question My thread class not working

    Error given :
    Error 1 error C3867: 'GenericThread::ThreadProc': function call missing argument list; use '&GenericThread::ThreadProc' to create a pointer to member ...\GenericThread\GenericThread.cpp 19
    IDE : MS Visual Studio 2005

    GenericThread.h
    Code:
    #pragma once
    #include <Windows.h>
    
    class GenericThread
    {
    	public:
    		GenericThread();
    		~GenericThread();
    		
    		// Create and run the thread
    		void Start(LPVOID lpParameter, int nPriority = 0, bool create_suspended = false, SIZE_T stack_size = 0, bool reserve_stack = false);
    		
    		// This member function will run when you call StartThread()
    		DWORD WINAPI ThreadProc(LPVOID lpParameter);
    		
    		// -15 : lowest; +15 : highest
    		void SetPriority(int nPriority);
    		
    		// Get currently assigned priority level
    		int GetPriority();
    		
    		// Get thread status
    		bool IsRunning();
    		
    		// Pause execution
    		void Suspend();
    		
    		// Resume execution
    		void Resume();
    		
    		// Terminate execution
    		void Terminate();
    		
    		// Return thread handle
    		HANDLE GetHandle();
    		
    	private:
    		// Handle to the thread
    		HANDLE m_hThread;
    		
    		// The thread id assigned by Windows, there is no use for now
    		LPDWORD m_lpdwThreadID;
    		
    		// You can use this property to signal the ThreadProc() to prepare for termination
    		bool m_bExiting;
    		
    		// Thread status
    		bool m_bRunning;
    };
    GenericThread.cpp
    Code:
    #include "GenericThread.h"
    
    GenericThread::GenericThread()
    {
    	m_bExiting = false;
    	m_bRunning = false;
    }
    
    GenericThread::~GenericThread()
    {
    	if (m_bRunning) CloseHandle(m_hThread);
    }
    
    void GenericThread::Start(LPVOID lpParameter, int nPriority /*= 0*/, bool create_suspended /*= false*/, SIZE_T stack_size /*= 0*/, bool reserve_stack /*= false*/)
    {
    	DWORD dwCreationFlags = 0;
    	if (create_suspended) dwCreationFlags |= CREATE_SUSPENDED;
    	if (reserve_stack) dwCreationFlags |= STACK_SIZE_PARAM_IS_A_RESERVATION;
    	m_hThread = CreateThread(NULL, stack_size, ThreadProc, lpParameter, dwCreationFlags, m_lpdwThreadID);	// ERROR GIVEN ON THIS LINE
    	SetPriority(nPriority);
    	m_bRunning = true;
    }
    
    DWORD WINAPI GenericThread::ThreadProc(LPVOID lpParameter)
    {
    	while (true) Sleep(60000);
    	return 0;
    }
    
    void GenericThread::SetPriority(int nPriority)
    {
    	SetThreadPriority(m_hThread, nPriority);
    }
    
    int GenericThread::GetPriority()
    {
    	return GetThreadPriority(m_hThread);
    }
    
    bool GenericThread::IsRunning()
    {
    	return m_bRunning;
    }
    
    void GenericThread::Suspend()
    {
    	SuspendThread(m_hThread);
    }
    
    void GenericThread::Resume()
    {
    	ResumeThread(m_hThread);
    }
    
    void GenericThread::Terminate()
    {
    	CloseHandle(m_hThread);
    	m_bRunning = false;
    }
    
    HANDLE GenericThread::GetHandle()
    {
    	return m_hThread;
    }

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: My thread class not working

    Code:
    #pragma once
    #include <Windows.h>
    
    class GenericThread
    {
    	public:
    		GenericThread();
    		~GenericThread();
    		
    		// Create and run the thread
    		void Start(LPVOID lpParameter, int nPriority = 0, bool create_suspended = false, SIZE_T stack_size = 0, bool reserve_stack = false);
    		
    		// This member function will run when you call StartThread()
                    static DWORD WINAPI ThreadProc(LPVOID lpParameter);
      //...

  3. #3
    Join Date
    Dec 2009
    Posts
    11

    Question Re: My thread class not working

    Quote Originally Posted by hoxsiew View Post
    Code:
    #pragma once
    #include <Windows.h>
    
    class GenericThread
    {
                    static DWORD WINAPI ThreadProc(LPVOID lpParameter);
    Yeah, it mysteriously works when I add the "static" keyword.
    But why, what is the logic behind this?

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: My thread class not working

    Without it, since your ThreadProc function is a member of the GenericThread class, it has an implicit "this" pointer as the first parameter. As such, it doesn't match the function argument for CreateThread.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center