Solved

thread error

Posted on 2004-04-23
5
706 Views
Last Modified: 2007-12-19
#pragma once

namespace example
{
      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;
      mytest * obj1;
      static void testthread()
      {
            obj1 = new mytest();
      }
      public __gc class Form1 : public System::Windows::Forms::Form
      {      
      public:

            Form1(void)
            {
                  InitializeComponent();
                  _beginthread(testthread, 0, NULL);
            }

      protected:
            void Dispose(Boolean disposing)
            {
                  if (disposing && components)
                  {
                        components->Dispose();
                  }
                  __super::Dispose(disposing);
            }
      private: System::Windows::Forms::ListBox *  listBox1;
      private: System::Windows::Forms::ListBox *  listBox2;

      private:
            System::ComponentModel::Container * components;
            void InitializeComponent(void)...
==========================================================================
This is the error i am getting eventhough I defined the function testthread as static.

error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void *)'

THanks for any input.
      
0
Comment
Question by:lordiano
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 10902778
>>error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void *)'

use

    static void __cdecl testthread(LPVOID)
    {
         obj1 = new mytest();
    }

0
 

Author Comment

by:lordiano
ID: 10905233
Hi jkr, thanks for input
it doesnt seem to work,
if i use
static void __cdecl testthread(LPVOID)
it seems that the compiler does not like LPVOID says that the identifier is unknown or something, then i removed the LPVOID and i would still get the same error...
0
 

Author Comment

by:lordiano
ID: 10905267
Actually I changed LPVOID to void* dummy and now it works
But What can I do if i want to make my testthread(void * dummy) be able to modify my windows form components?
Say i want it to be able to add stuffs in Form1->listBox1
I cannot seem to create that static void __cdecl testthread inside the Form1 class.. or at least i dont know how
0
 
LVL 86

Expert Comment

by:jkr
ID: 10908523
>>But What can I do if i want to make my testthread(void * dummy) be able to modify my windows
>>form components?

You'd pass the necessary information via that 'void*'.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 75 total points
ID: 10913546
>> But What can I do if i want to make my testthread(void * dummy) be able to modify my windows form components?

Change the call of _beginthread in Form1 constructor to this

        Form1(void)
          {
               InitializeComponent();
               _beginthread(testthread, 0, this);
          }

And in thread function do this

    static void __cdecl testthread(void* pParam)
    {
        static char* pMsg = "Thread started successfully";
         Form1* pForm = (Form1*)pForm;
         // do something with pForm, e. g. send a message
         pForm->PostMessage(WM_APP + 1, 1, (LPARAM) pMsg );
 
         obj1 = new mytest();
    }

If you call a member function from thread be aware that calling is asynchronous in the thread's context. i. e. you may not change member variables while not using synchronisation objects like CriticalSection or Mutex.

Regards, Alex
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now