CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 52
  1. #16
    Join Date
    Feb 2015
    Posts
    66

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by 2kaud View Post
    If a thread is sending a message to a window created by another thread, the sending thread is first suspended. The sent message is appended to the receiving thread's send-message queue. If the receiving thread is already executing code and isn't waiting for messages then the sent message can't be processed as the system won't interrupt the thread to process the message immediately. When the receiving thread has finished processing its current message and is ready to process a message, it first checks if there is at least one message in the send-message queue and if there is it processes the first sent message and removes it from the queue. This means that it is possible that several sent messages could pile up in the queue. Until a particular send message is processed, the thread that issued it stays suspended. This procedure means that waiting messages sent using SendMessage() are processed before any waiting PostMessage() messages. So if PostMessage() and SendMessage() are both used, the actual execution order can't be determined. This method of handling inter-thread messages can cause the issuing thread to hang.

    That's why Microsoft has SendMessageTimeout(), SendMessageCallback(), SendNotifyMessage() and ReplyMessage(). See MSDN for further details.

    There's also the issue that several messages to controls return an index (or position) or the number of items or some other control information. Depending upon what is happening with other threads this returned info may not be valid when it comes to be used as another thread may have issued a message to update a control in some way. (eg a control find returns a list position but in the meantime another thread may have added/deleted an item so the find position is no longer valid when used). This, of course, also applies to PostMessage().

    Also note that macros use SendMessage (eg ListView_GetItemText() etc) and I don't advise mixing SendMessage() and PostMessage() as above waiting SendMessage() is prioritised over waiting PostMessage().
    Messages sent using PostMessage() are placed in the thread message queue. However, messages sent using SendMessage() are placed in a queue that we don't know about (it is not documented). Is this correct?

  2. #17
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by john_n View Post
    Could you tell me how to do the following without using SendMessage():

    Code:
    /* Thread A */
    
    // Get content of hEdit into str
    // DeleteFile(str)
    If I use PostMessage() (which is asynchronous), then I could reach DeleteFile(str) before the content of hEdit are placed into str.
    If you use PostMessage, nothing prevents you from deleting the file in GUI thread context, i.e. the pseudo-code is to be executed in GUI thread instead of your worker. Is there anything that makes you delete the file strictly in worker thread?
    Best regards,
    Igor

  3. #18
    Join Date
    Feb 2015
    Posts
    66

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by Igor Vartanov View Post
    If you use PostMessage, nothing prevents you from deleting the file in GUI thread context, i.e. the pseudo-code is to be executed in GUI thread instead of your worker. Is there anything that makes you delete the file strictly in worker thread?
    Well, because this is a simplified scenario of why I need to make sure that PostMessage() does not return until I get the UI control content. But my real scenario is many lines of code long that will broke my program if not kept in the worker thread.

  4. #19
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by john_n View Post
    Messages sent using PostMessage() are placed in the thread message queue. However, messages sent using SendMessage() are placed in a queue that we don't know about (it is not documented). Is this correct?
    nope
    for all intents and purposes... If you put a SendMessage() that is pretty much 'hardwired' to directly call into the windows procedure without any sort of queueing. The real story is a bit more involved, but from a application p.o.v. that is how you should view it.

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to get the text of an "EDIT" control from another thread?

    See GetQueueStatus() https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx There is also the thread's virtualized input queue.

    GetMessage(), PeekMessage() etc decides which message is to be extracted next. This extraction order is
    sendmessage
    postmessage
    quit
    input
    paint
    timer

    For more detail see Programming Applications for Microsoft Windows Jeffrey Richter
    http://www.amazon.co.uk/Programming-...rosoft+windows
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #21
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to get the text of an "EDIT" control from another thread?

    If you put a SendMessage() that is pretty much 'hardwired' to directly call into the windows procedure without any sort of queueing. The real story is a bit more involved, but from a application p.o.v. that is how you should view it.
    Yes for within the same thread, but my understanding for inter-threads is as post #15?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by Igor Vartanov View Post
    A real experience is a great thing. A much better thing is explaining the case and revealing its root cause. Instead of just hearing "don't do that", it would be better to hear "don't do that because of this, this and this". This would let the questioner judge himself if to follow your advise or not.
    If someone asks to use GetWindowText() from a thread, that typically means it's about getting text that will change during the lifetime of the worker thread. Because if it wouldn't be changing, you can just pass the text along with the window as you create the thread.

    Now... Guess what happens when you call GetWindowText() from your worker thread at the precise moment your GUI thread (or another thread for that matter) is in the process of doing a SetWindowText()...

    One could easily assume that microsoft has been smart enough to properly synchronise all of their API's to deal with that, and you would be mistaken. for performance reasons they don't do that and simply go by the assumption that you are following the basic premise of not accesing the UI other than from the thread that created the window.

    Now, granted, the chances that you actually do get into the very small fail oportunity where thread A and thread B are just at the right point of the code to make things go bonkers aren't large, which is why "this usually works".

    And then you see it fail... horribly... and fairly consistently... at the moment the app is being demo'd to a really really important client and a big sale is dependant on this... and you're on holiday... to Hawaii... and they make you come back to fix this ASAP because this is legacy code nobody else in the company understands but you and they need to get this fixed before the weekend.

    Now also, granted, microsoft has been adding various bits of code to prevent things like this, but they're undocumented, they don't work on all platforms. So MAYBE in current versions this particular case is fixed, I don't know, you don't know. All I know is that it definately was in there in an older version of windows, and they did make me come back to fix it asap (ok, I wasn't in Hawaii, but I could have been :-))

  8. #23
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to get the text of an "EDIT" control from another thread?

    Okay, you say GetWindowText may conflict with SetWindowText, and I say it won't due to those being implemented atomic. The "microsoft smart enough" stuff, etc. What's next? And please save on touching Hawaii stories, let's go technical. A real story when everything went wrong because of sending message, I would appreciate one. What was wrong and how it was fixed?
    Last edited by Igor Vartanov; April 14th, 2015 at 04:21 PM.
    Best regards,
    Igor

  9. #24
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by john_n View Post
    Well, because this is a simplified scenario of why I need to make sure that PostMessage() does not return until I get the UI control content. But my real scenario is many lines of code long that will broke my program if not kept in the worker thread.
    The very fact that you use the path for file deletion obtained directly from UI control indicates some flaw in operation logic design. (For example, on edit control content change you might test the string for being a real file path, and in case it is, you put the path to some storage secure for multi-thread access, e.g. database, or maybe something more lightweight. This would entirely eliminate the original issue of accessing UI from worker thread and added some extra safety.)

    I suppose your problem is not merely "to use or not to use" SendMessage. So please do not expect a simple answer to your real problem. Complex problems not always solved simple way, almost never.

    In case you want to discuss your real situation here, you might start with explaining in draft your "real scenario in many lines of code to be kept in the worker thread" and why it to wait for PostMessage handling completion.

    PS. When GUI is involved, the code logic stops being linear very often. And you should be ready to bend your program seriously, or even break it to loosely coupled pieces.
    Best regards,
    Igor

  10. #25
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by OReubens View Post
    Now... Guess what happens when you call GetWindowText() from your worker thread at the precise moment your GUI thread (or another thread for that matter) is in the process of doing a SetWindowText()...

    One could easily assume that microsoft has been smart enough to properly synchronise all of their API's to deal with that, and you would be mistaken. for performance reasons they don't do that and simply go by the assumption that you are following the basic premise of not accesing the UI other than from the thread that created the window.
    There is no such assumption, otherwise this would make entire Windows GUI multi-thread awareness completely senseless. So I don't take this serious. In case something fails somehow in some Windows version, this is a bug of Windows, and Windows guys need to take care of that. Programmers must follow documentation and guidelines from their software vendors and don't take responsibility for what they do not control.

    PS. In one of my projects I dealt with Windows Media Player control embedded in web page and communicating with intranet web server providing WMV pseudo-streaming. One customer experienced a strange issue when playback is down in a second, and the workstation requires for full restart. It turned out, there was a bug in TCP stack going clinch during communication between sub-nets with different MTU size. The problem was discovered for only single Windows Server platform. MS guys issued the patch very soon that fixed the situation completely. Following your logic, I would advise customer to ditch the product, or what?
    Last edited by Igor Vartanov; April 15th, 2015 at 06:59 AM.
    Best regards,
    Igor

  11. #26
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by Igor Vartanov View Post
    Okay, you say GetWindowText may conflict with SetWindowText, and I say it won't due to those being implemented atomic. The "microsoft smart enough" stuff, etc. What's next? And please save on touching Hawaii stories, let's go technical. A real story when everything went wrong because of sending message, I would appreciate one. What was wrong and how it was fixed?
    Exactly like I posted

    Gui thread was updating controls, some automatically, some on user input
    worker thread reads the controls, does some work.
    Worked fine for quite a long time, until it didn't. And didn't fairly consistently on the test machine at the customer.

    changing the worker thread to no longer read the window but retrieve the data rather than the control contents solved everything.

  12. #27
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by john_n View Post
    Could you tell me how to do the following without using SendMessage():

    Code:
    /* Thread A */
    
    // Get content of hEdit into str
    // DeleteFile(str)
    If I use PostMessage() (which is asynchronous), then I could reach DeleteFile(str) before the content of hEdit are placed into str.
    Missed this before.

    1)
    have the worker thread post a message requesting info.
    gui thread retrieves the info and posts this back to the worker.
    There's various ways you could do this, depending on what sort of loop the worker thread is built around.
    A simple way is posting a info request along with an event handle
    waiting on the event, and expecting the ui thread to signal the event when it's made the data available. The worker thread waiting could be an issue, again, there's ways to deal with that.

    If the worker thread has a messageloop, or some other form of "jobs to do queue", simply post the message or the "job is ready to be processed, here is your info"

    2)
    Have accessible variables that are copies of what is on the control. (they'll need synchronisation ofc).
    for an editcontrol, handle the EN_CHANGED, read the control contents and store in variable. (or read it when the edit looses focus or on some other moment when you know the filename is complete).
    for controls where you decide the contents (such as statics), you probably format the contents into a string before anyway, so simply keep the string handy.
    Rather than reading the UI, the worker thread can now access the variables.



    Yes, those methods take some extra work, but you'll save yourself a lot of headaches down the road by doing things "right".

  13. #28
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by OReubens View Post
    ...
    If the worker thread has a messageloop, or some other form of "jobs to do queue", simply post the message or the "job is ready to be processed, here is your info"
    You probably meant "post the thread message" (using PostThreadMessage function)?
    Victor Nijegorodov

  14. #29
    Join Date
    Feb 2015
    Posts
    66

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by Igor Vartanov View Post
    The very fact that you use the path for file deletion obtained directly from UI control indicates some flaw in operation logic design. (For example, on edit control content change you might test the string for being a real file path, and in case it is, you put the path to some storage secure for multi-thread access, e.g. database, or maybe something more lightweight. This would entirely eliminate the original issue of accessing UI from worker thread and added some extra safety.)

    I suppose your problem is not merely "to use or not to use" SendMessage. So please do not expect a simple answer to your real problem. Complex problems not always solved simple way, almost never.

    In case you want to discuss your real situation here, you might start with explaining in draft your "real scenario in many lines of code to be kept in the worker thread" and why it to wait for PostMessage handling completion.

    PS. When GUI is involved, the code logic stops being linear very often. And you should be ready to bend your program seriously, or even break it to loosely coupled pieces.
    The basic idea is that I need to make sure that the code that write to or read from the UI controls has done its job before continuing with the rest of the code, the same way as if I only have a UI thread in my program and the code is executed in it.

    Anyway, I have decided to use SendMessage(), so my problem is solved (I have to pay attention not to cause a deadlock though!).

  15. #30
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to get the text of an "EDIT" control from another thread?

    Quote Originally Posted by john_n View Post
    The basic idea is that I need to make sure that the code that write to or read from the UI controls has done its job before continuing with the rest of the code,
    This is what I told you about: the design issue. Worker thread, especially multiple threads, extensively communicating with GUI elements is prone to cause troubles during product maintenance. Not because of possible deadlocks but operation logic degradation/corruption.

    A simple thought: UI is for visualization. When you write to a control, somewhere you already have the data ready for worker thread as well. When user updates the UI content, you are immediately notified about the updates and surely able to put the update right away into some data storage available for worker thread. Worker thread extensively communicating with GUI is a definite sign of bad design.

    the same way as if I only have a UI thread in my program and the code is executed in it.
    This is where I would suggest to think about moving your logic to state machine(s).
    Best regards,
    Igor

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured