This is a continuation of a previous blog post, Are const parameters dangerous? Basically, adding an out parameter into the mix does not change anything that has been said in the previous post about using const parameters. But out parameters, with their own side-effects, create a specific situation that requires additional explanation. First, let's see how an out parameter works. An out parameter is a var parameter with a twist. Just like a var parameter passes the original variable as a reference (pointer), so does the out parameter, but out also tells the compiler that the original value can be discarded, since it is not for passing any input data to the procedure, and will be only used for passing the result out of the procedure. For managed types, the compiler will clear the original contents of the out parameter at the call site. procedure OutInt(out p: integer); begin p := 5; end; procedure OutString(out p: string); begin p := '5'; end; procedure T
Showing posts from March, 2021
- Other Apps
"Embarcadero Technologies - Conversation with Dalija Prasnikar, Embarcadero MVP, on covering her new book on Delphi Event-based and Asynchronous Programming." Book intro by Neven Prasnikar Jr. and a recording of live Q&A chat hosted by Jim McKeeth and Stephen Ball. https://www.youtube.com/watch?v=C8clqgydTsM Making this webinar has been quite eventful The whole experience can be best summarized with: The year is 2021. After a whole year of lockdowns and working from home, cameras have decided they have had enough of it. Mics didn't join the rebellion Thank you all for joining live session and watching!
- Other Apps
Running long tasks in a background thread to keep the UI responsive is one of the main purposes of multithreading. A common code pattern for doing so would look like: procedure TMainForm.BtnClick(Sender: TObject); begin TThread.CreateAnonymousThread( procedure begin DoSomeWork; end; end).Start; end; However, such code often needs to show the results of that long work to the user. Working with the GUI from a background thread is not thread-safe, so such code should be executed in the context of the main thread: procedure TMainForm.BtnClick(Sender: TObject); begin TThread.CreateAnonymousThread( procedure begin DoSomeWork; TThread.Synchronize(nil, procedure begin ShowResults; end); end; end).Start; end; Because ShowResults will run in the context of the main thread, the code in ShowResults should be as minimal as possible—read: as fast as possible. Once you enter into the Synchronize (or Que