Help, cannot populate a list box when calling a thread function using AfxBeginThread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phenrol
    New Member
    • May 2007
    • 8

    Help, cannot populate a list box when calling a thread function using AfxBeginThread

    Hello,

    I am having issues getting my worker thread to function properly. I am working with visual c++ 6 and I am unsure that this is the correct forum to post in but I could not find another that seemed to better suit my question.

    I am trying to initiate a thread to populate a list box when I click on a button. Once I figure out I will be populating the list box with more complex code, but if I can't even get this to work, I can't move forward on the next part. I am trying to make the following code work but it is obviously not correct:

    void CDialogDlg::OnB utton1()
    {
    AfxBeginThread( CDialogDlg::MyT hreadFunction,( LPVOID)this);
    }

    UINT CDialogDlg::MyT hreadFunction(L PVOID lparam)
    {
    m_listbox.AddSt ring("testfromf unction");
    return 0;
    }

    This code produces the following error:
    'AfxBeginThread ':none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'

    If I change it to the following code, AfxBeginThread works just fine, but I cannot populate the listbox because the MyThreadFunctio n does not know anything about the m_listbox variable, let alone the AddString member function:

    void CDialogDlg::OnB utton1()
    {
    AfxBeginThread( MyThreadFunctio n,(LPVOID)this) ;
    }

    UINT MyThreadFunctio n(LPVOID lparam)
    {
    AfxMessageBox(" testfromfunctio n");
    return 0;
    }

    Any help would be greatly appreciated,

    Thanks
  • phenrol
    New Member
    • May 2007
    • 8

    #2
    I got it to work! I had to pass a pointer to the m_listbox variable I created in the AfxBeginThread function and then call it from a static function. Here is how I did it:

    UINT MyThreadFunctio n(LPVOID lparam)
    {
    ((CListBox*)lpa ram)->AddString("tes tfromfunction") ;
    return 0;
    }

    void CDialogDlg::OnB utton1()
    {
    AfxBeginThread( MyThreadFunctio n,&m_listbox);
    }



    Originally posted by phenrol
    Hello,

    I am having issues getting my worker thread to function properly. I am working with visual c++ 6 and I am unsure that this is the correct forum to post in but I could not find another that seemed to better suit my question.

    I am trying to initiate a thread to populate a list box when I click on a button. Once I figure out I will be populating the list box with more complex code, but if I can't even get this to work, I can't move forward on the next part. I am trying to make the following code work but it is obviously not correct:

    void CDialogDlg::OnB utton1()
    {
    AfxBeginThread( CDialogDlg::MyT hreadFunction,( LPVOID)this);
    }

    UINT CDialogDlg::MyT hreadFunction(L PVOID lparam)
    {
    m_listbox.AddSt ring("testfromf unction");
    return 0;
    }

    This code produces the following error:
    'AfxBeginThread ':none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'

    If I change it to the following code, AfxBeginThread works just fine, but I cannot populate the listbox because the MyThreadFunctio n does not know anything about the m_listbox variable, let alone the AddString member function:

    void CDialogDlg::OnB utton1()
    {
    AfxBeginThread( MyThreadFunctio n,(LPVOID)this) ;
    }

    UINT MyThreadFunctio n(LPVOID lparam)
    {
    AfxMessageBox(" testfromfunctio n");
    return 0;
    }

    Any help would be greatly appreciated,

    Thanks

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Great to here. Keep on coding! :)


      Adrian

      Comment

      Working...