Solved

thread error

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

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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