|
You could create a new worker thread. This worker thread can do the background stuff and keep updating the progress control. Thus the cancel button on your main window will still be receptive and not frozen as you might be experiencing now.
Nish
The lady in red is dancing with me
Cheek to cheek
There's nobody here
It's just you and me
It's where I wanna be
But I hardly know this beauty by my side
I'll never forget the way you look tonight
|
|
|
|
|
This being the obvious solution, I guessed it may be the 'too hard' solution he found in MSDN.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
|
|
|
|
|
You're about right It's no so hard, but I thought that building new thread, organizing some kind of inter-thread sinchronization is too big to doing such small task. Besides really I have had separate thread that perform that task already. But it's so bulky That why I decided to ask simplier solution.
|
|
|
|
|
Morozov Alexey wrote:
That why I decided to ask simplier solution.
I have always found that the simpler solutions are often the right solutions. The complicated solutions make you feel nice, but often just ends up in a mess.
KISS is actually true [except for the 2nd 'S']
Nish
The lady in red is dancing with me
Cheek to cheek
There's nobody here
It's just you and me
It's where I wanna be
But I hardly know this beauty by my side
I'll never forget the way you look tonight
|
|
|
|
|
Morozov Alexey wrote:
After searching through MSDN some implementation approach has appeared but it's so difficult.
Have you checked Feb'97 issue of MSJ - "Wicked Code" column by Jeff Prosise contains ready-to-use CWaitDialog class. The article may be in MSDN on your harddisk, if it's not there, go to msdn.microsoft.com
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Thanks a lot
I'll be searching more carefully in future. Sh*t, why did I can't find it myself It's really what I need.
|
|
|
|
|
Ok I posted before and some helpful folks advised me that I should use messages to communicate between my Dialog application and my threads connecting to a serial device. Trouble is I'm stuck on what the handles and messages are to send the messages so here's where I'm at:
Added this to .h file:
//}}AFX_MSG
afx_msg LRESULT OnCustomMessage(WPARAM w, LPARAM l);
DECLARE_MESSAGE_MAP()
Added this to .cpp file
const UINT wm_Message = RegisterWindowMessage(_T("A8479983-30B2-11d6-BD06-0010A4FDB6CB"));
//..snip ..//
ON_REGISTERED_MESSAGE(wm_Message, OnCustomMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
And this is in the thread:
CString *ps = new CString("Thread is busy.");
::PostMessage(HWND_BROADCAST, wm_Message, (WPARAM)0, (LPARAM)ps);
So as I understand it, the thread posts the message wm_Message and when the window receives the message it calls the function OnCustomMessage. I'm pretty sure using HWND_BROADCAST is wrong, as it isnt working anyway. What handle do I need to give the post message function?? and the next question is how do I get this handle from the thread??
Hopefully this will be the best way of doing what I want to do? The aim is to have a dialog that connects to the serial device and updates List Box control with records in the device. The record selected is then requested from the device and saved to a file. The whole time I also want a richedit box to be displaying progress messages, and maybe updating a progress bar too! Is sending messages back and forth the way to do this or is there another way of sharing the data between the dialog and the threads?
Sorry for the long message and thanks again for any help
-Mark
|
|
|
|
|
Mark,
Change the HWND_BROADCAST to the window handle of the parent.
ie. PostMessage(hWndParent, wm_Message, (WPARAM)0, (LPARAM)ps);
The hWndParent handle can be passed into the thread during creation or maybe simply via another custom message...
A note of caution. Posting messages with pointers to memory is bad news. Make sure that memory posted from the thread to the parent is not cleaned up/garbled before or during the parent's processing of the message. In your example, your passing a new instance of a CString() which needs to be cleaned up by the parent.
|
|
|
|
|
Thanks for the reply,
This is what I'm calling to start the thread:
HANDLE myConnectThread = AfxBeginThread(ConnectThread, (LPVOID)this);
So how would I change it to pass it the hWndParent? Also how do I get the hWndParent value?? is it this->m_hWnd??? This whole handle thing has got me slightly confused.
Also I know how the parent needs to clean up the CString! A helpful person from here has already brought it to my attention!
Is there any better way of doing all this though?? I mean because these threads are static I obviously cant access the member variables of the dialog class directly. Is there other ways of making threads where I could be able to do this? I see there are other ways of meaking threads ie: CreateThread or _beginThread, whats the difference with the one I'm using and which would be betst?
Thanks
-Mark
|
|
|
|
|
Ok I've got the function working with this:
HANDLE myConnectThread = AfxBeginThread(ConnectThread, (LPVOID)this->m_hWnd);
And in the thread:
UINT AlstomSerial::ConnectThread(LPVOID pParam)
{
::PostMessage((HWND)pParam, wm_Message, (WPARAM)0, (LPARAM)ps);
}
This feels a bit dodgy to me?? Anyone suggest a better way of doing this??
|
|
|
|
|
That seems about right. You may want to call ::IsWindow((HWND)pParam) first just to make sure the handle is valid. The parent window may have exited/died before the thread gets its hands on it. Stranger things have happened, and when multithreading, playing safe is a must.
|
|
|
|
|
Anybody know any good explanations of the inner workings of double buffering to remove flicker...?
I'm using the CMemDC class right now, but am currently experiencing problems, so i'd like to tackle the problem line by line and just do it manually.
I get the gist of it and even implemented it, but I used a bitmap from resource instead of creating it dynamically using CreateCompatibleBitmap. I'm doing something wrong and would like to see an exmaple of how to do it fom start to finish.
I'm aware of the classes currently on CP, but would like an explanation of double buffering with a small example I can pick apart and figure out what i'm doing wrong.
CreateCompatibleDC
CreateCompatibleBitmap
Select compatible bitmap into compatible DC
LineTo on compatible DC
BitBlt compatible bitmap to original DC
What am I missing here...?
Thanx a bunch!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
Looks OK. Just remember to call CreateCompatibleBitmap for original DC, not the compatible, in-memory one.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
ok this is vexing me
i have a string like this:
blahblahblahblah$blahblahblahblah$
notice is contains $ characters.
i want to SPLIT this string using the $ as a delimiter.
The string is stored in a CString (i'm using MFC).
Can someone show me how I can tokenize this string. A SIMPLE example please!!!
ARGH, thanks!!
|
|
|
|
|
CString buff("This is$my string");
for(int x=0; x<<buff.GetLength(); x++)
{
if(buff.Find('$', x)){
CString strSplitLeft = buff.Left(x-1);
CString strSplitRight= buff.Right(x+1);
}
}
Notice this only splits a string in half nothing more. It find the first $ character and copies anything left of it to a buffer and anything right of it to another buffer.
It would reauire a littel tweaking to split a string multiple times.
Cheers!
p.s-Ignore the << it should only be one...but I don't know the excape code in HTML for single angle brackets. Sorry!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
|
omg thanks been looking for a simple way like that!!!!
|
|
|
|
|
Cool...that is easy!
Thanx Mike!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
I wish we could rate these posts like you can at CodeGuru - what a helpful post!
|
|
|
|
|
Please help, whats wrong with this code?
"if statment"
char r;
gets(r);
if (r == "ok")
{
cout << ("good job");
}
What wrong with that? how can i make it work?
Thanks!
~SilverShalkin
ps... im new to this forum, so if you see another thread like this one, im sorry
didnt mean to.
Programming is an art, dont spoil it with work.
|
|
|
|
|
Uh, a lot. First of all, "char" is a single character, not a whole string. You need to do an char array for a complete string, or use a CString from MFC. Second, you can't compare char arrays with the "==" symbol, you need to use "strcmp" ("==" does work for CStrings however).
So, correct code would look something like this:
char r[256]; //note that if user inputs more than 255 characters, program might crash
gets(r);
if(strcmp(r,"ok")==0)
{
cout << ("good job");
}
Cya (c:
|
|
|
|
|
Lofote wrote:
or use a CString from MFC.
Or in this case, use std::string seeing as MFC is not being used anywhere else in the code.
Lofote wrote:
Second, you can't compare char arrays with the "==" symbol,
Really ?
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
you cant compare char arrays with "==", then how do you do it?
Thanks
~SilverShalkin
What little life a rose may give, its the buety that will last forever.
|
|
|
|
|
With the strcmp() function.
Jon Sagara
What about ?
Sonork ID: 100.9999 jonsagara
|
|
|
|
|
Jon Sagara wrote:
With the strcmp() function.
or even lstrcmp(), if you want to use the Win32 api to do it.
impuzible
|
|
|
|