I am trying to run a function or procedure in Delphi asynchronously, but without using a component, is there a way to do it with delphi core functions?
相关问题
- Is there a Delphi 5 component that can handle .png
- Is there a way to install Delphi 2010 on Windows 2
- Disable Browser onUnload on certain links?
- Is TWebBrowser dependant on IE version?
- iOS objective-c object: When to use release and wh
相关文章
- How to create a CFuncType in Python
- Change loss function dynamically during training i
- Best way to implement MVVM bindings (View <-> V
- With a Promise, why do browsers return a reject tw
- Asynchronous SHA256 Hashing
- Does aiohttp have ORM?
- Windows EventLog: How fast are operations with it?
- How to force Delphi compiler to display all hints
You may also want to execute your procedure on a thread. Use then the OnTerminate event to get the result. Yes, in these days of .NET and C# we are some kind of spoiled with the easy and convenient form of executing methods asynchronioulsly, but that's the way it works on Delphi.
If you are asking whether the VCL has something like BeginInvoke in .NET out-of-the-box, then the answer is no. However, you can get something quite similar in the form of a small unit that you link to your program, the AsyncCalls library by Andreas Hausladen. It's not a component, so I guess it qualifies. It also supports Delphi from version 5 onwards. Very much recommended.
Edit:
I'll add an example since you didn't get it running. If you get blocking in your calling code then your problem is that no reference is kept to the
IAsyncCall
interface pointer that the function returned. The object implementing the interface will therefore be destroyed immediately when the temporary reference goes out of scope. The destructor will be called in the context of the VCL thread, and it will callWaitForSingleObject()
or a similar function to wait for the worker thread to finish. The result of that is that your VCL thread blocks.You will get the correct behaviour if you maintain a reference to the interface pointer:
Set the timer to be disabled and let it have a very short
Interval
, say 50 ms. The button click starts the asynchronous operation:While the operation is active no other can be started. On completion it enables the timer to notify the form and reset the interface reference:
Note how it is even possible to access the form directly from the called method, by using
EnterMainThread()
andLeaveMainThread()
.Above code is not the absolute minimum, it is intended to demonstrate some ideas only.