when my window talks to my thread

hi,

I started my ui thread like this

theApp.m_uithread=(CMyUIThread*)AfxBeginThread(RUNTIME_CLASS(CMyUIThread));

and I have a window that talks to it like this

CMyWnd::MyFunction() {
theApp.m_uithread->PostThreadMessageW( ... );
}

but when I lock like this

CMyWnd::MyFunction() {
theApp.m_uithread->PostThreadMessageW( ... );
m_myCEvent = new CEvent();
m_myCEvent->Lock();
}

it gets in a deadlock and the function in my ui thread that I called
via theApp.m_uithread->PostThreadMessageW stops running.

Is what I am calling a ui thread really a ui thread? Am I doing it
right? Is the window thread and the ui thread separate? thx
0
4/16/2008 1:35:07 AM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
754 Views

Similar Articles

[PageSpeed] 6

Looks like you're locking the main thread in MyFunction. I'd recommend 
checking out Joseph Newcomer's essays on threading 
http://www.flounder.com/mvp_tips.htm#hreads%20and%20Processes%20series.

"alexl" <alextheblade@gmail.com> wrote in message 
news:70b3690a-4005-49b4-a2bc-970c3b0e514d@m73g2000hsh.googlegroups.com...
> hi,
>
> I started my ui thread like this
>
> theApp.m_uithread=(CMyUIThread*)AfxBeginThread(RUNTIME_CLASS(CMyUIThread));
>
> and I have a window that talks to it like this
>
> CMyWnd::MyFunction() {
> theApp.m_uithread->PostThreadMessageW( ... );
> }
>
> but when I lock like this
>
> CMyWnd::MyFunction() {
> theApp.m_uithread->PostThreadMessageW( ... );
> m_myCEvent = new CEvent();
> m_myCEvent->Lock();
> }
>
> it gets in a deadlock and the function in my ui thread that I called
> via theApp.m_uithread->PostThreadMessageW stops running.
>
> Is what I am calling a ui thread really a ui thread? Am I doing it
> right? Is the window thread and the ui thread separate? thx 

0
drpazz (112)
4/16/2008 1:51:50 PM
On Tue, 15 Apr 2008 18:35:07 -0700 (PDT), alexl <alextheblade@gmail.com>
wrote:

>hi,
>
>I started my ui thread like this
>
>theApp.m_uithread=(CMyUIThread*)AfxBeginThread(RUNTIME_CLASS(CMyUIThread));
>
>and I have a window that talks to it like this
>
>CMyWnd::MyFunction() {
>theApp.m_uithread->PostThreadMessageW( ... );
>}
>
>but when I lock like this
>
>CMyWnd::MyFunction() {
>theApp.m_uithread->PostThreadMessageW( ... );
>m_myCEvent = new CEvent();
>m_myCEvent->Lock();
>}
>
>it gets in a deadlock and the function in my ui thread that I called
>via theApp.m_uithread->PostThreadMessageW stops running.

See the necessary conditions for deadlock here:

http://en.wikipedia.org/wiki/Deadlock

You've shown half the necessary conditions. The other half lies in the
target thread, in code you haven't shown.

A couple of things:

1. PostThreadMessage is problematic if the target thread creates windows,
directly or indirectly. For more, see:

PRB: PostThreadMessage Messages Lost When Posted to UI Thread
http://support.microsoft.com/kb/183116

2. I suppose the target thread is also using m_myCEvent. If so, you'd
appear to have a race condition, because the target thread may try to
access it before the posting thread creates it. And why are you creating it
dynamically?

>Is what I am calling a ui thread really a ui thread?

If it runs a message loop, it's an "UI thread". The form of AfxBeginThread
you're using indicates you're creating an UI thread.

>Am I doing it
>right? Is the window thread and the ui thread separate? thx

A window belongs to whichever thread created it. That is, if a window is
created by a thread X, only thread X can process messages for that window.
There are three ways for a window to process a message:

1. PostMessage posts a message to the owning thread's message queue, which
the thread later retrieves and dispatches.

2. Code running in the owning thread calls SendMessage on the window, which
amounts to an ordinary function call (intra-thread SendMessage).

3. Code running in a different thread calls SendMessage on the window
(inter-thread SendMessage). This causes the sending thread to block, and
the message isn't processed until the thread that owns the window calls a
function such as GetMessage, WaitMessage, and yes, even SendMessage, which
are considered "safe" points to perform a context switch so that the owning
thread can process the sent message. Only after the target thread returns
from its window procedure or calls ReplyMessage does the sender thread
resume execution. Careless use of inter-thread SendMessage can easily
result in deadlocks, but as you are using PostThreadMessage, it doesn't
apply to what you've shown.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
4/16/2008 5:31:44 PM
See below...
On Tue, 15 Apr 2008 18:35:07 -0700 (PDT), alexl <alextheblade@gmail.com> wrote:

>hi,
>
>I started my ui thread like this
>
>theApp.m_uithread=(CMyUIThread*)AfxBeginThread(RUNTIME_CLASS(CMyUIThread));
>
>and I have a window that talks to it like this
>
>CMyWnd::MyFunction() {
>theApp.m_uithread->PostThreadMessageW( ... );
>}
>
>but when I lock like this
>
>CMyWnd::MyFunction() {
>theApp.m_uithread->PostThreadMessageW( ... );
>m_myCEvent = new CEvent();
>m_myCEvent->Lock();
****
Since as far as I can tell, the event serves no useful purpose, get rid of the above two
lines.  You are creating a new event, blocking on it, but there seems to be no way to
release it.  Besides, ALL the MFC 'synchronization' primitives are complete crap, and
should be avoided.  I had problems like this, and it turned out that the code CAREFULLY
make sure that the SetEvent was never called, because the person who wrote the CEvent
apparently never heard of threads

As far as I can tell, there is absolutely no reason for locking anything, nor should there
be.  What do you think you are accomplishing by using this event.  There is ALWAYS a
better way to do it.
****
>}
>
>it gets in a deadlock and the function in my ui thread that I called
>via theApp.m_uithread->PostThreadMessageW stops running.
>
>Is what I am calling a ui thread really a ui thread? Am I doing it
>right? Is the window thread and the ui thread separate? thx
****
The problem is simply your choice of (a) using an event to lock anything (b) using a
CEvent for any purpose whatsoever.  I suspect you do not need an event, and as I said, all
of the MFC synchronization primitives are complete crap and must be avoided.
				joe
*****
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
4/20/2008 10:25:17 PM
Reply:

Similar Artilces:

Windows mail problem 07-16-10
Tried to log into my optonline account and received the following message: The connection to the server has failed. Account: 'mail.optonline.net', Server: 'mail.optonline.net', Protocol: POP3, Port: 995, Secure(SSL): Yes, Socket Error: 10060, Error Number: 0x800CCC0E Can anyone decipher this for me? What is the issue? You should not be using advanced ports or SSL, see http://optimum.custhelp.com/cgi-bin/optimum.cfg/php/enduser/std_adp.php?p_faqid=2397&p_created=1237397986&p_sid=ZbGnw*4k&p_accessibility=0&p_redirect=&p_lva=&p_sp=cF9zcmNoPSZ...

PostThreadMessage lost messages when thread is busy
Hi,all. I use the API function PostThreadMessage to put data to another thread.If the thread was not busy everything's ok.But if the thread was blocked for some reasons( for example: a modal dialog ), I can't get the posted message in thread message loop. What's wrong? Thanks for any help! Here is my ThreadProc code: UINT CMainFrame::DataThreadProc(LPVOID lpPara) { MSG msg; //force to create the thread's message queue PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); BOOL bRet; while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) ...

windows/system32/config/system file is huge
Hi, My windows/system32/config/system file is huge -- the order of 200mb. This seems a bit unusual, and I did get some popups on startup that there is not enough room for the registry. I cleared out the paging file and reallocated 1.5*2gb (the size of my memory). Anyway, what can I do about this file to make it "normal" again? I will state that I am after a repair install because something happened to my system and I needed to repair. Now things are indeed working alot better, but there is still this issue, and I guess this is why my system works slowly at times a...

Windows 2000 server user names
Where can I configure the user names formatting like you do in Recipient policies? yaro ...

Windows installer problem 04-08-10
Hi All, After login to windows xp, on screen displayed windows installer and nothing showing on screen .Any reply will be appreciated. Best regards Anji Always state your full Windows version (e.g., WinXP SP3; WinXP 64-bit SP2) when posting in a forum or newsgroup. Please do so in your next reply. Was the computer fully patched at Windows Update when this problem started? What anti-virus application or security suite is installed and is your subscription current? What anti-spyware applications (other than Defender)? What third-party firewall (if any)? Has a(another)...

Find/Replace command in Windows Live Mail
Does anyone know if the Find/Replace command is available in Live Mail. I cannot find it. If not, does anyone know of a suitable solution for that "will be missed" command. No, it's not. If you need to use Find/Replace, select all the text and 'cut' it. Then paste into Notepad where you can do the Find/Replace. When done, paste the result back into your email message. Gary VanderMolen, Microsoft MVP (Mail) "LouiseL" wrote in message news:8C2EDC90-74F5-4D16-948F-461EACB0F3C3@microsoft.com... Does anyone know if the Find/Replace command is ava...

Regsitry Key inaccessable in Windows 2003
Hi all, I cannt access a Registry Entry from windows 2003. Works fine with Windows 2000. I am accesing it from a Service in vc++. If i change the service Run as to Administrative Account it works, but fails in local account. No problem at all in Windows 2000. Any Clues??? Thanks and Regards, Abhinav Have you checked if you have the correct permissions to access that key using a local account on the 2003 box? - MR "Abhi" <bhagwatav@hotmail.com> wrote in message news:u0cjdDP4EHA.2608@TK2MSFTNGP10.phx.gbl... > Hi all, > > I cannt access a Registry Entry from win...

A question on __declspec(dllimport) and the Windows APIs.
In chapter 19 of Jeff's classic book "Programming Applications for Microsoft Windows", there is a paragraph as follows: "What Importing Really Means The previous section introduced the _ _declspec(dllimport) modifier. When you import a symbol, you do not have to use the _ _declspec(dllimport) keyword-you can simply use the standard C extern keyword. However, the compiler can produce slightly more efficient code if it knows ahead of time that the symbol you are referencing will be imported from a DLL's .lib file. So I highly recommend that you use the _ _declspec(dllimp...

Rules window disappears in less than 1 second
I just installed Outlook 2003 and now when I click on Rules the Rules dialog ox pops up for less that 1 second and then disappears not allowing any entry into it for editing and or setting up a rule. Any Ideas? ...

windows mail- vista, error with downloading attachments
When i click on the attachment it asks for my approval and then i click open. Then an error appears which at the frame of the window(above) says C:/Users...temporaryinternetfiles... ,and below an X with a message saying: This file does not have a program associated with it for performing this action. Create an association in the set associations control panel . What do i have to do? Regards, Antonis You have to, "Create an association in the set associations control panel." Go to Start - Default Programs - "Associate a File Type or Protocol with a Program&q...

Problem with Static splitter window...
Hi, Iam trying to create splitter windows. I need my window to be split into two rows. And then, each of the two rows must be further split into 2 columns. I need it precisely like this because I need the splitter bar in both rows to be separate and independent. Can somebody help me find what's the problem in the following code? It does not create the lower splitter. Any help will be greatly appreciated. Thanks, Shirley BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { int nWidth, nHeight; nWidth = ::GetSystemMetrics(SM_CXMAXIMIZED); nHei...

Business Portal - Employee Pay
This window does not auto-populated from existing employee data in Business Portal HRM:SS. The Employee Profile/Dependent/Benefits etc. windows all populate immediately but the W-4 window requires that a Modify transaction be made by the employee and approved by the supervisor before it will display data. This is contrary to all other windows and confuses the employees. It provides a first-impression that is not good for their experience with Business Portal. Mike Lupro -- Mike Lupro ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestion...

Does ShellExecute run in separate thread?
How can I wait for ShellExecute command. I believe that it spawns a thread. Is there any way to synchronize that? rgds subhash Use ShellExecuteEx() or CreateProcess() { +WaitForSingleObject() } if you want to wait on the process that it runs. -- Cheers Check Abdoul [VC++ MVP] ----------------------------------- "Subhash Badri" <subhash.badri@in.bosch.com> wrote in message news:bog6ug$9qv$1@ns2.fe.internet.bosch.com... > How can I wait for ShellExecute command. I believe that it spawns a thread. > Is there any way to synchronize that? > > rgds > subhash...

How to set Excel to open a new window for each application
When I choose a spreadsheet to open, I want it to open a new window. This used to happen but now opens in the same instance, just hiding my original spreadsheet. How to I get it back to previous settings? Tools-->Options, View tab. Check Windows in Taskbar. ************ Anne Troy www.OfficeArticles.com "Luke" <Luke@discussions.microsoft.com> wrote in message news:052E3C3A-8A1A-4EB4-BE2D-B6E09D4D247F@microsoft.com... > When I choose a spreadsheet to open, I want it to open a new window. This > used to happen but now opens in the same instance, just hiding my ori...

Windows 7 stuck in "Preparing to Hibernate"
How do I override "Preparing to Hibernate" or "Preparing to Shutdown" ? It's been in this mode for over six hours. Nothing removes the blue screen w/these words. Nothing to do with Word, but just keep the on/off switch pressed. -- Hope this helps, Doug Robbins - Word MVP Please reply only to the newsgroups unless you wish to obtain my services on a paid professional basis. "lsmo64081" <lsmo64081@discussions.microsoft.com> wrote in message news:61669E75-5C16-4D1B-B69A-89F293C9B0FF@microsoft.com... > How do I override "P...

Drawing the iconic window
Many years ago, it was possible to draw anything you wanted in the iconic window view. Today, it seems like the SHell has taken this over completely. I've got two problems: My app has no caption bar and no system menu, but I want to handle right-click on the taskbar icon. No messages appear to be generated to indicate any activity in the task bar icon. My app would like to custom-draw the iconic representation on the taskbar, such as drawing the icon and the caption. It does not appear this is possible. Note that the funny routine that MFC generates for dialog-based apps, that is suppose...

Windows Live mail takes much longer than ISP
My email service is through Sky.com and hotmail but Windows Live does not display mail for sometime an hour after it is in Sky mail. Why is this happening. It was not a problem in Outlook Express. What is you setting under Tools/Options... General tab... Send/Receive messages... Check for new messages every "xx" minutes... reset "xx" to "10" (default = 30). If you needs something quicker, you can manually sync. "Bryan1345" <Bryan1345@discussions.microsoft.com> wrote in message news:864941E6-EA56-434F-8DB0-5F3F2EF3AA4C@microsoft.com...

After I install a software in windows xp , how many changes have h
After I install a software in windows xp , how many changes have happen in registry? Because I need to read the value of registry ,please tell me all the changes in registry. I appreciate your help. -- jake After you install "a piece of software" in Windows, some number of registry keys, dependent entirely on whatever the person writing the installation decided should be modified, have been modified. In addition, once you run it, some other keys, defined by the person who coded it, and based on whatever actions the user has taken, will be changed. Each piece of software is ...

Restore Backup from Outlook 2003 into Windows 7(64)
Old laptop running XP; did backup of Outlook 2003 pst files using microsoft personal folders backup tool, to ext. hard drive. Connected hard drive to new laptop running Win 7(64). How do i restore the bu? All attempts met with error message saying I do not have authorization to access the file. Then tried to export the pst files from old laptop to ext hard drive. Connected hard drive to new laptop and tried to import the exported files into Outlook 2003. Got a similar message saying I did not have permission to access the selected files. Please help Bill. Submitted using http:/...

Using Windows Mail in Windows &
I have been using Windows Mail (copied from Vista) in my Windows 7 computer for nearly 6 months without any problems. Now, since Windows update KB978542 was installed a couple of days ago it has stopped working. It appears as a running program in Windows Task manager but the user interface never appears. I have done a System Restore to before KB978542 and Windows Mail started working OK again, Repeated this three time so I have no doubt that it is KB978542 that has buggered it up. Has anyone else using Windows Mail in Windows 7 had this experience? Are MS purposely disabli...

See this correction patch for Microsoft Windows
--hanoxjmvugy Content-Type: multipart/related; boundary="geygsediuj"; type="multipart/alternative" --geygsediuj Content-Type: multipart/alternative; boundary="orxbebvyw" --orxbebvyw Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Microsoft Customer this is the latest version of security update, the "October 2003, Cumulative Patch" update which fixes all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three new vulnerabilities. Install now to protect your computer. This ...

Windows defrag on exchange partition.
Hello, I am running win 2003 with exchange 2003. Is it safe and/or worthwhile doing a defrag on the partition that the exchange databases are located? thanks in advance... In my experence with 2k3, i havnt had any probelms. supprisingly but no real problems. i think its because (on my test box now) i have everything on one 40 gig logical drive (c:) with no RAID. Now, note: RAID may not play nice, i wouldnt try it on RAID until someone else who has done it says so... enjoy... Anthony "Simon" wrote: > Hello, > I am running win 2003 with exchange 2003. Is it safe and...

Problems with windows
I just got my computer cleaned up and downloaded some programs without having adobe back on and a window appeared and said "Windows cannot open this and gave yes and no options." I clicked "No." And a window opened and I moved it to Word and I can't open anything on windows any more. This comes up and I sure would like some wisdom on what to do. File Conversion: Select the encoding that makes your document readable Text encoding Windows default has a circle with a period in the center MS-DOS other en...

internet explorer mime type resubmit to window error
I have an web-app that can produce a receipt by submiting a html table into a new window and setting the mime type to Excel. This functionality works fine. If the user does not close this window and goes to create a new receipt then the excel plugin within explorer simply copies the data within the previous window into the new window and ignores the new data. Is there any solution to this problem? First, I don't know what I'm talking about. Second, it does sound like some kind of caching problem. Is there anyway you can force the web application to refresh the cached data? ...

Access Denied error while edit some of the GPOs in Windows 2003 AD
Hi We are unable to edit some of the GPOs (Default Domain Policy, etc) and getting Access Denied error. We checked the permission of SYSVOL folder and found Administrators (Domain), System and Authenticated Users have full control share permissions. Full access has been provided to Administrators, creator owner & System and read & execute permission has been provided to Authenticated users in Security tab. Can anyone help me to resolve the issue and also any doc is available to check the correct permissions with SYSVOL. Thanks in advance for help Regards Lal -...