Recent

Author Topic: THTTPApplication object is not responding UI  (Read 1023 times)

Packs

  • Sr. Member
  • ****
  • Posts: 496
THTTPApplication object is not responding UI
« on: June 26, 2023, 08:25:32 pm »
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

  • Jr. Member
  • **
  • Posts: 83
Re: THTTPApplication object is not responding UI
« Reply #1 on: June 26, 2023, 08:40:38 pm »
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][+][-]
  1. unit HttpServerThread;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, HTTPApp;
  7.  
  8. type
  9.   THttpServerThread = class(TThread)
  10.   private
  11.     myhttpapp: THTTPApplication;
  12.   protected
  13.     procedure Execute; override;
  14.   public
  15.     constructor Create;
  16.     destructor Destroy; override;
  17.   end;
  18.  
  19. implementation
  20.  
  21. uses
  22.   FrmMarksheet; // Make sure the unit containing your main window form is included here
  23.  
  24. procedure THttpServerThread.Execute;
  25. begin
  26.   myhttpapp := THTTPApplication.Create(FrmMarksheet);
  27.   myhttpapp.Port := 5008;
  28.   HTTPRouter.RegisterRoute('/time', @timeEndpoint);
  29.   myhttpapp.Threaded := False;
  30.   myhttpapp.Initialize;
  31.   myhttpapp.StopOnException := True;
  32.   myhttpapp.Run;
  33.   FreeAndNil(myhttpapp);
  34. end;
  35.  
  36. constructor THttpServerThread.Create;
  37. begin
  38.   inherited Create(False); // Create the thread in a suspended state
  39. end;
  40.  
  41. destructor THttpServerThread.Destroy;
  42. begin
  43.   Terminate; // Terminate the thread when it is being destroyed
  44.   inherited Destroy;
  45. end;
  46.  
  47. 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][+][-]
  1. uses
  2.   ..., HttpServerThread;
  3.  
  4. ...
  5.  
  6. procedure TForm1.FormCreate(Sender: TObject);
  7. begin
  8.   THttpServerThread.Create; // Create and start the HTTP server thread
  9. end;


By running the HTTP server in a separate thread, your main window should remain responsive while the server is running and handling incoming requests.
fpmtls - ssl/tls 1.3 implementation in pure pascal
fpmailsend - sending a simple email message
pascal-webui - use web browser as gui and fpc as backend

Packs

  • Sr. Member
  • ****
  • Posts: 496
Re: THTTPApplication object is not responding UI
« Reply #2 on: June 26, 2023, 09:16:53 pm »
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

  • Jr. Member
  • **
  • Posts: 83
Re: THTTPApplication object is not responding UI
« Reply #3 on: June 27, 2023, 07:49:17 pm »
Why 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.

inside execute method why you have used freeandnil .

When you create an object using the `Create` operator, like in your case:


Code: Pascal  [Select][+][-]
  1. 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][+][-]
  1. 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.
« Last Edit: June 27, 2023, 07:58:17 pm by delphius »
fpmtls - ssl/tls 1.3 implementation in pure pascal
fpmailsend - sending a simple email message
pascal-webui - use web browser as gui and fpc as backend

 

TinyPortal © 2005-2018