Recommended lifespan of Seagull.BarTender.Print.Engine フォローする

0
Avatar
Aaron Carlson

We have a windows service that pulls jobs from a web service and sends them to the BarTender sdk for printing.

The code that interacts with the BarTender sdk is similar to the following:

public void Print(FileInfo dataFile, FileInfo label, string printerName)
{
Engine engine = new Engine(true); //expensive

LabelFormatDocument format = engine.Documents.Open(label.FullName); //expensive

TextFile fileConnection = format.DatabaseConnections.FirstOrDefault(c => c.Type == InputDataFile.TextFile) as TextFile;

if (fileConnection == null)
throw new Exception("Provided format does not have a text file datasource");

fileConnection.FileName = dataFile.FullName;

format.PrintSetup.PrinterName = printerName;

format.Print("printJob", 10);

engine.Stop(SaveOptions.DoNotSaveChanges);
}

 

I've done some internal timings and found that creating a new instance of Engine and opening a new file are relatively expensive operations.

I am wondering if I can keep an instance of the engine and label around for multiple print requests to improve performance.

For example, something like the following:


private static Engine _engine = new Engine(true);
private static Dictionary<string, LabelFormatDocument> _labels = new Dictionary<string, LabelFormatDocument>();

public void Print(FileInfo dataFile, FileInfo label, string printerName)
{
LabelFormatDocument format = GetLabelFormat(label);

TextFile fileConnection = format.DatabaseConnections.FirstOrDefault(c => c.Type == InputDataFile.TextFile) as TextFile;

if (fileConnection == null)
throw new Exception("Provided format does not have a text file datasource");

fileConnection.FileName = dataFile.FullName;

format.PrintSetup.PrinterName = printerName;

format.Print("printJob", 10);
}

public LabelFormatDocument GetLabelFormat(FileInfo label)
{
if (!_labels.ContainsKey(label.FullName))
{
_lables[label.FullName] = _engine.Documents.Open(label.FullName);
}

return _lables[label.FullName];
}

 

Is that an acceptable way to use the c# SDK?

3 コメント

0
Avatar
Rick Thompson
コメントアクション Permalink

Hi Aaron,

I've been wondering the same thing. Since version 9.x we've had our own "Print Web Service" that accepts as inputs a printer name, "relative path" btw file (the web service's configured default label folder or can take a lib:<path> since 2016 version), and then a list of name/value pairs that get substituted into the document.

With version 9.x we found that holding on to the Engine to long created problems so we have a configuration parameter that says how many prints can be done before dropping and creating a new engine. As I recall we settled on 5 or so ... I have not adjusted this for the 2016 version.

This particular service is not self-hosted and runs under IIS, so we also watch for application close and gracefully shutdown the engine as well. This also means it gets a 20 minute life span when not in use after the latest print.

I certainly would like to know if there is better way to do this.

Thanks,

Rick

0
Avatar
Aaron Carlson
コメントアクション Permalink

Hi Rick, I don't have an exact answer for you.  However, we switched over to using the TaskManager API in the BarTender SDK.  It's a layer on top of the Engine that can manage multiple instances of the Engine and will auto destroy and create new instances when it determines old instances have gone "bad".  We use a single instance of the TaskManager class for the lifetime of the application.

We have needs to be able to handling multi threaded printing (printing to multiple printers on the same machine) that using a single instance of the Engine didn't handle well.  Switching to the TaskManager API improved both performance and reliability.

You can find doc on the TaskManager API in the "BarTender .net SDK Help" that comes with the BarTender install.

FYI, using the TaskManager API requires an Enterprise Automation Edition License

0
Avatar
Rick Thompson
コメントアクション Permalink

Hi Aaron,

 

That sounds like exactly what I need - I'll take look ... and we do use the Enterprise version.

 

Thanks,

Rick

ログインしてコメントを残してください。