Hydra Non-Visual Plugins
From RemObjects Wiki
Many applications need to perform background tasks that don't need a visible form. Examples could be an Import/Export module or a Send and Receive Email plugin (think of Microsoft Outlook). These tasks often display limited information to the user (initial option dialog, completion information, etc.). In Hydra, this type of plugin is called "non-visual".
Contents |
How to create a Non-Visual Plugin
Creating a non-visual plugin is as simple as creating a visual one. In this article, we will assume you completed the first tutorial (Hydra Visual Plugins - Step By Step) and have a Hydra Module created and open.
Click on "File|New|Other", switch to the RemObjects Hydra tab and double click "Hydra Plugin":
The "New Hydra Plugin Wizard" will appear. Click Next and then select "Non-Visual Hydra Plugin":
The screens that follow match those of the Visual Plugin wizard and allow you to enter information like the plugin name, its description, version ,etc. We will jump to the last screen that summarizes the selected options.
Click Finish to create the new non-visual plugin.
You will now see a new data module that contains several new properties, as shown in the following screenshot:
How Non-Visual Plugins work
Non-Visual plugins are very similar to Delphi's NT Service projects: they can be started, stopped, paused and resumed. The events in the data module we just created provide you with the necessary hooks to implement this functionality.
In this example we will make a simple non-visual plugin that beeps every second.
Drop a TTimer component on the data module, change its Name property to "Timer" and associate the following code to the OnTimer event:
procedure TNewNonVisualPlugin.TimerTimer(Sender: TObject); begin Beep; end;
Now complete the plugin by typing the following code in the OnStart, OnStop, OnResume and OnPause event handlers.
procedure TNewNonVisualPlugin.HYNonVisualPluginStart(Sender: TObject); begin Timer.Enabled := TRUE; Host.SendMessage(Self, 0, PChar('The plugin has been started')); end; procedure TNewNonVisualPlugin.HYNonVisualPluginStop(Sender: TObject); begin Timer.Enabled := FALSE; Host.SendMessage(Self, 1, PChar('The plugin has been stopped')); end; procedure TNewNonVisualPlugin.HYNonVisualPluginResume(Sender: TObject); begin Timer.Enabled := TRUE; Host.SendMessage(Self, 2, PChar('The plugin has been resumed')); end; procedure TNewNonVisualPlugin.HYNonVisualPluginPause(Sender: TObject); begin Timer.Enabled := FALSE; Host.SendMessage(Self, 3, PChar('The plugin has been paused')); end;
Note: when you compile, you will see the SendMessage marked as deprecated. They are used here for simplicity but for real code we recommend that you use custom interfaces, which also work for .NET.
The following screenshot shows how everything should look:
How to use a Non-Visual Plugin in a host application
In order to use this non-visual plugin, you will need to make a few changes to the Host project.
Add the following components the the host form created in the first tutorial (Hydra Visual Plugins - Step By Step): 6 new TButton(s) named bStart, bStop, bResume, bPause, bCreateNonVisual, bDestroyNonVisual and aTMemo called Memo.
This is how your host application should look after you make these changes:
Add the following code to the main form:
procedure TMainForm.bUnloadClick(Sender: TObject); begin ModuleManager.ReleaseInstance(fVisualPlugin); // Equivalent to setting to NIL ModuleManager.ReleaseInstance(fNonVisualPlugin); // Added to support the non visual plugin ModuleManager.UnloadModule(TestDLLName); end; procedure TMainForm.ModuleManagerPluginMessage(const Sender: IHYPlugin; aMessageID: Integer; const aMessageData: Pointer); begin Memo.Lines.Add(IntToStr(aMessageID)+': '+PChar(aMessageData)); end; procedure TMainForm.bCreateNonVisualClick(Sender: TObject); begin if (ModuleManager.FindModule(TestDLLName)=NIL) then ModuleManager.LoadModule(TestDLLName); ModuleManager.CreateNonVisualPlugin('NewNonVisualPlugin', fNonVisualPlugin); end; procedure TMainForm.bDestroyNonVisualClick(Sender: TObject); begin fNonVisualPlugin := NIL; end; procedure TMainForm.bStartClick(Sender: TObject); begin fNonVisualPlugin.Start; end; procedure TMainForm.bStopClick(Sender: TObject); begin fNonVisualPlugin.Stop; end; procedure TMainForm.bPauseClick(Sender: TObject); begin fNonVisualPlugin.Pause; end; procedure TMainForm.bResumeClick(Sender: TObject); begin fNonVisualPlugin.Resume; end;
In the same way we had to add a private fVisualPlugin variable before, we now need to add fNonVisualPlugin:
procedure bLoadClick(Sender: TObject); procedure bUnloadClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ModuleManagerPluginMessage(const Sender: IHYPlugin; aMessageID: Integer; const aMessageData: Pointer); private fVisualPlugin: IHYVisualPlugin; fNonVisualPlugin: IHYNonVisualPlugin; public { Public declarations } end;
Compile and launch the application and click "Create NonVisual". Now click Start, Stop, Pause, Resume and see what happens in the Memo.
You now have a simple application that uses non-visual plugins.
Other Articles
Product Articles — Data Abstract — RemObjects SDK — Internet Pack — AnyDAC — Pascal Script
Glossary — Architecture — Articles — Library