|
Yeah, that's what I was advocating.
|
|
|
|
|
There has to be a reason that it's expecting to find this file, you searched for the word 'sensors' in the text of your entire project ?
|
|
|
|
|
Did you look in the project's .dsp file?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
no, will try that..
Thanks
C++Prog
|
|
|
|
|
Does anyone know how i can call a function I created inside a thread? Thanks a lot!
I have a class with a list object in it. I need to do some processing that's why
I created a thread which does that. I have created another function which accepts
3 or 4 strings inside the class...
void AddListItem(CString strbufffn, CString strbuffip, CString strbuffdate, CString strbuffstat);
note that inside the function im adding items to the CListCtrl of my List object
and i need the information processed by my thread to be passed to the function.
DWORD WINAPI ReceiveThread(LPVOID lParam)
{
//some processing here
//need to call the AddListItem here
}
Again thanks for the help!
|
|
|
|
|
In general you just call it. Be warned however; multithreading introduces many new and often unexpected complexities to an application. If you don't understand these complexities you're better off avoiding it. For example something as simple as incrementing a variable might not work as expected if more then one thread tries to "touch" it at the same time. If you've never heard of EnterCriticalSection , LeaveCriticalSection , InterlockedIncrement and many others, don't even consider using multiple threads.
Steve
|
|
|
|
|
Thanks but I already tried that and unfortunately it did not work. It had an error saying that the function is an external function or something.
|
|
|
|
|
This doesn't sound like a threading problem to me. Can you call the function at all even when no threading is used (in the same file as you do when threading is used)?
Steve
|
|
|
|
|
yes I can. I've been looking at every possible problem but it seems like the thread is it. the thread can't see the function i declared in the main class.
|
|
|
|
|
Llasus wrote: the thread can't see the function i declared in the main class.
The compiler knows nothing about threads. Threads are supported by the OS and the C-runtime library so the visibility of functions will not be effected. That's why I suggested putting the call to the function in the same file (and as close as possible) as the call made in the thread function: as location is an issue.
Steve
|
|
|
|
|
I tried putting the function in the same file as the thread and it still generated the same error as it cant see the function when called in the thread.
|
|
|
|
|
Perhaps if you posted some code and the exact error message text something might reveal itself.
Steve
|
|
|
|
|
Ok here are cutout of the codes that i have..
in LPAServerDlg.h
class CLPAServerDlg : public CDialog<br />
{<br />
public:<br />
void AddListItem(CString strbufffn, CString strbuffip,<br />
CString strbuffdate, CString strbuffstat);<br />
}
in LPAServerDlg.cpp
DWORD WINAPI ListenThread(LPVOID lParam);<br />
DWORD WINAPI ReceiveThread(LPVOID lParam);
<br />
class CAboutDlg : public CDialog<br />
{<br />
};<br />
<br />
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)<br />
{<br />
}<br />
<br />
DWORD WINAPI ReceiveThread(LPVOID lParam)<br />
{<br />
...codes<br />
AddListItem(strbufffn, strbuffipa, "Time", "Printing");<br />
...codes<br />
}<br />
<br />
DWORD WINAPI ListenThread(LPVOID lParam)<br />
{<br />
..codes<br />
hThread = CreateThread(0, 0, ReceiveThread, (LPVOID)Message, 0, NULL);<br />
..codes<br />
}<br />
<br />
void CLPAServerDlg::AddListItem(CString strbufffn, CString strbuffip,<br />
CString strbuffdate, CString strbuffstat)<br />
{<br />
int nItem;<br />
<br />
nItem = m_ListFile.InsertItem(0,strbufffn);<br />
m_ListFile.SetItemText(nItem, 1,strbuffip);<br />
m_ListFile.SetItemText(nItem, 2,strbuffdate);<br />
m_ListFile.SetItemText(nItem, 3,strbuffstat);<br />
}<br />
error C2440: '=' : from 'void (__thiscall CLPAServerDlg::*)(class CString,class CString,class CString,class CString)' to 'void (__cdecl *)(class CString,class CString
,class CString,class CString)' cannot change. (see the new of operation ; help)
sorry bout the error message.. its all in japanese and i just translated it. Thanks again for all your time and help!
|
|
|
|
|
Can you indicate on which line the error occurs?
Steve
|
|
|
|
|
oh, sorry but i pasted the wrong error message. the error line there occured on the call of the AddLIstItem() on the ReceiveThread.
|
|
|
|
|
I'm still not sure where the error occured or now if the error you gave is the correct error. If the error message is correct it means you're trying to assign a member function pointer to a pointer that expects a "free" (non member) function.
Steve
|
|
|
|
|
I made changes in my code earlier thats why i pasted the wrong error message. I attempted earlier to pass the function's address to the thread but failed. Still i cant directly call a function of the class inside a thread even though they are of the same file.
Here is the correct error message..
E:\Program Files\Microsoft Visual Studio\MyProjects\LPAServer\LPAServerDlg.cpp(355) : error C2065: 'AddListItem' : It is the discernment child who is not defined.
Again, thanks alot for the time Steve
|
|
|
|
|
I haven't got the whole file so line numbers don't tell me where the error occured. You'll have to show post some code and mark the line where the error occurs.
Steve
|
|
|
|
|
DWORD WINAPI ReceiveThread(LPVOID lParam)<br />
{<br />
...codes<br />
AddListItem(strbufffn, strbuffipa, "Time", "Printing");
...codes<br />
}
|
|
|
|
|
ReceiveThread is a global function not a class member. AddListItem is a member function. You have to call a member function either in another member function or qualify the call using a . or a -> .
Steve
|
|
|
|
|
You need an object of your class type to call a non-static member function of that type.
You could pass an object (pointer) into your thread proc via the lParam:
DWORD WINAPI ReceiveThread(LPVOID lParam)
{
MyClass *pMyClass = (MyClass *)lParam;
//some processing here
pMyClass->AddListItem(...);
}
Pass the MyClass pointer as the LPVOID lpParameter in the CreateThread() call.
|
|
|
|
|
ok ill try that in a bit. but would it also be possible of passing the pointer of the function?
|
|
|
|
|
Llasus wrote: but would it also be possible of passing the pointer of the function?
Yes, but only if it's a static method, or a regular function.
--
Featuring GRATUITOUS ALIEN NUDITY
|
|
|
|
|
i have tried doing a static method however, since inside the function i am doing access to a non-static CListCtrl in which ill need to add items to it generates the error that it cant recognize the CListCtrl of the list in the dialog box.
|
|
|
|
|
Thanks Mark, I have already solved the problem using your way by passing the pointer to the class.
|
|
|
|