Solved

thread error

Posted on 2004-04-23
5
716 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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