|
A reply to your modifications
That's not very usefull: it is kind of a snake that bites its own tail. You pass in a structure which contains a pointer to the class which itself contains the structure. So, what's the point ? So, you still can't pass different 'x' values to different threads (here again, potential race condition). Because I suppose if the OP wanted to do something like that was because he plans to start several threads and pass some specific information to each of them along the this pointer.
There is a workaround that but which is ugly too: create the structure on the heap and delete it inside the thread function.
|
|
|
|
|
Cedric Moonen wrote: Because I suppose if the OP wanted to do something like that was because he plans to start several threads and pass some specific information to each of them along the this pointer
The above scenario breaks your code too (unless he starts a single thread per class instance).
Cedric Moonen wrote: There is a workaround that but which is ugly too: create the structure on the heap and delete it inside the thread function.
This is useless if he needs write access to method local vars (but why should he need it?).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
CPallini wrote: The above scenario breaks your code too (unless he starts a single thread per class instance).
Sure. But hey, we both said this kind of code was totally ugly .
(I also *suggested* something like that in my first post but I didn't insist on it because I thought that was too ugly to be posted )
|
|
|
|
|
Thread management is not trivial. You need to have a clear understanding of what is involved in order to use them effectively. Simply using a class pointer as the thread argument can lead to trouble; as has been mentioned, consider,
class gladiator
{
gladiator() {}
virtual ~gladiator()
{
_beginthread(&threadFunction, 0, this);
}
static void threadFunction(void *pargList)
{
if (!pargList)
return;
gladiator * pActor = static_cast<gladiator *>(pargList);
_endthread();
}
};
This highlights that the owner of a thread needs to manage the thread lifetime. There are numerous ways to do this but if you use the class object then the class should hold on to the handle returned by _beginthread and insure proper thread termination before object destruction.
class gladiator
{
gladiator()
: m_bThreadAlive(false),
m_bStopThread(false),
m_hThread(-1)
{
m_hThread = _beginthread(&threadFunction, 0, this);
}
virtual ~gladiator()
{
shutdown();
}
protected:
bool m_bThreadAlive;
bool m_bStopThread;
HANDLE m_hThread;
virtual void shutdown()
{
if (m_bThreadAlive) {
do {
m_bStopThread = true;
} while (m_bThreadAlive);
}
}
static void threadFunction(void *pargList)
{
if (!pargList)
return;
gladiator * pActor = static_cast<gladiator *>(pargList);
pActor->m_bThreadAlive = true;
while (!pActor->m_bStopThread) {
}
pActor->m_bThreadAlive = false;
_endthread();
}
};
Note, the destructor for the gladiator class has a potentially endless while loop. A bug in the threadFunction while loop may prevent it from exiting properly. This is why the thread handle is needed to force the thread to exit if it fails to respond to the request to terminate (m_bStopThread). You can use whatever condition you want in shutdown() but it has the final say on the thread lifetime,
void shutdown()
{
if (m_bThreadAlive) {
m_bStopThread = true;
long lCoundown = 10000L;
do {
if (--lCoundown < 1L) {
if (!TerminateThread(m_hThread, -1)) {
}
m_bThreadAlive = false;
}
} while (m_bThreadAlive);
}
}
Threads have potential problems with concurrent data access as well (see EnterCriticalSection, etc.) and you probably want to look at using events (see CreateEvent, etc.) to communicate.
|
|
|
|
|
Hello,
Is there any way to check if service is available in scmanager from account that is different than the account the service is logging to??
I am using such code:
...
SC_HANDLE hHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
if (NULL == hHandle)
{
return FALSE;
}
ENUM_SERVICE_STATUS service;
DWORD dwBytesNeeded = 0;
DWORD dwServicesReturned = 0;
DWORD dwResumedHandle = 0;
DWORD dwServiceType = SERVICE_WIN32;
// Query services
BOOL retVal = EnumServicesStatus(hHandle, dwServiceType, SERVICE_STATE_ALL,
&service, sizeof(ENUM_SERVICE_STATUS), &dwBytesNeeded, &dwServicesReturned,
&dwResumedHandle);
if (!retVal) {
// Need big buffer
if (ERROR_MORE_DATA == GetLastError()) {
// Set the buffer
DWORD dwBytes = sizeof(ENUM_SERVICE_STATUS) + dwBytesNeeded;
ENUM_SERVICE_STATUS* pServices = NULL;
pServices = new ENUM_SERVICE_STATUS [dwBytes];
// Now query again for services
EnumServicesStatus(hHandle, SERVICE_WIN32,
SERVICE_STATE_ALL, pServices, dwBytes, &dwBytesNeeded,
&dwServicesReturned, &dwResumedHandle);
// now traverse each service to get information
for ( unsigned iIndex = 0; iIndex < dwServicesReturned; iIndex++ )
{
CString sServiceName = (pServices + iIndex)->lpServiceName;
if ( !sServiceName.Compare( "Net-SNMP Agent" ) )
{
return TRUE;
}
}
...
It seems to work on XP but not on Vista but I maybe wrong.
Many thanks,
Draco
|
|
|
|
|
Lord_Draconis wrote: It seems to work on XP but not on Vista but I maybe wrong.
What do you mean when you say it doesn't work on Vista? Are you getting errors from the functions? The functions return success but the data doesn't look good? Have you tried running it in the debugger and checking the results one line at a time?
Judy
|
|
|
|
|
I meant that on Vista it doesnt find the service that logs on as different user that you are currently logged on. E.g. service "XXX" logs on as ".\YYY" so when you are logged on "ZZZ" user this program doesnt find that service in SCM
|
|
|
|
|
Given that Vista has clamped down on user access rights, that doesn't surprise me terribly. I haven't played with Vista yet but I suspect that you're going to have to elevate the priviledges to Administrator for the code that is searching the SCM. Are you running in an Admin account? If not, log in as one and try it. If it works under Admin, you've confirmed where the problem is.
Judy
|
|
|
|
|
Hi friends,
I developed a dialog application.
I want the content to be displayed in "Print Preview" view.
I tried it by calling the OnFilePrintPreview() fn.
But its aborting.
Plz anybody help me out to solve the problem..
Thanks.
Reagards
Shiva
|
|
|
|
|
PrintPreview is quite less problematic than printing in paper. BTW if you say that is aborting but you give wether the exact error nor a code snippet... it will be pretty difficult to help you. There are very good programmers here, but AFAIK there are no mediums or telepats in forum.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
CFrameWnd* pFrameWnd=(CFrameWnd*)AfxGetThread()->m_pMainWnd;
{
CPrintPreviewView* pView=(CPrintPreviewView*)pFrameWnd->GetActiveView();
pView->OnFilePrintPreview();
}
Now im getting the preview, but the prgm hangs..
whats the problem?
Reagards
Shiva
|
|
|
|
|
I guess an unended bucle. Or a reiterative call to OnDraw without end. Or something like this...
This code seems to be ok, can you show what you are doing to print contents in the preview?
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Hi,
This OnFilePrintPreview() is available in CView or VEditView.
But u can`t use it directly in a Dialog.
If you want you have to Create a Dialog with Print Preview Option, inherited from CDialog Class.
Thanks and Regards.
SANTHOSH V
|
|
|
|
|
hi all !
I'm working on c#.net but i'm new in c++ . i want to know is there any difference between C++ and C++.NET from point of in powerful or ... .
i want to work in cell phones programm .
Thanx in Advance !
Yours ,
s_mostafa_h
|
|
|
|
|
Yes, there are differences.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
can u explain more about this !
thanx
s_mostafa_h
|
|
|
|
|
C++ produces native code. "C++.NET", more commonly known as Managed C++ or C++/CLI produces MSIL, which is the intermediate language understood by the .NET Framework...
the C++/CLI language differs by its syntax however, and such source code would not compile with a pure C++ compiler.
|
|
|
|
|
Another difference will be the "easibility", some things that in .Net are made with just a few lines using the libraries, MFC or other things... won't be available in pure C++ and you will have to learn/understand what that functions make and how they work to simulate their behaviour, and (if it is possible) to code them yourself.
C++ doesn't need the .Net framework so it may be more portable if you code it following standards.
BTW, if you want to code for cell phones... should it not be better to use embeded/embebed (or as it is written) C? J2ME
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
modified on Friday, December 14, 2007 6:56:55 AM
|
|
|
|
|
J2ME !
|
|
|
|
|
eing ???
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
For mobiles, use J2ME (Java 2 Mobile Edition)
|
|
|
|
|
only J2ME , even for tools programm .as u know , all mobiles don't support some languages ... .making menu for these languages is necessary for use them .
thanx
s_mostafa_h
|
|
|
|
|
I am doing a TankGame program,and I write a CBullet class.I write a thread function to make it move when it is created:
UINT BulletGo(LPVOID pParam)
{
CBullet* bullet=(CBullet*)pParam;
while(TRUE)
{
switch(bullet->m_bCanGo.GetDirection())
{
case UP:
bullet->GoUp();
break;
case DOWN:
bullet->GoDown();
break;
case LEFT:
bullet->GoLeft();
break;
case RIGHT:
bullet->GoRight();
break;
}
if(!bullet->m_bCanGo)
break;
else
bullet->MoveWindow(bullet->GetPosition().x,
bullet->GetPosition().y,
bullet->GetThisRect().Height(),
bullet->GetThisRec().Width());
Sleep(30);
}
return 0;
}
int CBullet::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
m_bCanGo=TRUE;
m_pThread=AfxBeginThread(BulletGo,this); //begin thread
return 0;
}
BOOL CBullet::GoDown()
{
if((m_ptPosition.y+m_rtThis.Height())>m_rtParent.bottom)
{
m_ptPosition.y+=2;
m_bCanGo=TRUE;
return TRUE;
}
else
{
this->DestroyWindow();
m_bCreated=FALSE;
m_bCanGo=FALSE;
return FALSE;
}
}
When the bullet go down and hit the bottom of client rect the program apprear errors.Why...Don't the "break" in the BulletGo() end the thread?
|
|
|
|
|
what exactly is the error?
|
|
|
|
|
The application pops up a dialog about error saying that Debug Assertion Failed!
Program : D:\Code\TankGame\Debug\TankGame.exe
File : wincore.cpp
Line : 304
......
|
|
|
|