|
how is it i communicate with the thread? events pr semaphores?
thanks
|
|
|
|
|
The best way is to provide a mechanism inside of your newly created thread that will accept a message to shut itself down. That is the only way to insure that all of the resources have been properly released by the new thread.
However in an emergency you can use the ::TerminateThread API function to "kill" the thread from another thread.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
I have been writing an app that uses a contextual dialog bar which displays information pertaining to the type of object represented by the active view in an MDI MFC app.
When the user selects a new view I need to know what view has been selected in order to change the dialog information. When the user closes a view but another is available I need to know what view has been selected automatically (again). When the user has closed the last view I need to close the dialog bar all together.
This sounded easy but I have tried all manner of messages - WM_ACTIVATE, WM_SHOWWINDOW, WM_KILLFOCUS, WM_SETFOCUS... Everytime I think I have it a message will not come through when I expect it and everything falls apart.
Is there an easy way to know this information?
thanks
kirk
|
|
|
|
|
I do this by handling WM_SETFOCUS of the view's frame window (CMDIChildWnd derived).
Pavel
Sonork 100.15206
|
|
|
|
|
Override OnActivateView() .
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
I declare these below data
[code]
CString Hash[2] = {"208803DD4D","146DB1CB1C"};
CArray<unsigned char,="" unsigned="" char=""> hashdata;
[/code]
and hashdata have some value
I compare CArray and CString like these
[code]
if (hashdata == Hash[0])
{
......
.....
.....
}
[/code]
but there is error message show
error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'const class CArray<unsigned char,unsigned="" char="">' (or there is no acceptable conversion)
Do I need to change CArray to other type of data? If you know how to solve my problem, please tell me.
Thank you very much.
|
|
|
|
|
do you want to compare the CArray of strings with the C-style array of CStrings? in that case you have to compare each element manually:
for (int i=0;i<hashdata.GetSize();i++)
{
if (hashdata.GetAt(i)==Hash[i])
{...}
}
-c
Green's Law of Debate:
Anything is possible if you don't know what you're talking about.
|
|
|
|
|
if (hash[0] == (LPCTSTR)hashdata.GetData())
hashdata should be NULL terminated for this to work.
modified 29-Aug-18 21:01pm.
|
|
|
|
|
std::string str;
char x[512];
ZeroMemory(x, 512);
int len = 0;
do
{
len = recv(socket, x, 512, 0);
str += x;
}while (len > 0);
I want to eliminate this double copying and want the data to come directly in the string. How can I do that?
modified 29-Aug-18 21:01pm.
|
|
|
|
|
There are several solutions including an ostreamstring object, insert_iterator, and insertbuf_iterator.
Look those STL tools up at SGI.
Kuphryn
|
|
|
|
|
sorry, but how can i give a back_inserter or an ostringstream as a parameter in recv call? I could not figure it out.
modified 29-Aug-18 21:01pm.
|
|
|
|
|
You can eliminate double copying if having an std::vector instead of a std::string is OK with you. Of course, copying is still done when resizing the container (but this is unavoidable):
std::vector<char> v(1);
size_t extra_space=0;
int len=0;
do
{
if(!extra_space){
v.resize(v.size()+512);
extra_space=511;
}
char * p=&v[0]+v.size()-extra_space-1;
len= recv(socket, p, extra_space, 0);
extra_space-=len;
}while (len > 0);
v[v.size()-extra_space-1]='0';
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thank you very much.
But, I use a few string operations that i believe is not available in vector. I have to look at the rest of my code to see whether vector can indeed be used.
|
|
|
|
|
Andrei Alexandrescu wrote a A Policy-Based basic_string Implementation[^] that's basically a drop-in replacement of std::string s. One of the incarnations of this class, namely that which uses VectorStringStorage as its Storage template parameter, uses a std::vector as back-end storage, so you can use it like I suggested in my latter post and yet have all the facilities std::string s come equipped with.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
It is a realy pity that Mr. Alexandrescu doesn't seem to be doing anything much with his Loki library. It is a very impressive body of work, but he almost seems to have abandoned it. I know there is a group on Sourceforge doing some work on it, but Andrei appears to be invisible.
And wouldn't it be great if we could use it with VC6, not that thats his fault of course. This is one of the few reasons for moving to VC7.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
VC7.1 you mean? VC7 can't for christs sake even handle PTS!
|
|
|
|
|
Mike Nordell wrote:
VC7.1 you mean? VC7 can't for christs sake even handle PTS!
I reckon I should know what PTS is but ...???
Is there such as beast as VC7.1?
STL, Boost, Loki et.all are all wonderfull stuff for our toolkit, but without compilers that are able to use them all we can do is sit and drool and wait impatiently.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
I reckon I should know what PTS is but ...???
Partial Template Specialization. For instance:
template <typename A> class MyTemplateClass{...};
template <> MyTemplateClass<int>{...};
template <typename A> MyTemplateClass<A *>{...};
Is there such as beast as VC7.1?
This is an internal version codenamed Everett (I think) which allegedly supports PTS. MS says it'll be released by February 2003.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
I reckon I should know what PTS is but ...???
Partial Template Specialization. For instance:
Yes of course. I knew it would be something obvious.
Joaquín M López Muñoz wrote:
Is there such as beast as VC7.1?
This is an internal version codenamed Everett (I think) which allegedly supports PTS. MS says it'll be released by February 2003.
I thought the folks working on Loki at Sourceforge had a complete imlementation for VC7, and I thought Loki required PTS, but it seems like I'm wrong somewhere along the line.
It is really surprising that a company the size of MS is taking so long to get issues like PTS handled in their compiler.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
The group on Sourceforge is working on porting Loke to VC6 --take a look at the project CVS respository. I don't know how much has been ported so far.
Some of the concepts discussed in Loki are implemented under various forms in Boost, notably functors. I think Boost is a more reliable library to use: continuous support, has been ported to many more platforms, appears more stable.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Last time I looked only a very small part of Loki had been implemented for VC6 and I was told that their wasn't much more they could do because of the VC6 compiler limitations we all know about.
I bought "Modern C++ Design" a few months back and got quite excited reading it, only to be let down by the inability to actually use it.
It sure would be great if someone would write a nice big reference + examples book for Boost.
I'll follow your suggestion and focus on Boost, and I guess STLPort.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
Forgive me if this is a dumb question I am very new to thread programming.. I have read a bunch of the articles here on code project about threads.. I want to do the following in a thread but the problem is that I need to pass the thread or start the thread with 3 vars "CString strPath, CString strDns, int nPort"... heres the snip of code that I am using to send a file.
I have tried to create a thread to do the following but it crashes every time..
Anyone have any ideas how I can do the following inside a thread??
CFile myFile(strPath, CFile::modeRead | CFile::typeBinary);
CSocket sSocket;
sSocket.Create();
int iConnect = sSocket.Connect(strDns,nPort);
if(iConnect == SOCKET_ERROR)
{
AfxMessageBox("Communication Error!");
}
else
{
CSocketFile sf(&sSocket);
CArchive ar(&sf, CArchive::store);
DWORD length = myFile.GetLength();
char *data = new char[length];
myFile.Read(data, length);
ar << myFile.GetFileName();
ar << length;
ar.Write(data,length);
myFile.Close();
delete[] data;
}
Thanks!
|
|
|
|
|
I do not see where you create a worker thread. Where does the thread begin?
Use a worker thread.
Kuphryn
|
|
|
|
|
I have tried a few different ways to create the thread but all result in the same type of errors.. here is one example that i used, but i cant pass the thread any variables.. I can just start it and stop it..
Header..
static UINT run(LPVOID p);
void run();
volatile BOOL running;
void CMyView::TransferFile()
{
running = TRUE;
AfxBeginThread(run, this);
}
UINT CMyView::run(LPVOID p)
{
CMyView * me = (CMyView *)p;
me->run();
return 0;
}
void CMyView::run()
{
running = FALSE;
}
|
|
|
|
|
Consider a global thread function. Otherwise, make run() a static function.
Kuphryn
|
|
|
|