Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

thread error

Posted on 2004-04-23
5
Medium Priority
?
736 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

636 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