Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Realtime Input? Thread? (https://www.delphipraxis.net/69337-realtime-input-thread.html)

sk.Silvia 13. Mai 2006 19:01


Realtime Input? Thread?
 
hi there, lets say i have this simple code

i:=0;
repeat
i:=i+1;
until i=10;

and i will enter numbers from a edit component - i write a number to edit and press the button, then the cycle continues

like :

Delphi-Quellcode:
i:=0;
repeat
  Memo1.Lines.Append('Enter field values '+IntToStr(i));
  P[i]:=%MyInput%; //Edit1.text;
  i:=i+1;
until i=10;
and it will looks like

> program starts
> it displays -> Enter field values 1
> then i write the values to an edit component and press the ok button //smt like the program stops and w8 for input from anothe component
> then again it displays -> Enter field values 2
> then again enter the numbers to edit component and click ok

it looks simple, but i have no clue how to do this, how to stop a procedure at a certain point, work with another procedure and the result inputs and continue from the neeede point

maybe to give the procedure to a threat, but thats also :/ for me

thanks

C.Schoch 13. Mai 2006 19:34

Re: Realtime Input? Thread?
 
why you dont try

Delphi-Quellcode:
i:=0;
repeat
  inputbox('Enter Number', ''Enter field values '+IntToStr(i)', '') // here i changed
  P[i]:=%MyInput%; //Edit1.text;
  i:=i+1;
until i=10;
?
The code waits while the Inputbox is diplayed.

Or don`t I understand the question?

sk.Silvia 13. Mai 2006 21:44

Re: Realtime Input? Thread?
 
ok thanks, thats it, but can i make my custom input box?

C.Schoch 13. Mai 2006 22:09

Re: Realtime Input? Thread?
 
Liste der Anhänge anzeigen (Anzahl: 1)
Yes you can. go to File - New - (in german version "Weitere") the rest is shown in the screenshot of course you can use the red marked.

you can add buttons, fields ... .
But you have to write every funktion by own hand.
like this:
Delphi-Quellcode:
procedure TMainForm.pmAddClick(Sender: TObject);
// Job hinzufügen
begin
  // here you can set standart values if you need
  TEditChange.ShowModal;
  if not (TEditChange.ModalResult = mrCancel) then // maybe use mrOK and leave "not"
    begin
      // do sometheng whith the Input Data
      lvSourceDestination.Items.Add;
      lvSourceDestination.Items[lvSourceDestination.Items.Count-1].SubItems.clear;
      lvSourceDestination.Items[lvSourceDestination.Items.Count-1].SubItems.add(TEditChange.eSourcePath.Text);
      lvSourceDestination.Items[lvSourceDestination.Items.Count-1].SubItems.add(TEditChange.eDestinationPath.Text);
      lvSourceDestination.Items[lvSourceDestination.Items.Count-1].SubItems.add(TEditChange.eApplicationPath.Text);
      // reset the edit fields, if you don`t do the old is shown at next show
      TEditChange.eSourcePath.Text := '';
      TEditChange.eDestinationPath.Text := '';
      TEditChange.eApplicationPath.Text := '';
    end
end;
edit: In D7 it`s in File - New - (in german version "Weitere") in the register "Dialogs"

sk.Silvia 14. Mai 2006 09:43

Re: Realtime Input? Thread?
 
but thats not exactly what i wanted... i will make smt like this // StopProcedure(Haf); and ContinueProcedure(Haf); this procedures dont exist, but i will make smt like this

Delphi-Quellcode:
procedure Haf;
var i:integer;
 begin
 i:=0;
 repeat
  StopProcedure(Haf); //this stops the procedure, but local variables, nothing will be lost, smt like it freezes the procedure for a            while
  input[i]:=Edit1.text;
  i:=i+1;
 until i=3;
 end;

//we can work with the program and when we press Button, Haf will contiue
procedure ButtonClick;
 begin
 ContinueProcedure(Haf);
 end;
i hope its a bit clearer now :-D

C.Schoch 14. Mai 2006 12:16

Re: Realtime Input? Thread?
 
You can do this with WaitForSingleObject and an Event

Your function
Delphi-Quellcode:
procedure Haf;
var i:integer;
begin
i:=0;
repeat
//////Wait///////////////////////////////////
repeat
  WaitResult := DWORD WaitForSingleObject(HANDLE hHandle, { handle of object to wait for} DWORD dwMilliseconds    { time-out interval in milliseconds}); // for Handele use "event" and you must set an timeout that the funktion returns and Messages are viewed and worked on
  if WaitResult <> WAIT_OBJECT_0 then
  begin
    Application.ProcessMessages;
  end;
until WaitResult = WAIT_OBJECT_0;
/////////////////////////////////////////////
// here you can use reset event if you want to wait contiously
  input[i]:=Edit1.text;
  i:=i+1;
until i=3;
end;
Create the event:
Delphi-Quellcode:
procedure TForm1.FormShow(Sender: TObject);
begin
event := HANDLE CreateEvent(

    LPSECURITY_ATTRIBUTES lpEventAttributes,   // pointer to security attributes
    BOOL bManualReset,   // flag for manual-reset event
    BOOL bInitialState,   // flag for initial state
    LPCTSTR lpName    // pointer to event-object name
   );
end;
Set the event:
Delphi-Quellcode:
procedure ButtonClick;
begin
SetEvent(event)
end;
Close the event:
Delphi-Quellcode:
procedure TForm1.FormClose(Sender: TObject);
begin
 CloseHandle(event)
end;
Now a lot of Pseudocode (not tested)
for additional informatin wiew the SDK.

Olli 14. Mai 2006 12:23

Re: Realtime Input? Thread?
 
If it runs in a separate thread MSDN-Library durchsuchenSuspendThread and MSDN-Library durchsuchenResumeThread might be exactly what you are searching for.

However, if you suspend the thread from the outside, the context is undetermined. So if you want the thread to be suspended at a certain position, it may suspend itself using SuspendThread and be woken up from another thread using ResumeThread.

Note: if you have the usual Delphi-like architecture of an application, there is only one main thread with a message queue. So to achieve the above you need to have a secondary thread at least. And be sure to sync them because the VCL does not like threads too much ;)

DGL-luke 14. Mai 2006 12:31

Re: Realtime Input? Thread?
 
So you have like 10 input params you want to fetch from the user?

Why not just make a second form:

Delphi-Quellcode:
for i := 0 to 9 do
  if form2.ShowModal = mrOK then
    begin
      values[i] := form2.some_public_field_assigned_before_closing;
    end
  else
    begin
      ShowMessage('Naaah... don''t press cancel!'); // ;-)
      Break;
    end;
EDIT: if you don't know how to make a form act modal, there should be enough material in the forum. or just ask ;)

C.Schoch 14. Mai 2006 13:06

Re: Realtime Input? Thread?
 
Liste der Anhänge anzeigen (Anzahl: 1)
@Olli the mainform is a thread too, and bacause of this you can use the wait function too.
Here i work with events and thay you can set from everywhere in the programm.

I made a little test program:

sk.Silvia 14. Mai 2006 13:11

Re: Realtime Input? Thread?
 
thanks for your help, threat are looking good, but what am i making wrong, why does it do - nothing - ?

Delphi-Quellcode:
var fiber:TThread;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
  begin
  fiber:=TTHread.Create(false);
  //fiber.Synchronize(???);
  i:=0;
  repeat
    Memo1.Lines.Append(IntToStr(i));
    fiber.Suspend;
    i:=i+1;
  until i=3;
  fiber.Terminate;
  end;

procedure TForm1.Button2Click(Sender: TObject);
  begin
  fiber.Resume;
  end;

C.Schoch 14. Mai 2006 13:17

Re: Realtime Input? Thread?
 
You have to add a Thread-Objekt to your programm and fill it with code

edit:
or view my test programm

Olli 14. Mai 2006 13:26

Re: Realtime Input? Thread?
 
Zitat:

Zitat von C.Schoch
@Olli the mainform is a thread too, and bacause of this you can use the wait function too.

Also IsWindow() gibt aber TRUE zurück. Ist wohl doch ein Fenster und kein Thread? Ist mir schon klar, daß das Hauptform im Hauptthread "lebt" und die Nachrichten über diesen Thread bekommt, aber was willst du mir damit sagen?

Zitat:

Zitat von C.Schoch
Here i work with events and thay you can set from everywhere in the programm.

Jupp.

@sk.Silvia: The TThread class must not be used like this. It has to be derived and the derived class has to be instantiated (and obviously implemented).

sk.Silvia 14. Mai 2006 13:30

Re: Realtime Input? Thread?
 
but how? :oops:

Delphi-Quellcode:
type TFiber=class(TTHread)
  procedure execute; override;
  end;

  procedure TFiber.execute;
  var i:integer;
      begin
      repeat
        ShowMessage(IntToStr(i));
        Suspend;
        i:=i+1;
      until i=3;
      end;

var Fiber:TFiber;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
  begin
  Fiber:=TFiber.Create(false);
  Fiber.execute;
  end;

procedure TForm1.Button2Click(Sender: TObject);
  begin
  Fiber.Resume;
  end;
but thats not correct:(

DGL-luke 14. Mai 2006 13:32

Re: Realtime Input? Thread?
 
I still don't know why you want to do this with a thread, if you mind looking at my post above.

But if, this is how to do it:

- place the query (or whatever fetches the input form the user) in a thread
- after the query was answered and confirmed by the user, the thread sets a variable "I'm done", places the user input somewhere the main thread has access to and goes asleep/terminates

- in your main thread, you use MSDN-Library durchsuchenwaitforsingleobject to wait until the thread does set this variable, fetch the input and continue your loop.

But unless you have to keep your app doing something else while waiting for the input (which wouldn't work the way I just described it), i don't know why you would want to use a thread here...

EDIT: you don't initialize your "i" in the thread, so it'll probably take a long time until it gets to "3". but I can't see any othermistakes in it.

sk.Silvia 14. Mai 2006 13:39

Re: Realtime Input? Thread?
 
cause i dont want that other forms are opened, i want just to stop the process, work with some other procedures and then by cliking resume work with the stoped procedure again


but this codes dont stopes on Suspend, but counts directly up to 3

Delphi-Quellcode:
type TFiber=class(TTHread)
  Memo:TMemo;
    procedure execute; override;
    procedure doit;
    constructor Create(iMemo:TMemo);
  end;

  constructor TFiber.Create(iMemo:TMemo);
    begin
    inherited Create(false);
    Memo:=iMemo;
    end;

  procedure TFiber.execute;
  var i:integer;
      begin
      Synchronize(Doit);
      doit;
      end;

  procedure TFiber.doit;
  var i:integer;
      begin
      i:=0;
      repeat
        Memo.Lines.Append(IntToStr(i));
        Suspend;
        i:=i+1;
      until i=3;
      end;

var Fiber:TFiber;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
  begin
  Fiber:=TFiber.Create(Memo1);
  Fiber.execute;
  end;

procedure TForm1.Button2Click(Sender: TObject);
  begin
  Fiber.Resume;
  end;

C.Schoch 14. Mai 2006 13:43

Re: Realtime Input? Thread?
 
IMHO you have to use suspend in the main thread

DGL-luke 14. Mai 2006 13:51

Re: Realtime Input? Thread?
 
ah... think i got you.

You have to place the thing you want to be able to suspend in the second thread.

well, your first example does work fine with me:

Delphi-Quellcode:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    {...}
  end;

  TFiber=class(TTHread)
    procedure execute; override;
  end;


var
  Form2: TForm2;
  Fiber: TFiber;

implementation

procedure TFiber.execute;
var i:integer;
begin
  i := 0;
  repeat
    ShowMessage(IntToStr(i));
    Suspend;
    i:=i+1;
  until i=3;
end;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  Fiber.Resume;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  Fiber := TFiber.Create(false);
end;

end.
@C.Schoch: Yes, if you want to stop the thread like when the user says so or similarly.

sk.Silvia 14. Mai 2006 14:11

Re: Realtime Input? Thread?
 
but by me the Suspend; dont stop the threat, it do nothing(likle i could exclude Suspend from the code and it would be the same) :(

but i need to suspend the threat in the repeat, so i cannot suspend it in Form1

Delphi-Quellcode:
  procedure TFiber.execute;
  var i:integer;
      begin
      i:=0;
      repeat
        Memo.Lines.Append(IntToStr(i));
        Suspend; //<------------HERE
        i:=i+1;
      until i=3;
      end;

DGL-luke 14. Mai 2006 14:24

Re: Realtime Input? Thread?
 
Liste der Anhänge anzeigen (Anzahl: 1)
it DOES with me...

I attached some primitive prime test, which stops itself after every found prime.

C.Schoch 14. Mai 2006 14:26

Re: Realtime Input? Thread?
 
maybe try :
Delphi-Quellcode:
   procedure TFiber.execute;
  var i:integer;
      begin
      i:=0;
      repeat
        Memo.Lines.Append(IntToStr(i));
        wait(1) // <--- Here i changed
        Suspend; //<------------HERE
        i:=i+1;
      until i=3;
      end;
i have no Delphi here to test this now

DGL-luke 14. Mai 2006 14:37

Re: Realtime Input? Thread?
 
btw, if you look at my example, I posted a message in order to transfer my data - accessing a memo owned by the main thread (well, kind of) can lead to conflicts when two threads try to access the same object.

sk.Silvia 14. Mai 2006 14:52

Re: Realtime Input? Thread?
 
Liste der Anhänge anzeigen (Anzahl: 1)
ok, thank you all, i realized my mistake thanks your program :kiss: iam so happy, thats exactly what i wanted to make :)) (see attachment)

well done, problem solved :party:


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:44 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz