|
I have been using multithreading with MFC for over 5 years and in my experience you will run into problems if you do any GUI code in a thread that is not the main window thread. You must never call any MFC gui code across thread boundries and never create any CViews in a thread that is not the main thread. This is because MFC uses thread local data to store a lot of its info about Doc and view. With that said your problem may be auto delete as CWinThread classes will automatically delete them selves on termination. You should disable this by setting m_bAutoDelete to FALSE and free the object yourself.
John
|
|
|
|
|
hi john,
i (obviously) forgot to mention that the app i'm doing is dialog based. i still can't figure it out: how can one thread affect the other, because my filesystem thread, when run alone, really works fine..?
i'll try the m_bAutoDelete thing, is there something i should know about it? is there a standard way everyone uses to stop a thread?
thanx
---
kick ash.
http://sprdsoft.bigmoron.com
http://t1tan.cjb.net
|
|
|
|
|
T1TAN wrote:
i (obviously) forgot to mention that the app i'm doing is dialog based.
Oh. I saw that, I'm sorry about the long rant about doc/view but you still have to be careful of GUI calls with dialog based apps.
Did you check the source for wincore.cpp, at Line 304? What version of VC are you using? I looked at my version of vc6 and it is not a valid instruction but it is exactly where I thought the problem to be. I mean it is between the CWnd::FromHandle(HWND hWnd) or CWnd::FromHandlePermanent(HWND hWnd). Niether of these functions will work accross threads.
[EDIT]
Ok line 304 in wincore.cpp is in VC7. Here:
CWnd* PASCAL CWnd::FromHandle(HWND hWnd)
{
CHandleMap* pMap = afxMapHWND(TRUE);
ASSERT(pMap != NULL);
The assert fails because the handle map is null when you are calling this member from a thread that did not create the window. This is fixed by putting the dialog in the main application thread.
[/EDIT]
John
|
|
|
|
|
argh, argv, argc...
John M. Drescher wrote:
I'm sorry about the long rant about doc/view
heh, it's better than saying nothing, is it?
i haven't checked it out (and i'm not at my PC right now..) but i sure will. so.. my dialog cannot be inside a thread? now that sucks. i really need to do this app multithreaded, because my app is a file exchange && chat server, and it would be really awkward to leave it single-threaded.
if you have any other ideas for making the app more responsive, now is the time and place
thanx again
---
kick ash.
http://sprdsoft.bigmoron.com
http://t1tan.cjb.net
|
|
|
|
|
T1TAN wrote:
so.. my dialog cannot be inside a thread?
You can but in a lot of cases you will run into problems like this. You must not have the parent of the dialog be the main window. You must not call any of the dialog's GUI calls from any other thread than the one the dialog was created in. You must not delete the dialog object from a thread that did not create it.
John
|
|
|
|
|
John M. Drescher wrote:
You can but in a lot of cases you will run into problems like this.
nothing is ever easy..
John M. Drescher wrote:
You must not have the parent of the dialog be the main window.
i made it so that the desktop window is the parent, without knowing this
John M. Drescher wrote:
You must not call any of the dialog's GUI calls from any other thread than the one the dialog was created in.
hm.. does that mean that i should send a message to thread, and then handle it so that the owner thread takes care of the dialog? a man's gotta do what man's gotta do..
thanks john, you're a great help
dst
---
kick ash.
http://sprdsoft.bigmoron.com
http://t1tan.cjb.net
|
|
|
|
|
T1TAN wrote:
does that mean that i should send a message to thread
Yes!
John
|
|
|
|
|
hi,
With what u have mention above, does it means that we cannot include any updates of the GUI codings in the thread function?
I have 2 MFC program, can i use just one button to control these 2 MFC?
Btw, this 2 MFC is link by a database(Microsoft Access). How can i run this 2 MFC concurrently and continously with just one Start button and will end it once i click on the Exit button.
And I tried using thread function with database. But it has some errors on the recordset that i have used. can u advice me on this issue?
Thanks lots..
dReaMerzZ
|
|
|
|
|
I get back on the rest if I have some time.
dreamerzz wrote:
And I tried using thread function with database. But it has some errors on the recordset that i have used. can u advice me on this issue?
This is because DAO is not multithreaded. There are workarounds for this but it is much better to use ADO to connect to the database. I use ado and connect with any of the threads in my program with no problems at all. A good ado article is here:
http://www.codeproject.com/database/caaadoclass1.asp
John
|
|
|
|
|
Hi,
Thanks for ur help and advice
dReaMerzZ
|
|
|
|
|
Sorry I did not get the rest of the info for you. I am behind on a very important deadline and I do not have a lot of time.
John
|
|
|
|
|
I know how to make a BSTR from a CString object but can't find the way to do the other way around.
Does anyone know how to create a CString object from a BSTR ??
|
|
|
|
|
Suppose you have :
BSTR b; // Allocate this by SysAllocString API
CString s;
then use:
s=b;
So simple.:-DOK It will work.
Regards,
Darshan Jani
|
|
|
|
|
Is this what you want ?
BSTR bstr = SysAllocString( L"abc" );
CString cs;
cs = bstr;
GuimaSun
www.nexsun.com.br
|
|
|
|
|
Hello,
At the moment I am outputing a bitmap on the screen and when it resizes etc. it redraws that bitmap ok. I need to output more bitmaps to the screen and when it need to be redrawn to display all of them. How can I do that? I tried double-buffering but I don't know how to add the "small" bitmaps to the off-screen bitmap.
Thanks
|
|
|
|
|
Hi,
I am trying to customize the CFileDialog. I want to have the select multiple files feature to turn on and off based on the user behavior (e.g. click a button on this customized dialog). Is it possible to do that ? I only know that it works only if I set the ofn flag to OFN_ALLOWMULTISELECT before intialize.
Thanks.
|
|
|
|
|
I'm not seeing that it makes any sense to ask the user if they want to select multiple files or not. Depending on the context in which files are selected, that feature should be determined by the program. What are the conditions that would toggle it one way or the other?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Actually the customize dialog will have 2 different sets of display. One of them only allows the user to select a single file, while the other one is intended to allow them to select multiple files. In both case the program will do some customize validation before opening the file(s). There will be a button for them to switch between the displays.
The reason i do this is because select multiple files won't allow work in different directories. I actually put a list box in this customize dialog to store the selected files such that when user browse to different directories, the previous selected files will not be lost.
|
|
|
|
|
benny2323 wrote:
The reason i do this is because select multiple files won't allow work in different directories.
I assume you know what folders are multiple-select and what folders are single-select before displaying the dialog. While you probably aren't allowing the button to switch to multiple-select mode when in a single-select folder, it doesn't make much sense to be in a multiple-select folder and have the button switch to single-select mode.
Realize that I'm making this observation based on the very little information that's been provided, but it just seems to stray too far from the (de facto) standard.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
My Service gets stopped when checked for dependensies through SCM.
ie, scm->rgt click->Properties->Dependencies
Then my CallBack function (for service stopping)gets called.
|
|
|
|
|
I get this linker error everytime I tried to create an instance of _variant_t,
I've check my project settings in case i miss any library.. but everything seems in order..
what did I miss ??
|
|
|
|
|
yanuart wrote:
what did I miss ??
Telling us what the linker error is.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
oh, that's okay I just include comdef.h and everything's fine
|
|
|
|
|
I'm trying to create a clients/server program, which only one server and lots of clients..
so I get the computer name using GetComputerName( ) as a server name pipe.
I had no problem creating name pipe when
lpszPipename = "\\\\.\\pipe\\mynamepipe";
but when I changed lpszPipename, it doesn't work..
LPCTSTR lpszPipename = "\\\\computer1\\pipe\\mynamepipe";
<br />
LPCTSTR lpszPipename = "\\\\computername\\pipe\\mynamepipe";<br />
<br />
HANDLE hPipe;<br />
<br />
hPipe = CreateNamedPipe( <br />
lpszPipename,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUFSIZE,
BUFSIZE,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
<br />
if (hPipe == INVALID_HANDLE_VALUE) <br />
{<br />
MessageBox("CreatePipe failed"); <br />
return ;<br />
}<br />
What am I doing wrong?
|
|
|
|
|
The MSDN says:
lpName
[in] Pointer to the null-terminated string that uniquely identifies the pipe. The string *MUST* have the following form:
\\.\pipe\pipename
I suggest you put your "computername" preceding the "pipename" name part.
GuimaSun
www.nexsun.com.br
|
|
|
|