|
Hello all,
I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:
LPDWORD iID;
HANDLE hThread;
hThread = CreateThread (
0,
0,
(LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
NULL,
CREATE_SUSPENDED,
iID);
}
static DWORD WINAPI Threadi()
{
System::Windows::Forms::MessageBox::Show("Hi there!");
return 0;
}
When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening?
Thanks
|
|
|
|
|
kikoso wrote: CREATE_SUSPENDED,
This is the problem. If you use this attribute then the thread is created in suspended state. More Info Here[^]
If you want the thread to start as soon as you launch the application, don't create a thread in suspended state. If you want to create in suspended state, then you can use ResumeThread() function.
|
|
|
|
|
Thank you HimanshuJoshi. I was reading in zig-zag the doc, so I didn´t realize about it.
I still have some problems. After changing it, I have a problem accessing the memory. This is the error I got (after using 0 instead of CREATE_SUSPENDED,:
<br />
Unhandled exception at 0x003628c2 in OpenCV Win32.exe: 0xC0000005: Access violation reading at position 0x0000000c.<br />
I´ve been thinking about sending the 3 parameter as
(LPTHREAD_START_ROUTINE)&OpenCVWin32::Form1::Threadi,
But I got a different error:
error C3374: Address of "OpenCVWin32:: Form1:: Threadi" can only be adopted if a delegate instance is created.
I will appreciate any suggestions. Thank you
|
|
|
|
|
LPTHREAD_START_ROUTINE is defined as:
typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)(
LPVOID lpThreadParameter
);
typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;
so, your thread function is to be:
static DWORD WINAPI Threadi(LPVOID lpThreadParameter)
{
System::Windows::Forms::MessageBox::Show("Hi there!");
return 0;
}
|
|
|
|
|
Hello!
Thank you! I didn´t though that passing the parameters will be compulsory. But nevermind, I put it on my code.
|
|
|
|
|
If you're using MFC, you *must* use AfxBeginThread to begin a worker thread. If you are using Win32, you should be using _beginthreadex to create a thread.
CreateThread is an API and that will even create a thread if you're linking to the single threaded version of the CRT (which will inevitably lead to disaster).
If you're writing managed code, take a look at Thread.Start method in the documentation. Basically, I want to emphasise that there's no sane reason or excuse to use CreateThread from your code. There are safer and sane alternatives.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hello,
I finally found the problem. The code for the Thread Function was inside the ref class, so that made it managed code. I finally put outside it, and now is working fine
Thank you.
|
|
|
|
|
I was having this initially
double* m_Value [4];
I want to redefine it differently.So what I did is
typedef vector <double> DOUBLE_VECTOR;
DOUBLE_VECTOR DoubleVec;
vector <double_vector> m_Value;
Above I did in .h file
Now I initialize it in constructor I mean .cpp file
DoubleVec.resize (10,0.0);
m_SplitArrPtr.resize (5,DoubleVec);
Till here I thing I have done fine.
Issue:
Now there is a function whose defination is somewhat like this
static void Func (double& ObjData,void* DataPtr);
In above function I have to fill the ObjData by DataPtr OK
So I did this
void ClassName::Func (double& ObjData,void* DataPtr)
{
int RowIndex = 0; // row index
int ColIndex = 0; // column index
//copy data to existing structure
for (RowIndex;
RowIndex < 5;
RowIndex++)
{
for(ColIndex;
ColIndex < 10;
ColIndex++)
{
ObjData [RowIndex][ColIndex] = DataPtr->m_Value [RowIndex][ColIndex];
} //for ColIndex
} // for RowIndex
}
But I am facing an error
"error C2109: subscript requires array or pointer type"
So please help me out that how I will be able to fill the ObjData.
|
|
|
|
|
Please, read the posting guidelines and format your code properly, it is almost unreadable.
deadlyabbas wrote: static void Func (double& ObjData,void* DataPtr);
deadlyabbas wrote: ObjData [RowIndex][ColIndex] = DataPtr->m_Value [RowIndex][ColIndex];
ObjData is a double, not an array so you can't use it like an array. DataPtr is a pointer to void, so if you want to access its content, you first have to cast it to something appropriate. Why are you using a pointer to void here ?
|
|
|
|
|
ObjData is a double &. To use two subscripts (ObjData[RowIndex][ColIndex]). You have to have a two dimensional array such as double **ObjData.
|
|
|
|
|
Hi ,
I just wanted to know, how can I register a service using Visual studio setup project. I can create a Setup Project successfully, it installs all the program, but I need a hint on how to run register my exe as one of the services
And of course how can I unRegister the Service when
uninstalling the application.
Your help much appreciated.
Regds
Jain
|
|
|
|
|
i have added a method in a header file as follows
void TestMethod()
{
..........
}
Now this header file is included in many places of same project.
The following error link error occurs.
Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj
How to rectify this error?
|
|
|
|
|
Why do you put the definition of your function in a header file ? Usually it is much better practice to move it to a cpp file.
Anyway, what you can do to remove your problem is make use of include guards. Surround your code in the file with this (replace MYFILE by something unique, typically the file name):
#ifndef MYFILE
#define MYFILE
....
#endif
You can also put #pragma once at the top of your file. Google for "include guards" for more information.
|
|
|
|
|
i tried with
#pragma once
its not working.
|
|
|
|
|
Where did you put it ? And what do you mean with "its not working" ?
Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice.
EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.
|
|
|
|
|
Yes i know that. To solve some issues they added function definition in header file.
I have added #pragma at the beginning of header file. Still same error(already defined).
|
|
|
|
|
Please see 'Stuart Dootson' reply.
|
|
|
|
|
Don't put implementation code in header files. Place this function in one .cpp file only.
|
|
|
|
|
For some issues we have included one function definition in header file.
That we cannot change.
However
#pragma once should slove the problem. But it doesnt why?
|
|
|
|
|
KASR1 wrote: For some issues we have included one function definition in header file.
That we cannot change.
Why ? And why not trying to fix the original issue ?
|
|
|
|
|
Actually this project is a static library. When linking with some exe/dll we are getting CTime link error. So we have defined a function with CTime and including that header in the exe/dll.
|
|
|
|
|
You do not seem to understand the difference between declaration and implementation. Your header file should contain only the declaration e.g.
void TestMethod();
The implementation code should go in a single source file (Test.cpp) as follows:
void TestMethod()
{
}
Try reviewing the relevant sections in your C/C++ guides for further explanations.
|
|
|
|
|
is in Test2 the methode ALSO defined?
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
|
Mark the method as 'inline', or place it inside an anonymous namespace:
namespace {
void TestMethod()
{
..........
}
}
[edit] To expand on that answer - by including a function body in a header, it's defined in lots of .obj files. When you link, the linker attempts to merge contents of all object files into an executable. As you have lots of functions with the same signature, it can't do that. The suggestions I've given prevent that. The inline suggestion should stop the function ever existing in the object file, while ht eanonymous namespace thing stops the function being visible outside each of the object files.[/edit]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|