Hello all,
I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it (before I incorporate them in another program). I just can’t seem to get a non-static call to work in my thread that has access to the Form1 variables and controls I need. I can call a non-static function using another class but then I can seem to get in to link to Form1 without creating a new instance (I don't want that as then it will not really work).
Below is my code using VC++ Express Edition on win xp pro -
-
#pragma once
-
#include "Thread.h"//test header class not used
-
#include "stdafx.h"
-
#include <process.h>//multi-threading
-
#include <stdlib.h>
-
#include "windows.h"//windpws commands
-
-
//multi-threading
-
unsigned __stdcall MyThread1(void * param);
-
unsigned tid1; // thread IDs
-
HANDLE hMainThread; // thread handles
-
HINSTANCE hInst; // instance handle
-
HWND hwnd;
-
-
//namespace
-
using namespace System;
-
using namespace System::Windows;
-
using namespace System::ComponentModel;
-
using namespace System::Collections;
-
using namespace System::Windows::Forms;
-
using namespace System::Data;
-
using namespace System::Drawing;
-
#using <System.dll>
-
#using <System.Drawing.dll>
-
#using <System.Windows.Forms.dll>
-
-
namespace Multi_Thread_Test2 {
-
-
#define __stdcall
-
-
using namespace System;
-
using namespace System::ComponentModel;
-
using namespace System::Collections;
-
using namespace System::Windows::Forms;
-
using namespace System::Data;
-
using namespace System::Drawing;
-
-
public ref class Form1 : public System::Windows::Forms::Form
-
{
-
public:
-
Form1(void)
-
{
-
InitializeComponent();
-
//
-
//TODO: Add the constructor code here
-
//
-
}
-
public:
-
/// <summary>
-
/// Clean up any resources being used.
-
/// </summary>
-
~Form1()
-
{
-
if (components)
-
{
-
delete components;
-
}
-
}
-
public: System::Windows::Forms::Button^ Run;
-
public: System::Windows::Forms::ProgressBar^ ProgressBar;
-
-
public:
-
-
System::ComponentModel::Container ^components;
-
-
#pragma region Windows Form Designer generated code
-
/// <summary>
-
/// Required method for Designer support - do not modify
-
/// the contents of this method with the code editor.
-
/// </summary>
-
void InitializeComponent(void)
-
{
-
this->Run = (gcnew System::Windows::Forms::Button());
-
this->ProgressBar = (gcnew System::Windows::Forms::ProgressBar());
-
this->SuspendLayout();
-
//
-
// Run
-
//
-
this->Run->Location = System::Drawing::Point(202, 61);
-
this->Run->Name = L"Run";
-
this->Run->Size = System::Drawing::Size(75, 23);
-
this->Run->TabIndex = 0;
-
this->Run->Text = L"Run";
-
this->Run->UseVisualStyleBackColor = true;
-
this->Run->Click += gcnew System::EventHandler(this, &Form1::Run_Click);
-
//
-
// ProgressBar
-
//
-
this->ProgressBar->Location = System::Drawing::Point(13, 12);
-
this->ProgressBar->Name = L"ProgressBar";
-
this->ProgressBar->Size = System::Drawing::Size(450, 43);
-
this->ProgressBar->Step = 1;
-
this->ProgressBar->TabIndex = 1;
-
this->ProgressBar->Value = 1;
-
//
-
// Form1
-
//
-
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
-
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
-
this->ClientSize = System::Drawing::Size(475, 95);
-
this->Controls->Add(this->ProgressBar);
-
this->Controls->Add(this->Run);
-
this->Name = L"Form1";
-
this->Text = L"Form1";
-
this->ResumeLayout(false);
-
-
}
-
#pragma endregion
-
private: System::Void Run_Click(System::Object^ sender, System::EventArgs^ e)
-
{
-
StartThreadFunc();
-
}
-
public: void UpdateProgressBar()
-
{
-
int i;
-
for (i=0;i<100;i++)
-
{
-
System::Threading::Thread::Sleep(500);
-
this->ProgressBar->Value = i;
-
}
-
}
-
public: void StartThreadFunc()
-
{
-
void ( *funcPtr)( ) = &Multi_Thread_Test2::Form1::UpdateProgressBar;
-
hMainThread = (HANDLE) _beginthreadex(NULL, 0, funcPtr, (void *) hwnd, 0, &tid1);
-
}
-
-
};// end class Form1 definition
-
unsigned __stdcall MyThread1(void * param)//unused function
-
{
-
/*int i;
-
for (i=0;i<100;i++)
-
{
-
System::Threading::Thread::Sleep(500);
-
this->ProgressBar->Value = i;
-
}*/
-
/*Form1 MainForm;
-
MainForm.UpdateProgressBar();*/
-
return 0;
-
}
-
-
}//end multi_thread_test2
-
-
What i get as an output is -
------ Rebuild All started: Project: Multi_Thread_Test2, Configuration: Debug Win32 ------
-
Deleting intermediate and output files for project 'Multi_Thread_Test2', configuration 'Debug|Win32'
-
Compiling...
-
stdafx.cpp
-
Compiling...
-
AssemblyInfo.cpp
-
Multi_Thread_Test2.cpp
-
c:\documents and settings\lspencer\my documents\visual studio 2005\projects\multi_thread_test2\multi_thread_test2\Form1.h(139) : error C3374: can't take address of 'Multi_Thread_Test2::Form1::UpdateProgressBar' unless creating delegate instance
-
c:\documents and settings\lspencer\my documents\visual studio 2005\projects\multi_thread_test2\multi_thread_test2\Form1.h(140) : error C2665: '_beginthreadex' : none of the 2 overloads could convert all the argument types
-
C:\Program Files\Microsoft Visual Studio 8\VC\include\process.h(57): could be 'uintptr_t _beginthreadex(void *,unsigned int,unsigned int (__stdcall *)(void *),void *,unsigned int,unsigned int *)'
-
C:\Program Files\Microsoft Visual Studio 8\VC\include\process.h(67): or 'uintptr_t _beginthreadex(void *,unsigned int,unsigned int (__clrcall *)(void *),void *,unsigned int,unsigned int *)'
-
while trying to match the argument list '(int, int, void (__clrcall *)(void), void *, int, unsigned int *)'
-
Generating Code...
-
Build log was saved at "file://c:\Documents and Settings\lspencer\My Documents\Visual Studio 2005\Projects\Multi_Thread_Test2\Multi_Thread_Test2\Debug\BuildLog.htm"
-
Multi_Thread_Test2 - 2 error(s), 0 warning(s)
-
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
-
-
I now the problem lies with -
-
void ( *funcPtr)( ) = &Multi_Thread_Test2::Form1::UpdateProgressBar;
-
hMainThread = (HANDLE) _beginthreadex(NULL, 0, funcPtr, (void *) hwnd, 0, &tid1);
-
-
All help would be welcome. I have already tried using the CreateThread function to no luck.
2 4353
You are creating a native pointer to reference the function. You need to create a managed pointer to use it.
Managed pointers are declared with ^ instead of *
I explain it in more detail on this thread: Thread Regarding Native/Managed Pointers
i managed to fix it in the end via changing the code to use
using namespace System::Threading;
here it is, to help any one else -
-
#pragma once
-
-
namespace Multi_Thread_Test2 {
-
-
using namespace System;
-
using namespace System::ComponentModel;
-
using namespace System::Collections;
-
using namespace System::Windows::Forms;
-
using namespace System::Data;
-
using namespace System::Drawing;
-
using namespace System::Threading;
-
-
/// <summary>
-
/// Summary for Form1
-
///
-
/// WARNING: If you change the name of this class, you will need to change the
-
/// 'Resource File Name' property for the managed resource compiler tool
-
/// associated with all .resx files this class depends on. Otherwise,
-
/// the designers will not be able to interact properly with localized
-
/// resources associated with this form.
-
/// </summary>
-
public ref class Form1 : public System::Windows::Forms::Form
-
{
-
public:Form1(void)
-
{
-
InitializeComponent();
-
}
-
public:~Form1()
-
{
-
if (trd->IsAlive)
-
trd->Abort();
-
if (components)
-
{
-
delete components;
-
}
-
}
-
private: Thread ^trd;
-
public: System::Windows::Forms::Button^ Run;
-
public: System::Windows::Forms::ProgressBar^ ProgressBar;
-
public: System::ComponentModel::Container ^components;
-
#pragma region Windows Form Designer generated code
-
/// <summary>
-
/// Required method for Designer support - do not modify
-
/// the contents of this method with the code editor.
-
/// </summary>
-
void InitializeComponent(void)
-
{
-
this->Run = (gcnew System::Windows::Forms::Button());
-
this->ProgressBar = (gcnew System::Windows::Forms::ProgressBar());
-
this->SuspendLayout();
-
//
-
// Run
-
//
-
this->Run->Location = System::Drawing::Point(202, 61);
-
this->Run->Name = L"Run";
-
this->Run->Size = System::Drawing::Size(75, 23);
-
this->Run->TabIndex = 0;
-
this->Run->Text = L"Run";
-
this->Run->UseVisualStyleBackColor = true;
-
this->Run->Click += gcnew System::EventHandler(this, &Form1::Run_Click);
-
//
-
// ProgressBar
-
//
-
this->ProgressBar->Location = System::Drawing::Point(13, 12);
-
this->ProgressBar->Name = L"ProgressBar";
-
this->ProgressBar->Size = System::Drawing::Size(450, 43);
-
this->ProgressBar->Step = 1;
-
this->ProgressBar->TabIndex = 1;
-
this->ProgressBar->Value = 0;
-
//
-
// Form1
-
//
-
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
-
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
-
this->ClientSize = System::Drawing::Size(475, 95);
-
this->Controls->Add(this->ProgressBar);
-
this->Controls->Add(this->Run);
-
this->Name = L"Form1";
-
this->Text = L"Form1";
-
this->ResumeLayout(false);
-
-
}
-
#pragma endregion
-
private: System::Void Run_Click(System::Object^ sender, System::EventArgs^ e)
-
{
-
-
StartThreadFunc();
-
}
-
-
public: void UpdateProgressBar()
-
{
-
int i;
-
for (i=0;i<100;i++)
-
{
-
System::Threading::Thread::Sleep(500);
-
this->ProgressBar->Value = i;
-
}
-
}
-
public: void StartThreadFunc()
-
{
-
ThreadStart ^myThreadDelegate = gcnew ThreadStart(this, &Form1::UpdateProgressBar);
-
trd = gcnew Thread(myThreadDelegate);
-
trd->IsBackground = true;
-
trd->Start();
-
}
-
-
};// end class Form1 definition
-
}//end multi_thread_test2
-
-
I tried to use the ^ pointer but i kept getting two linking errors, oh well this method works fine.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jason Hickey |
last post by:
Has there been a change in the way the UI designer handles winform
inheritance in the 2003 version of visual studio.
Consider the following (try it if you are using 2003 Everything seems to
work...
|
by: Popoxinhxan |
last post by:
Dear experts, i want to develop an client application that consume the google
search web service.
In my MainForm i have a method to retrieve all the search result e.g.
GetGoogleResults().
Now i...
|
by: sarge |
last post by:
I would like to know how to perform simple multithreading.
I had created a simple form to test out if I was multithreading
properly, but got buggy results. Sometime the whole thig would lock
up...
|
by: Ilia |
last post by:
Hi folks,
I have some problems with ASP.NET Session State. The
following simple program runs well if the Session State
set as "InProc". If I switch to "SQLServer", the changes,
made by the...
|
by: Lucvdv |
last post by:
Can anyone explain why this happens with the code at the bottom?
It looked like a thread safety issue, but changing the declaration of
Label1 to Shared doesn't help.
Standard windows form;...
|
by: Sam |
last post by:
Hi,
I have a serious issue using multithreading. A sample application
showing my issue can be downloaded here:
http://graphicsxp.free.fr/WindowsApplication11.zip
The problem is that I need to...
|
by: Pradnya Patil |
last post by:
hi ,
I am trying to draw ' html div-tag ' on the screen which will
resemble a rectangle through vb.net code.
I want it to be drawn faster...so I introduced multithreading
using Threadpool. I...
|
by: dgleeson3 |
last post by:
Hello All
Im having lots of fun with window handles and invoke.
The code started off in a single class. Main thread set up a worker
thread and the worker thread updated the progress bar on...
|
by: farnooshr65 |
last post by:
hi i'm new to multithreading in c#, i have this code:
namespace WindowsApplication4
{
public partial class Form1 : Form
{
Thread t1;
delegate void d1();
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |