|
See the example here and here.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hi everybody!
I'm making a routing algo simulator using MFC and GDI+.
I have created three function that are the three steps the algo does: ElectCluster(); FindMember(); StartSteadyState();.
Now the StartSteadyState() creates a thread for each node of the network that has been elected Cluster head (p->ch).
<br />
void CSimDlg::StartSteadyState(void)<br />
[...].br />
for (int i=0; i<m_vecNodes.size(); i++)<br />
{<br />
NODE* p = m_vecNodes[i];<br />
<br />
if(p->ch)<br />
{<br />
ThreadParam* pParam = new ThreadParam;<br />
pParam->m_pDialog = this;<br />
pParam->m_nNodeIndex = i;<br />
_beginthread(StaticTx, 0, (void*)pParam);<br />
counter++;<br />
}<br />
}<br />
[...].
and:
void CSimDlg::StaticTx(void * param)<br />
{<br />
ThreadParam* pParam = (ThreadParam*)param;<br />
pParam->m_pDialog->Tx(pParam->m_nNodeIndex);<br />
delete pParam;<br />
}<br />
<br />
void CSimDlg::Tx(int nNodeIndex)<br />
{<br />
[simulate transmission]<br />
[graphic: draw nodes and color lines of the link used]<br />
}
Now my problem is that I would like to make different rounds:
<br />
void CSimDlg::OnBnClickedStart()<br />
{<br />
for(int i=0; i < NumberOfRounds; i++)<br />
{<br />
ElectCluster();<br />
FindMember();<br />
StartSteadyState();<br />
}<br />
}
This obviosly doesn't work because "for" doesn't wait for all the thread created by StartSteadyState();
I have tried to solve using:
<br />
hThread = (HANDLE)_beginthread(StaticTx, 0, (void*)pParam);<br />
WaitForSingleObject(hThread,INFINITE);<br />
It seems to work but doesn't shows the graphic. I have tried to put a MessageBox before WaitForSingleObject and for as long as I don't press the ok button I see that the links and nodes are coloring. pressing ok the graphic part stops.
What am I doing wrong?
Do you have any advices or suggestion?
modified 11-Mar-21 21:01pm.
|
|
|
|
|
The general solution is following:
1. Create thread with attribute "normal" and then suspend it (somehow the creation in suspendedstate does not work well.
2. activate thread when needed.
Serge Chychkan
|
|
|
|
|
First of all thankyou for your fast reply.
Hum... you mean create a thread for each cluster head and suspend it, than resume all at the same time after the for(... if(p->ch) ) ?
I have tryed using SuspendThread(hThread); and ResumeThread(hThread); .. the thread works but the graphic part is not shown again.
modified 11-Mar-21 21:01pm.
|
|
|
|
|
blpdone wrote: hThread = (HANDLE)_beginthread(StaticTx, 0, (void*)pParam);
WaitForSingleObject(hThread,INFINITE);
This is a very expensive way of making a function call. You are practically just making a call to StaticTx(0, (void*) pParam) since your function won't return until the StaticTx function has returned. This is definately incorrect. You should never block the main GUI thread as it is needed to handle window messages to the application.
I'm not sure why you need a thread for each node, and I hope that you are drawing only in response to WM_PAINT or have a good reason for not doing so.
|
|
|
|
|
Thankyou for your reply.
what do you suggest to do instead?
I have made a thread for each Cluster Head [CH] node because they have to make the same action at the same time. The Tx(int nNodeIndex) function is used by all CH node to activate, one at the time, the node that has to trasmit (coloring the link, redrawing all the nodes on the IDC_PLOT control). The same thing is done by all the CH at the same time managing all the nodes each CH has under its control.
Everythig seems to work fine for one round but if I increase it I have to "tell" the function to wait all the thread of the StartSteadyState(void) to end before restarting everything with the ElectCluster(void) .
Any advice on how to solve it? Or some ideas on other ways to do the same thing?
Thankyou Again
modified 11-Mar-21 21:01pm.
|
|
|
|
|
It's really hard to say without really knowing the specifics of the problem. But the thing is that even with threads, you aren't really doing the same action at the same time. How much processing is each cluster head doing? For each step? How many cluster heads? Actually if processing is low for each step I would be just using a WM_TIMER to update my state at discrete points in time and Invalidate.
Here's an idea in sorta pseudocode. Frankly I find threads with message loops to be much more flexible but this idea may or may not be suitable. I trust you can take care of any synchronisation issues with shared variables.
ThreadProc(...)
{
WaitForMultipleObjects( Start Event or Stop Event )
if Start Event
int nCurrentTime = 0;
while(true)
{
foreach(clusterHead in ClusterArray)
{
clusterHead->PerformNextStep(nCurrentTime);
}
Invalidate();
WaitForSingleObject(Stop Event, millisecsToNextInterval)
if Stop Event
return;
nCurrentTime++;
}
if Stop Event
return;
}
|
|
|
|
|
What is the best way to destroy a ToolTip Ctrl?
->DestroyToolTipCtrl()
or:
->DestroyWindow()
do i need to delete the control after that or if I use one of the above there will be no memory leeks?
Moreno
|
|
|
|
|
Hello friends. I on one of forums have found this e-mail:
unknown_cpp@yahoo.com. He helps everything, if a problem interesting.
|
|
|
|
|
vir_spb wrote: unknown_cpp@yahoo.com. He helps everything, if a problem interesting.
What do you want to say?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
maybe if you have a problem you can send your problem to this address and maybe you find your problem!
whitesky
|
|
|
|
|
where can i readup on writing to and reading from a command promp window?
|
|
|
|
|
If you mean writing a Windows Console application then any beginners book or tutorial for C/C++ programming.
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
Check out the Console Functions.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
error C2533: 'CSentenceArray::__ctor' : constructors not allowed a return type
Code:
SentenceAr.h
class CSentenceArray
// Sentences stored as an array
{ public:
CSentenceArray(bool bCreateArray);
virtual ~CSentenceArray(void);
void readFromFile (const char *pszFileName);
//void Display (ostream &Sentence);
void sortSentences(bool bAscending); // implements BubbleSort or quicksort
void Clear();
int FirstWord(const char *ptrWord);
virtual int HowManyWords(const char *pszWord);
protected:
//virtual void storeSentence(const char *pszSentence);
//virtual const char *getNextSentence();
//virtual const char *peekNextSentence();
//virtual void swapWithNext();
//virtual void Reset();
//virtual void eraseAll();
private:
char *m_pSentences;
int m_iCurrentPos;
};
SentenceAr.cpp
#include "StdAfx.h"
#include ".\sentenceAr.h"
CSentenceArray::CSentenceArray(bool bCreateArray)
{
int x = NULL;
}
virtual CSentenceArray::~CSentenceArray(void)
{
}
Jon
|
|
|
|
|
I've created a console project and added you class.
Apart from removing virtual from destructors implementation (You've marked it as virtual in the header file so there's no need for "virtual" keyword in the implementation file) there were no errors...
I'm usig VStudio 2003
|
|
|
|
|
I have written some program codes in C#.NET and VC++.NET.I used to use .NET 2003.But as far as it doesn't support XML Documentation for VC++, I have obliged to switch to .NET 2005.It makes XML files for me. When I was using .NET 2003, I used NDOC to make .chm files out of this XML file(which was made from C#.NET 2003), but now that I am using .NET 2005 and making XML files out of VC++.NET, it is impossible to use NDOC for this kind of XML files, because NDOC only supports XML files made by C# compiler. What can I do? Is there any other tools which support both of them? I am in urgent need. Looking forward to hearing from you.
Thanks in advance.
|
|
|
|
|
Is there a flag i need to raise in order to draw each item (such as in list control i use Ownder-Draw style and then the method DrawItem is getting called).....
thanks in advanced
Yaron
Interface basics click here :
http://www.codeproject.com/com/COMBasics.asp
don't forget to vote
|
|
|
|
|
Do you need to customize CTreeCtrl?
whitesky
|
|
|
|
|
Generally, you are better off using custom draw[^] with the tree view.
Software Zen: delete this;
|
|
|
|
|
I build a based dialog application!
and I add some controls ,such as CEdit, CProgress, Cbutton
etc.
But I dbclick the title to maxsize the dialog,
the control still looks old, not changed together
It makes me angry, and how to adjust them to be look comfortable?
if you know, please describle it detailly!
Waiting for answers online!
Just my interest for these common things
|
|
|
|
|
Hope I understood your question
You want to set size and location for your controls when you max or min your dialog
you can use MoveWindow or SetWindowPos in WM_SIZE
for example m_edit.MoveWindow(cx,cy,cx/2,cy/2)
I hope its helpful to you
whitesky
|
|
|
|
|
yes, that's right!
but does it exist any easy way to make all controls can adjust themselves ?
I see some codes in VB,
:
with each obj in form
// adjust the size
Has it the same effect codes in VC?
Just my interest for these common things
|
|
|
|
|
maybe its some helpful to you
CWnd *pWnd2;
pWnd2 = GetTopWindow();
do
{
if(!pWnd2)
break;
pWnd2 ->ShowWindow(0);
pWnd2 =pWnd2 ->GetNextWindow();
}while(pWnd2);
</code>
When you run this code all your controls are hide
whitesky
|
|
|
|
|
xuwenq88 wrote: yes, that's right!
There's an example in the Extras section of this article.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|