Hydra Non-Visual Plugins

From RemObjects Wiki

Jump to:navigation, search
Delphi Platform Header-48.png

This is a Hydra for Delphi Articles topic
Feel free to add your notes to this topic below.


New page awaiting review

Please do not rely on the content. After review, the page will be indexed and may have its name changed.
If you have suggestions for this page, please send them to us: email.


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

Delphi

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", go to the RemObjects Hydra category and double click "Hydra Plugin":

HY 03 01 1.png

The "New Hydra Plugin Wizard" will appear. Click Next and then select "Non-Visual Hydra Plugin":

HY 02 08 1.png

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.

HY 03 03 1.png

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:

HY 03 04 1.png

.Net

We will add .Net non-visual plugin to the existing project, that we have created in the previous article.

Click on Project|Add New Item and select Non-Visual Plugin.

HY 03 01 2.png

Enter name of the plugin and click Add.

You will now see a new component that contains several new properties, as shown in the following screenshot:

HY 03 02 2.png



How Non-Visual Plugins work

Delphi

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 send message to the host 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
  Inc(fTickCount);
  Host.SendMessage(Self, fTickCount, PChar('Plugin tick count'));
end;

Also, we need to add fTickCount to the host form as a private variable of type integer, and add Dialogs, Windows and Messages to the uses section.

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.

Now complete the plugin by typing the following code in the OnStart, OnStop, OnResume and OnPause event handlers.

procedure TNewNonVisualPlugin.HYNonVisualPluginPause(Sender: TObject);
begin
  Timer.Enabled := false;
  ShowMessage('Delphi plugin has been paused');
end;

procedure TNewNonVisualPlugin.HYNonVisualPluginResume(Sender: TObject);
begin
  Timer.Enabled := true;
  ShowMessage('Delphi plugin has been resumed');
end;

procedure TNewNonVisualPlugin.HYNonVisualPluginStart(Sender: TObject);
begin
  Timer.Enabled := true;
  ShowMessage('Delphi plugin has been started');
end;

procedure TNewNonVisualPlugin.HYNonVisualPluginStop(Sender: TObject);
begin
  Timer.Enabled := false;
  fTickCount := 0;
  ShowMessage('Delphi plugin has been stoped');
end;

The following screenshot shows how everything should look:

HY 03 05 1.png


.Net

The .Net plugin will calculate it's run time, and save results to a file.

Add Timer component to the plugin, set it's Interval property to 1000 and and associate the following code to the Tick event:

private DateTime fStartTime;
    private TimeSpan fRunTime = new TimeSpan();

    private void timer_Tick(object sender, EventArgs e)
    {
      fRunTime = fRunTime.Add(new TimeSpan(0, 0, 1));      
    }

Now complete the plugin by typing the following code in the OnStart and OnStop event handlers.

private void Plugin2_OnStart(object sender, EventArgs e)
    {
      fStartTime = DateTime.Now;
      fRunTime = fRunTime.Subtract(fRunTime);
      timer.Start();
    }

    private void Plugin2_OnStop(object sender, EventArgs e)
    {
      timer.Stop();

      using (StreamWriter OutFile = new StreamWriter("Output.txt"))
      {
        OutFile.WriteLine("Started: " + fStartTime.ToLongTimeString());
        OutFile.WriteLine("Run time: " + fRunTime.ToString());
        OutFile.WriteLine("Stoped: " + DateTime.Now.ToLongTimeString());
      }
    }


How to use a Non-Visual Plugin in a host application

In order to use these non-visual plugins, we will create a new Host project. You can read in detail about host creation in the Hydra Visual Plugins - Step By Step article.

Add three buttons to the host form, named bLoadDelphi, bLoadNet and bUnload and add following code to their OnClick handlers:

procedure TMainForm.bLoadDelphiClick(Sender: TObject);
begin
  bUnload.Click;
  ModuleManager.LoadModule('NewPluginLibrary.dll');
  ModuleManager.CreateNonVisualPlugin('NewNonVisualPlugin', fNonVisualPlugin);
end;

procedure TMainForm.bLoadNetClick(Sender: TObject);
begin
  bUnload.Click;
  ModuleManager.LoadModule('PluginModule1.dll');
  ModuleManager.CreateNonVisualPlugin('PluginModule1.Plugin2', fNonVisualPlugin);
end;

procedure TMainForm.bUnloadClick(Sender: TObject);
begin
  fNonVisualPlugin := nil;
  ModuleManager.UnloadModules;
end;

Note: CreateNonVisualPlugin is passed an 'out' parameter called fNonVisualPlugin and this needs to be added to the host form as a private variable of type IHYNonVisualPlugin.

Add four more buttons named bStart, bStop, bPause, bResume and a TLabel called lMessage. Also add THYModuleManager.OnPluginMessage handler.

This is how your host application should look after you make all these steps:

HY 03 06 1.png

Add the following code to the main form:

procedure TMainForm.bPauseClick(Sender: TObject);
begin
  if Assigned(fNonVisualPlugin) then
    fNonVisualPlugin.Pause;
end;

procedure TMainForm.bResumeClick(Sender: TObject);
begin
  if Assigned(fNonVisualPlugin) then
    fNonVisualPlugin.Resume;
end;

procedure TMainForm.bStartClick(Sender: TObject);
begin
  if Assigned(fNonVisualPlugin) then
    fNonVisualPlugin.Start;
end;

procedure TMainForm.bStopClick(Sender: TObject);
begin
  if Assigned(fNonVisualPlugin) then
    fNonVisualPlugin.Stop;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  bUnload.Click;
end;

procedure TMainForm.ModuleManagerPluginMessage(const Sender: IHYPlugin;
  aMessageID: Integer; const aMessageData: Pointer);
begin
 lMessage.Caption := PChar(aMessageData) + ' : ' + IntToStr(aMessageID);
end;


Compile and launch the application and click "Load Delphi plugin". Now click Start, Stop, Pause, Resume and see what happens.

HY 03 07 1.png

Click Load .Net plugin, then Start and after some time Stop, now a file named Output.txt is created in the project folder.


HY 03 08 1.png

You now have a simple application that uses non-visual plugins.



Other Articles

Product Articles Data Abstract RemObjects SDK   — Internet Pack Pascal Script


Hy-48.png

Product: RemObjects Hydra
Current version: Hydra 3.0

GlossaryArchitectureArticles   — Library  

Navigation
products
hubs
special
Toolbox