Autor Beitrag
gunman
Hält's aus hier
Beiträge: 4



BeitragVerfasst: Mi 02.10.02 12:39 
Hallo Leudz,

ich arbeite gerade an einer Konsolenanwendung (nonvcl logischerweise:.)) die aus einem Mainthread und einem Nebenthread besteht. Der Nebenthread wurde mit der API Funktion CreateThread erzeugt. Wie kriege ich es hin das ich aus diesem Thread Ereignisse an den Mainthread schicken kann.
Problem ist dabei auch noch meine Unit-Aufteilung. Das heist der Haupteil meines Codes benutzt die ThreadUnit aber nicht umgekehrt.
Muss ich dafür API Code benutzen oder gibt es noch einen eleganteren Weg über Delphi :?: :?: :?:
OregonGhost
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 215



BeitragVerfasst: Sa 05.10.02 17:57 
Du kannst mit SetEvent() einen Event (= Ereignis (c; ) auslösen, den der andere Thread mit WaitForSingleObject() jederzeit abfragen kann.

_________________
Oregon Ghost
---
Wenn NULL besonders groß ist, ist es fast schon wie ein bisschen eins.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Fr 06.04.07 11:17 
Hierzu noch eine kleine Frage, nachträglich:

Kann ich in einer, von TThread abgeleiteten Klasse, Ereignisse deklarieren und auslösen?
Und kann dann der HauptThread, also die Anwendung eine Ereignisbehandlungsroutine bereitstellen?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 06.04.07 11:20 
Moin1

TThread hat doch dafür Synchronize(). ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 06.04.07 11:24 
Sendmessage/Postmessage von einem Thread an ein Fenster sind kein Problem.

_________________
We are, we were and will not be.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Fr 06.04.07 11:26 
Ich hätte aber gern, dass der Programmierer, der meinen Thread benutzt sich selbst überlegen kann, ob er ein Ereignis implementiert, oder nicht. Geht das also so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
type
  TLeseThread = class(TThread)
  private   
    FOnInitFailed: TNotifyEvent;
  protected
    procedure Execute; override;
  public
    property OnInitFailed: TNotifyEvent read FOnInitFailed write FOnInitFailed;    
  end;


... und in Execute:
ausblenden Delphi-Quelltext
1:
if Assigned(FOnInitFailed) then FOnInitFailed(Sender);					


? Danke schon mal!
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 06.04.07 11:28 
Moin!

Ja, so etwa kann man das machen. Du mußt dir dann nur noch überlegen, ob du das Ereignis VCL-synchron ausführen willst (dann noch mit Synchronize() kapseln) oder nicht (wie es jetzt ist -> im Thread-Kontext nämlich -> keine VCL!).

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Fr 06.04.07 11:33 
Gut, danke Narses.

Aber noch eins, ich kann ja eine Methode mit Parametern nicht mit Synchronize kapseln, sondern nur welche ohne Parametern.

Synchronize(FOnInitFailed(Self)); da kommt ein Compiler-Fehler.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 06.04.07 11:47 
Dann kannst du sowas machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
//...
fMyParam := Whatever;
Synchronize(MeinEventProc);
//...

procedure TMyThread.MeinEventProc;
begin
  if Assigned(FOnInitFailed) then FOnInitFailed(fMyParam);
end;

_________________
We are, we were and will not be.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Fr 06.04.07 11:50 
ja, daran hab ich auch schon gedacht. Naja Danke euch zwei...