<< Click to Display Table of Contents >> Navigation: Programming > Mail Merge (replace fields with data) and data forms > Working with multiple threads |
Unlike many other editor components WPTools Version 9 can work threadsavely. This makes sense if you use WPTools to create documents. Since WPTools can be used to create and print documents without any window handle being required You can use WPTools as a powerful engine to created electronic documents. This includes RTF documents, HTML documents and, with our product wPDF, also PDF documents.
The demo 'ThreadSave' shows how to create a seperate task to do merge text. The merge template is sent from the main thread to the sub thread. Each subthread merges the text 100 times and saves each resulting file as RTF FILE:
The constructor is the most important part of the thread class:
constructor TWPToolsThread.Create(const SomeText, DirName, Text: string; Count: Integer);
begin
inherited Create(false);
ForceDirectories(DirName);
RichText := TWPCustomRtfEdit.CreateDynamic;
{$IFNDEF NOENVIROMENT}
Enviroment := TWPToolsEnviroment.Create(nil);
Enviroment.Assign(GlobalWPToolsCustomEnviroment);
RichText.Memo.RTFData.RTFProps.Enviroment := Enviroment;
{$ENDIF}
RichText.OnMailMergeGetText := DoMailMergeGetText;
RichText.AsString := Text;
FCount := Count;
FSomeText := SomeText;
FDirName := DirName;
end;
With this line the editor which is used for the process is created:
RichText := TWPCustomRtfEdit.CreateDynamic;
TWPCustomRtfEdit is defined in unit WPCTRMEMO - it is the basic editor class. This class does not contain all features TWPRichTExt has, cannot be attached to a TWPRuler or a TWPToolBar but contains all procedures which are required to create text and tables, insert images and to do mail merge.
The optional code
Enviroment := TWPToolsEnviroment.Create(nil);
Enviroment.Assign(GlobalWPToolsCustomEnviroment);
RichText.Memo.RTFData.RTFProps.Enviroment := Enviroment;
is only required if you need threadsave printing or if you need to add different object and file format handling classes to the enviroment.
Example C++Builder code to work with dynamic WPTools editor
TWPCustomRtfEdit *wp2;
wp2 = new TWPCustomRtfEdit(); // = CreateDynamic
wp2->_MakeDynamic();
wp2->InputString("Hello World\rNext Line",0);
// Insert the text into a differen editor
WPRichText1->SelectionAsString = wp2->AsString;
// If we need to print we need ReformatAll
wp2->ReformatAll(false,false);
wp2->PrintPages(1,1);
// Delete the object
delete wp2;
Note: do not create editor windows which should work interactively using CreateDynamic!