| Autor |
Beitrag |
bobby
      
Beiträge: 25
Win XP
D7 Prof
|
Verfasst: Fr 04.07.08 23:00
I apologize for writing in English. I understand German very well, but I can't write in German.
So, the problem is like the following:
I have main app which creates a thread. This thread uses another unit, and this third unit needs ShowMessage and InputBox.
This will throw errors, as expected, as this unit is not aware of the main thread (GUI).
Is there any way to get this working?
How can I make the third unit to be aware of the thread which called it, so that I can do Synchronize in the thread, and this way I get the message to show up in the GUI?
Here is the code of the thread (TJSBrowserProxy):
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139:
| unit untJSBrowserProxy;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, untJSBrowser, SynUnicode;
type TJSBrowserProxy = class(TThread) private protected uBuffer: TWideStrings; uBrws: TBrowserBase; uStatus: string; uCompiled: Boolean; procedure Execute; override; procedure UpdateMain; procedure UpdateStatus;
public tScript: WideString; tEval: WideString; tEvalReplace: Boolean; tDebug: Boolean; tEvalFolder: string; tExeFolder: string; end;
implementation
uses untMain, MD5;
function FindFiles(Directory: string; InclAttr, ExclAttr: Integer; const SubDirs: Boolean; const Files: TStrings): Integer; var SearchRec: TSearchRec; begin InclAttr := InclAttr or $00000080; Directory := IncludeTrailingPathDelimiter(Directory); FillChar(SearchRec, SizeOf(SearchRec), 0); if FindFirst(Directory + '*.*', faAnyFile or $00000080, SearchRec) = 0 then begin try repeat if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then begin Files.Add(Directory + SearchRec.Name); if SubDirs then if SearchRec.Attr and faDirectory <> 0 then FindFiles(Directory + SearchRec.Name, InclAttr, ExclAttr, SubDirs, Files); end; until FindNext(SearchRec) <> 0; finally FindClose(SearchRec); end; end; Result := Files.Count; end;
procedure TJSBrowserProxy.UpdateStatus; begin frmMain.stbrStatus.SimpleText := uStatus; end;
procedure TJSBrowserProxy.UpdateMain; begin frmMain.DecoderCallback(uCompiled); end;
procedure TJSBrowserProxy.Execute; var sl: TStringList; i: Integer; tmpName: string; begin uStatus := 'Preparing temp folder'; ForceDirectories(tEvalFolder); Synchronize(UpdateStatus); sl := TStringList.Create; try FindFiles(tEvalFolder, faDirectory or faAnyFile, 0, false, sl); for i := 0 to sl.Count - 1 do DeleteFile(sl[i]); finally sl.Free; end;
uStatus := 'Working...'; Synchronize(UpdateStatus); SetCurrentDir(tExeFolder); uBrws := TBrowserBase.Create; uBrws.WriteObj := frmMain.mmResult.Lines; if tEvalReplace then uBrws.SetEvalReplace(tEval); if tDebug then uBrws.DoDebug; uBrws.CreateObjects; if uBrws.RunScript(tScript) then begin uCompiled := True; uStatus := 'Script compiled' end else begin uCompiled := False; uStatus := 'Collecting garbage'; Synchronize(UpdateStatus); sl := TStringList.Create; try FindFiles(tEvalFolder, faAnyFile, 0, false, sl); for i := 0 to sl.Count - 1 do begin tmpName := MD5Print(MD5File(sl[i])); if not (FileExists(tEvalFolder + tmpName)) then RenameFile(sl[i], tEvalFolder + tmpName) else DeleteFile(sl[i]); end; if sl.Count > 0 then uStatus := 'Script can''t be compiled, but it produced evaluation results' else uStatus := 'Script can''t be compiled'; finally sl.Free; end; end; Synchronize(UpdateStatus); Synchronize(UpdateMain); uBrws.Free; end;
end. |
Here is the code of the third unit, the one which needs ShowMessage and InputBox (TJSBrowser):
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266:
| unit untJSBrowser;
interface
{$J+}
uses js15decl, jsintf, Classes, SynUnicode, Messages;
var GlobWriteObj: TWideStrings;
type TBrowserBase = class private fengine: TJSEngine; fStatus: widestring; fDebuger: Boolean; fOnStatus: TNotifyEvent; eval_replace: WideString; procedure setWriteObj(Strings: TWideStrings); protected public constructor Create; destructor Destroy; override; procedure CreateObjects; procedure GarbageCollect; procedure DoDebug; function RunScript(Script: widestring): boolean; procedure SetStatus(StatusText: widestring); procedure SetEvalReplace(s: WideString); property Status: widestring read fStatus; property WriteObj: TWideStrings write setWriteObj; property OnStatus: TNotifyEvent read fOnStatus write fOnStatus; published end;
var GlobBrowserBase: TBrowserBase; eval_replace: WideString; const my_def: JSClass = (name: 'my'; flags: JSCLASS_HAS_PRIVATE; addProperty: JS_PropertyStub; delProperty: JS_PropertyStub; getProperty: JS_PropertyStub; setProperty: JS_PropertyStub; enumerate: JS_EnumerateStub; resolve: JS_ResolveStub; convert: JS_ConvertStub; finalize: JS_FinalizeStub);
function BR_write(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; function BR_writeln(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; function BR_open(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; function BR_alert(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; function BR_prompt(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; function BR_Set_Status(cx: PJSContext; obj: PJSObject; id: jsval; vp: pjsval): JSBool; cdecl; function BR_Get_Status(cx: PJSContext; obj: PJSObject; id: jsval; vp: pjsval): JSBool; cdecl;
implementation
uses Dialogs, SysUtils;
function BooleanToStr(Value: Boolean): string; begin if Value then Result := 'true' else Result := 'false'; end;
constructor TBrowserBase.Create; begin fEngine := TJSEngine.Create(400000); GlobBrowserBase := Self; eval_replace := ''; fDebuger := False; end;
destructor TBrowserBase.Destroy; begin fEngine.Free; inherited; end;
procedure TBrowserBase.DoDebug; begin fDebuger := True; end;
function TBrowserBase.RunScript(Script: widestring): boolean; begin try if fDebuger then begin fengine.StartDebugger; fEngine.Debugger.Initialize; fEngine.Debugger.SetCode(Script); end; result := fEngine.global.Evaluate(Script); except on e: exception do begin ShowMessage('Some violation occured' + #13#10 + 'in SpiderMonkey engine'); Result := False; end; end; end;
procedure TBrowserBase.SetEvalReplace(s: WideString); begin eval_replace := s; end;
procedure TBrowserBase.setWriteObj(Strings: TWideStrings); begin GlobWriteOb doc := global.AddObject(my_def, 'document'); SetLength(myfuncs, 3); myfuncs[0].name := 'writeln'; myfuncs[0].call := BR_writeln; myfuncs[0].nargs := 1; myfuncs[1].name := 'write'; myfuncs[1].call := BR_write; myfuncs[1].nargs := 1; myfuncs[2].name := 'open'; myfuncs[2].call := BR_open; myfuncs[2].nargs := 0; doc.AddMethods(myfuncs);
wind := global.AddObject(my_def, 'window'); SetLength(myprops, 1); myprops[0].name := 'status'; myprops[0].setter := BR_Set_Status; myprops[0].tinyid := 0; myprops[0].getter := BR_Get_Status; wind.AddProperties(myprops);
SetLength(myfuncs, 2); myfuncs[0].name := 'alert'; myfuncs[0].call := BR_alert; myfuncs[0].nargs := 1; myfuncs[1].name := 'prompt'; myfuncs[1].call := BR_prompt; myfuncs[1].nargs := 2; wind.AddMethods(myfuncs); end;
procedure TBrowserBase.GarbageCollect; begin fEngine.GarbageCollect; end;
procedure TBrowserBase.SetStatus(StatusText: widestring); begin fStatus := StatusText; if assigned(onStatus) then onStatus(self); end;
function BR_Set_Status(cx: PJSContext; obj: PJSObject; id: jsval; vp: pjsval): JSBool; cdecl; begin GlobBrowserBase.SetStatus(JSStringToString(JSValToJSString(vp^))); Result := JS_TRUE; end;
function BR_Get_Status(cx: PJSContext; obj: PJSObject; id: jsval; vp: pjsval): JSBool; cdecl; begin vp^ := StringToJSVal(cx, Pwidechar(GlobBrowserBase.fStatus)); Result := JS_TRUE; end;
function BR_writeln(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; begin rval^ := JSVAL_VOID; if Assigned(GlobWriteObj) then begin if JSValIsNull(argv^) then GlobWriteObj.Add('null') else if JSValIsVoid(argv^) then GlobWriteObj.Add('undefined') else if JSValIsString(argv^) then GlobWriteObj.Add(JSStringToString(JSValToJSString(argv^))) else if JSValIsInt(argv^) then GlobWriteObj.Add(IntToStr(JSValToInt(argv^))) else if JSValIsDouble(argv^) then GlobWriteObj.Add(FloatToStr(JSValToDouble(cx, argv^))) else if JSValIsBoolean(argv^) then GlobWriteObj.Add(BooleanToStr(JSValToBoolean(argv^))); end; Result := JS_TRUE; end;
function BR_write(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; begin rval^ := JSVAL_VOID; if Assigned(GlobWriteObj) then begin if JSValIsNull(argv^) then GlobWriteObj.Text := GlobWriteObj.Text + 'null'; if JSValIsVoid(argv^) then GlobWriteObj.Text := GlobWriteObj.Text + 'undefined'; if JSValIsString(argv^) then GlobWriteObj.Text := GlobWriteObj.Text + JSStringToString(JSValToJSString(argv^)); if JSValIsInt(argv^) then GlobWriteObj.Text := GlobWriteObj.Text + IntToStr(JSValToInt(argv^)); if JSValIsDouble(argv^) then GlobWriteObj.Text := GlobWriteObj.Text + FloatToStr(JSValToDouble(cx, argv^)); if JSValIsBoolean(argv^) then GlobWriteObj.Text := GlobWriteObj.Text + BooleanToStr(JSValToBoolean(argv^)); end; Result := JS_TRUE; end;
function BR_open(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; begin rval^ := JSVAL_VOID; if Assigned(GlobWriteObj) then GlobWriteObj.clear; Result := JS_TRUE; end;
function BR_alert(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; begin rval^ := JSVAL_VOID; ShowMessage(JSStringToString(JSValToJSString(argv^))); Result := JS_TRUE; end;
function BR_prompt(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl; var ResString, Mesg, Deflt: widestring; begin if (argc >= 1) and JSValIsString(argv^) then begin Mesg := JSStringToString(JSValToJSString(argv^)); inc(argv); end; if (argc >= 2) and (not JSValIsNumber(argv^)) then begin Deflt := JSStringToString(JSValToJSString(argv^)); end; ResString := InputBox('[JavaScript Application]', Mesg, Deflt); rval^ := jsval(JS_NewStringCopyZ(cx, PChar(ResString))) or JSVAL_STRING; Result := JS_TRUE; end; end. |
Moderiert von Christian S.: Replaced Code with Delphi Tags
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: Sa 05.07.08 07:22
bobby hat folgendes geschrieben: | I have main app which creates a thread. This thread uses another unit, and this third unit needs ShowMessage and InputBox.
This will throw errors, as expected, as this unit is not aware of the main thread (GUI).
How can I make the third unit to be aware of the thread which called it, so that I can do Synchronize in the thread, and this way I get the message to show up in the GUI? |
Simply use the class procedure TThread.Synchronize anywhere you like (e.g. TThread.Synchronize(nil, MyMethod);)
|
|
alzaimar
      
Beiträge: 2889
Erhaltene Danke: 13
W2000, XP
D6E, BDS2006A, DevExpress
|
Verfasst: Sa 05.07.08 07:38
No need for the nil-Parameter. There is a 'Synchronize'-Version with one Parameter, the method to be called.
As the passed method does not allow any parameters, you must define private fields in your own thread class and fill them prior to calling the Synchronize-Method.
Here's an example:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
| Type TMythread = Class (TThread) private fMessage : String; Procedure DoShowMessage; .... End; ...
Procedure TMyThread.DoShowMessage; Begin ShowMessage (fMessage); End;
Procedure TMyThread.Execute; Begin ... fMessage := 'Shown from within a thread'; Synchronize (DoShowMessage) ... End; |
NB: Usually it is not a good idea to have user input in threads. Try to avoid this by letting the user enter the required information before the thread is called.
As an alternative, create a separate process
_________________ Na denn, dann. Bis dann, denn.
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: Sa 05.07.08 07:43
@alzaimar: If I understood correctly the point is that he wants to run synchronized code in a third unit outside the context of a particular TThread instance.
|
|
bobby 
      
Beiträge: 25
Win XP
D7 Prof
|
Verfasst: Sa 05.07.08 08:37
delfiphan hat folgendes geschrieben: | | @alzaimar: If I understood correctly the point is that he wants to run synchronized code in a third unit outside the context of a particular TThread instance. |
Exactly.
Here is the scheme what calls which unit.
untMain -> untJSBrowserProxy(this is a thread) -> untJSBrowser -> js32.dll(external dll)
I gave the code of untJSBrowserProxy and untJSBrowser in my previous post. js32.dll is Mozilla's
SpiderMonkey JavaScript engine.
untJSBrowser creates DOM objects needed by SpiderMonkey. Two of them are callbacks for JavaScript functions Alert() and Prompt().
Thats the catch.
I have inserted untJSBrowserProxy just because some JavaScripts need long time to compile, and GUI gets locked for that time (some scripts can compile more than one minute).
I do not run parallel threads, it is just because the GUI. Also, this unit cleans the temp files after js32.dll finish with compiling (I use hacked js32.dll which uses temp files).
My basic problem is how to make untJSBrowser to be aware of his parent - untJSBrowserProxy, so that I can trigger a procedure in untJSBrowserProxy. That way I could display these messages.
@delfiphan
Can you give an example of TThread.Synchronize(nil, something) ?
I can call that one from my untJSBrowser even if untJSBrowser is not a thread itself?
thanks for all you help
bobby
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: Sa 05.07.08 11:20
bobby hat folgendes geschrieben: | @delfiphan
Can you give an example of TThread.Synchronize(nil, something) ?
I can call that one from my untJSBrowser even if untJSBrowser is not a thread itself? |
That is an example, isn't it? something is parameterless method (doesn't have to belong to TThread class).
If this: bobby hat folgendes geschrieben: | | untMain -> untJSBrowserProxy(this is a thread) -> untJSBrowser |
all runs in the same Delphi project you don't have to make any Unit aware of any threads. Just use TThread.Synchronize to run synchronized code in the main thread.
TThread.Synchronize cannot be called from a DLL. In that case, use SendMessage to run code (synchronously) in another thread or another process.
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: Sa 05.07.08 19:44
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
| type TMyShowMessage = class private FMessage: WideString; public property Message: WideString read FMessage write FMessage; procedure ShowTheMessage; end;
procedure TMyShowMessage.ShowTheMessage; begin Windows.MessageBoxW(0, PWideChar(FMessage), PWideChar(WideString(Application.Title)), MB_OK or MB_ICONWARNING); end;
function BR_alert ...; cdecl; var MyShowMessage: TMyShowMessage; begin (...) MyShowMessage := TMyShowMessage.Create; try MyShowMessage.Message := MyMessage; TThread.Synchronize(nil, MyShowMessage.ShowTheMessage); finally MyShowMessage.Free; end; end; |
Or if you don't mind a slight bit of hacking:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
| procedure WideAlert(const AMessage: PWideChar); begin Windows.MessageBoxW(0, AMessage, PWideChar(WideString(Application.Title)), MB_OK or MB_ICONWARNING); end;
procedure SynchronizedWideAlert(const AMessage: WideString); var Method: TThreadMethod; begin TMethod(Method).Code := @WideAlert; TMethod(Method).Data := PWideChar(AMessage); TThread.Synchronize(nil, Method); end;
function BR_alert ...; cdecl; begin (...) SynchronizedWideAlert(MyMessage); end; |
PS: Just as an OT side note: Can't you implement window and document as COM objects?
|
|
bobby 
      
Beiträge: 25
Win XP
D7 Prof
|
Verfasst: Sa 05.07.08 21:44
@delfiphan
Thank you very much!
Your first code worked like a charm (with the exception that the unit outside a main thread isn't aware of Application.Title)
About COM objects - I do not have a clue what these are at all...
btw. do you mind if I mention you in the About box of my app?
The app is this one here - malzilla.sourceforge.net
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: Sa 05.07.08 22:01
bobby hat folgendes geschrieben: | | with the exception that the unit outside a main thread isn't aware of Application.Title |
Delphi-Quelltext
|
|
bobby 
      
Beiträge: 25
Win XP
D7 Prof
|
Verfasst: Sa 05.07.08 22:14
Thanks again
If you are still in mood to help me, what is the native (Win32 API) call for InputBox?
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: Sa 05.07.08 22:23
I'm not aware of such a thing. See msdn.microsoft.com/e...ms632588(VS.85).aspx for further information.
|
|
bobby 
      
Beiträge: 25
Win XP
D7 Prof
|
Verfasst: Sa 05.07.08 22:41
I've take look at Dialogs.pas unit coming with Delphi, InputBox is created as a composition of label, editboxes, buttons etc. It is not native Windows component.
|
|
delfiphan
      
Beiträge: 2684
Erhaltene Danke: 32
|
Verfasst: So 06.07.08 08:33
bobby hat folgendes geschrieben: | | I've take look at Dialogs.pas unit coming with Delphi, InputBox is created as a composition of label, editboxes, buttons etc. It is not native Windows component. |
Well, as I said, I'm not aware of a WinAPI function for InputBoxes. Therefore if you look at the VCL unit Dialogs you will see VCL code.
See above link for more information on WinAPI dialogs. I'm not an expert in non-VCL programming. You might also find something on Luckie's Website.
|
|
|