Advertisement

03.06.2008 at 08:15AM PST, ID: 23220045
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Aplpication.ProcessMessages within a Thread

Tags: Delphi 2006 (Win32)
Does anyone know if it's safe to call Application.ProcessMessages within a thread?

I'm developing a threaded application that uses some library code. I've noticed that deep within this there is a call to the above. Just wondered if this was safe and if not what the thread-safe equivalent would be.

Thanks

Steve
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: steve-west
Solution Provided By: ciuly
Participating Experts: 2
Solution Grade: A
Views: 8
Translate:
Loading Advertisement...
03.06.2008 at 08:24AM PST, ID: 21061828

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 08:43AM PST, ID: 21062095

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 10:19AM PST, ID: 21063145

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 10:40AM PST, ID: 21063311

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.06.2008 at 03:06PM PST, ID: 21065722

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 12:05AM PST, ID: 21068184

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 12:13AM PST, ID: 21068218

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 01:11AM PST, ID: 21068427

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.06.2008 at 08:24AM PST, ID: 21061828

Rank: Wizard

calling Application.ProcessMessages from any other thread than main thread does not make sense.
If the main thread is blocked (and thus not processing messages), you should call Application.ProcessMessages() at the place main thread is blocked, this way you don't have any threading issues.
 
03.06.2008 at 08:43AM PST, ID: 21062095
But this is within library code - I don't feel comfortable simply removing this just to suit my own requirements.

Although "it doesn't make sense", can any harm come from executing this code from within a thread?
 
03.06.2008 at 10:19AM PST, ID: 21063145

Rank: Wizard

so you are using a library which is blocking?
In that case, use the library from a separate thread.

I'm not completely sure, but I suspect that calling Application.ProcessMessages() from a non-main thread is asking for trouble.
 
03.06.2008 at 10:40AM PST, ID: 21063311

Rank: Sage

>> can any harm come from executing this code from within a thread?

yes, and lots of it. Golden rule of delphi threading: NEVER EVER use VCL stuff from a thread ouside of a synchronize call.

the application object is VCL. so if you reall-really want to call applicaiton.processmessages (and keep in mind, that the application object does not always reffer to the exe/host application and in some circumstances it is invalid to use it !!! (well, delphi help jsut notes on some of the stuss to not use it in non-delphi exe, but it doesn not say what happens if you do...)), then you would create a method in your thread like:

procedure TThread1.procmsg;
begin
  application.processmessages;
end'

and replace that call you found with
synchronize(procmsg);

but for petes sake, I cannot understand how some people can think of calling application.processmessages from a thread. if somebody even considers that, obviously, that somebody has no idea what threads are and how they function.
if the thread blocks, the application does NOT block. they run separatly. there is absolutely no need to call application.processmessages from a thread. none whatsoever.
whomever wrote that obviously has serious lacks in delphi threading, but I am repeating myself :D
Accepted Solution
 
03.06.2008 at 03:06PM PST, ID: 21065722

Rank: Wizard

> there is absolutely no need to call application.processmessages from a thread. none whatsoever.

unless you want to try to keep the mainthread 'running' when it's actually blocked from a separate thread. I think that's what steve-west is trying to achieve.
Like I said, put the blocking stuff in a separate thread in stead of trying to keep the mainthread processing messages from a separate thread.
 
03.07.2008 at 12:05AM PST, ID: 21068184

Rank: Sage

>. unless you want to try to keep the mainthread 'running' when it's actually blocked from a separate thread.

and how can that be done?

I can only think of (I just woke up :D) one "correct" way: thread.waitfor (or anything equivalent like while not thread.terminated do sleep(); (hint: TThread=class(classes.TThread); public property Terminated; end; )) or waitforsingleobject(thread.handle,infinite); etc) but this is a bug and in this case the fix is to call the processmessages in the mainthread and mofidy the waiting accordingly.

there is another way, deadlock, but that is a definite bug and using application.processmessages won't help anyway, unless the lock is done via windows messaging (like from the thread lock a resource, you then call sendmessage and the mainthread (in the message handler) also locks that resource with the same lock and tries to do something)
but these are all bugs na dthey don't require application.processmessages.

any other scenario you can think of when application.processmessages needs to be called from a thread and it's not because of a bug?
 
03.07.2008 at 12:13AM PST, ID: 21068218

Rank: Wizard

>> unless you want to try to keep the mainthread 'running' when it's actually blocked from a separate thread.

>and how can that be done?

Maybe I wasn't clear. I absolutely disagree with this approach, it's bullocks :)
I only tried to say that I think that this is what motivates steve-west to opt this approach.

bottomline: If don't want to block the main thread while running blocking (or sync) code, don't do it from the main thread. Do it from a separate thread.
Assisted Solution
 
03.07.2008 at 01:11AM PST, ID: 21068427
Thanks guys for these points, very interesting.

In the threaded application I'm writing, I'm having to make use of - within each thread - a component which has been developed by a predecessor of mine and which is scattered throughout all our developments (threaded and non-threaded).

While investigating a few issues that had cropped up, I was inspecting the inherited ancestry of this component. I noticed deep within a base ancestor a couple of references to "application.processmessages".

This did indeed raise eyebrows - the component was, in my particular case, running within a thread. I'm aware of synchronisation issues and when methods should be synchronised, but to be honest I was not sure of the implications of executing the processmessages within a thread. I can see how this can be undesirable - if you have a 100 threads, you don't each to instruct the application to process messages...,.

We have various threaded applications making use of this component, some of which run continuously processing a serious amount of data. So far, none have fallen foul of this code (although I don't think anyone has actually looked into this at the same depth that I have), so I was curious - given the intense activity of these applications without any apparent side effects, if it really was such a bad thing.

Anyway, my question has been answered - as Ciuly pointed out, Application is still VCL and as such, should never be referenced within a thread.

Thanks to you both.

Steve

 
 
03.07.2008 at 08:34AM PST, ID: 21071810
>. Application is still VCL and as such, should never be referenced within a thread.
outside the synchronize call ;) and you can reference it, it's the methods that usually cause problems (executing code). reading properties is ok (in case of TApplication). writing them is probably problematic in some cases.
basically, you just need to pay attention to what you're doing and with what.
just because everything seems to work fine it doesn't mean that the bug is not there. since the call is somewhere deep, you might not get to execute that but in rare cases.

I just made a quick research into this and in case of application.processmessages it's like this:

http://msdn2.microsoft.com/en-us/library/ms644943(VS.85).aspx
"If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed."

so, calling application.processmessages which in turn will call peekmessage will process the messages belonging to the current thread, which is either the mainvcl thread or the thread from which it is called.

basically, it does nothing, unless the thread owns some windows (which I doubt) or has some messages in it's queue (which again, I doubt) (I am assuming that it's all TThread, which doesn't have neither of them)

you should also check if the application.processmesseges is not called in an event handler. because if it is and the event is fired in the vcl thread context, thenit makes sense. so don't go running commentign it out, first try to understand why it is there ;)
 
 
03.07.2008 at 09:02AM PST, ID: 21072149
Ciuly - thanks

The problem I've found is that the ICS WSocket DOES create a hidden window and inserts into the WNClass structure of this window its own message processing function. THis is causing problems freeing the threads (and is related to another open question I have) - when a thread is freed (and therefore the socket) alt-tabbing or changing focus to another application causes my application to immediately crash. I think it's redirecting message to an invalid window...., something I've yet to solve.

 
 
03.07.2008 at 09:43AM PST, ID: 21072542
I don't know about ICS. I dropped that quite a few years back in favor of indy :)

you could do the same, too :D
 
 
03.07.2008 at 11:11AM PST, ID: 21073304
I've used both ICS and Indy a lot. I personally prefer ICS because it's async.
Never had any problems with ICS in a thread though.
 
 
03.08.2008 at 11:28AM PST, ID: 21078271
MerlinB

If you could explain why, when I free an ICS socket component within a thread, my application crashes when focus is removed from it, you'll be, well, you'll be - I'll tell you what you'll be - you'll be the godfather to my brothers children when he decides to have some.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23220172.html

It's definitely the socket causing this, and the socket's creation (for some reason) of the hidden window. I really don't know how to solve it.

Regards


Steve
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628