THTTPApplication object is not responding UI lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Packs · 2023-06-26 18:25 I have create one windows gui application myhttpapp: THTTPApplication;     ( defined variable ) inside form create event created object myhttpapp := THTTPApplication.Create(FrmMarksheet);   myhttpapp.Port := 5008;   HTTPRouter.RegisterRoute('/time', @timeEndpoint);   myhttpapp.Threaded:= false ;   myhttpapp.Initialize;   myhttpapp.StopOnException:= true;   myhttpapp.Run;  this code is giving response , but my main window is not responding . what is the mistake ---------------------------------------------------------------------- delphius · 2023-06-26 18:40 Based on the code you provided, it seems that you are creating an instance of `THTTPApplication` and running it in your main window's create event. However, since `THTTPApplication.Run` is a blocking method, it will prevent the main window from responding to user interactions until the application is stopped or an exception occurs. To allow your main window to remain responsive while running the `THTTPApplication`, you can run it in a separate thread. Here's an example of how you can achieve this: 1. Create a new unit (e.g., `HttpServerThread.pas`) that will contain the code for running the HTTP server in a separate thread. 2. In this unit, define a new class that inherits from `TThread`, let's call it `THttpServerThread`. Override the `Execute` method to contain the code for initializing and running the `THTTPApplication`. Here's an example implementation: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit HttpServerThread; interface uses  Classes, HTTPApp; type  THttpServerThread = class(TThread)  private    myhttpapp: THTTPApplication;  protected    procedure Execute; override;  public  constructor Create;    destructor Destroy; override;  end; implementation uses  FrmMarksheet; // Make sure the unit containing your main window form is included here procedure THttpServerThread.Execute;begin  myhttpapp := THTTPApplication.Create(FrmMarksheet);  myhttpapp.Port := 5008;  HTTPRouter.RegisterRoute('/time', @timeEndpoint);  myhttpapp.Threaded := False;  myhttpapp.Initialize;  myhttpapp.StopOnException := True;  myhttpapp.Run;  FreeAndNil(myhttpapp);end; constructor THttpServerThread.Create;begin  inherited Create(False); // Create the thread in a suspended stateend; destructor THttpServerThread.Destroy;begin  Terminate; // Terminate the thread when it is being destroyed  inherited Destroy;end; end. 3. Modify your main window's code to create an instance of `THttpServerThread` and start it. This will run the HTTP server in a separate thread, allowing your main window to remain responsive. Here's an example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ..., HttpServerThread; ... procedure TForm1.FormCreate(Sender: TObject);begin  THttpServerThread.Create; // Create and start the HTTP server threadend; By running the HTTP server in a separate thread, your main window should remain responsive while the server is running and handling incoming requests. ---------------------------------------------------------------------- Packs · 2023-06-26 19:16 Thank So much for your quick response. It is working . I have following doubts . Why you want reference of mainform( frmmarksheet)inside execute method why you have used freeandnil . ---------------------------------------------------------------------- delphius · 2023-06-27 17:49 Quote from: Prakash on June 26, 2023, 09:16:53 pmWhy you want reference of mainform( frmmarksheet) The `THTTPApplication` class is part of the HTTPApp unit. It is responsible for handling HTTP requests and managing the HTTP server functionality. When you create an instance of `THTTPApplication`, it needs to be associated with a form or a component that will serve as the main application container. By passing `FrmMarksheet` as the parameter when creating `THTTPApplication`, you are associating the HTTP server with your main form. This allows the HTTP server to be aware of the main form's existence and interact with it if necessary. In some cases, the HTTP server may need to access or modify properties, components, or functionality provided by the main form. For example, it might need to display information or perform some action based on user input received through HTTP requests. By referencing the main form when creating `THTTPApplication`, you establish a connection between the HTTP server and the main form, enabling such interactions. In your specific code snippet, it seems like you want the HTTP server to be integrated into your main application window (`FrmMarksheet`). By associating `THTTPApplication` with `FrmMarksheet`, the HTTP server becomes part of your application, and any requests or responses it handles can be coordinated within the context of your main form. Overall, passing the main form as a parameter when creating `THTTPApplication` allows for seamless integration between the HTTP server functionality and the rest of your application, enabling communication and interaction between the server and your main form. And now, considering the above, and answering the essence of the question, in order to get access to FrmMarksheet, you need to connect a module with the form to uses, and it was added to the implementation section specifically so that the compiler does not swear at cyclic links between modules. Quote from: Prakash on June 26, 2023, 09:16:53 pminside execute method why you have used freeandnil . When you create an object using the `Create` operator, like in your case: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i myhttpapp := THTTPApplication.Create(FrmMarksheet); An instance of the `THTTPApplication` class is created in memory. The `myhttpapp` object references this instance, allowing you to access and manipulate it. However, when you're done with the `myhttpapp` object and want to free the memory it occupies, you need to properly release the object. This is done using the `Free` operator, which calls the object's destructor and frees the memory it occupies: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i myhttpapp.Free; However, when an object is freed using `Free`, the pointer to it still remains valid, meaning it still points to the deallocated object. This can lead to errors and unexpected behavior if you attempt to use the pointer after it has been freed. To avoid this problem, it's recommended to use `FreeAndNil`. This function does two things: 1. Frees the memory occupied by the object by calling `Free`. 2. Sets the pointer to `nil`, indicating that the object no longer exists. Therefore, calling `FreeAndNil(myhttpapp);` removes the `myhttpapp` object and ensures that the `myhttpapp` pointer no longer points to an invalid object. This prevents potential errors when trying to use the object after it has been freed. It's important to note that using `FreeAndNil` is not mandatory, but it's good practice to prevent potential memory and pointer issues.