Solved

Thread Problem... AGIAN!

Posted on 2004-04-23
1
215 Views
Last Modified: 2010-04-01
#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 __cdecl testthread(void * dummy)
     {
          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)...
==========================================================================

I created a thread using _beginthread , but the problem is that I want the function testthread to be able to modify
the components in my windows form, (such as listbox or combobox)
But by creating that testthread function outside the class i am not able to access the component of Form1.. (very obvious :P)
I tried creating the thread inside the Form1(VOID) function, but it would never compile..
I think there is someway to create a thread inside the Form1 class so that the function is going to be able to update on the windows form component while the windows form can take user inputs.

Can anyone please help out on this?
thanks
0
Comment
Question by:lordiano
1 Comment
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 10906291
_beginthread is C++ Run-Time Library function, and you are trying to apply it from managed application. You need to use managed Thread class instead of unmanaded _beginthread function.
Running managed thread is relatively simple, but it has one restriction: it's impossible to update windows form directly from the worker thread. To do this you need to use Control::BeginInvoke or Control::Invoke functions. The following article describes this:

http://www.codeproject.com/csharp/workerthread.asp

I hope you don't have problems translating of C# code to managed C++. Slightly different syntax:

Thread m_WorkerThread;     // C#
Thread* m_WorkerThread;   // C++
...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

863 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

22 Experts available now in Live!

Get 1:1 Help Now