C#调用把BarTender模板 下载本文

内容发布更新时间 : 2024/6/16 3:18:19星期一 下面是文章的全部内容请认真阅读。

In C#:

// Calling constructor with 'true' automatically starts engine. using (EnginebtEngine = new Engine(true)) {

// Do something with the engine. // Stop the BarTender process. btEngine.Stop(); } In VB:

' Calling constructor with 'true' automatically starts engine. UsingbtEngineAs NewEngine(True) ' Do something with the engine. ' Stop the BarTender process. btEngine.Stop() End Using

In the above example, an engine is created and started implicitly by passing 'true' as an argument to the constructor. The engine is then stopped by calling the Engine.Stop method. This terminates the background bartend.exe process. Finally, Engine.Dispose is called automatically when execution leaves the 'using' statement, releasing all Engine resources.

It is also possible to start the engine explicitly after it has been created using the default Engine constructor and the Engine.Start method. By default, Engine.Stop will close all open formats without saving, but an overloaded version is provided that allows manual control. The following example shows alternative code for starting and stopping an engine and saving changes: In C#:

using (EnginebtEngine = newEngine())

{

// Application specific code // Explicitly start the engine btEngine.Start();

// Application-specific code

// Assuming the application wants to save changes, //it can be easily done at Stop time. btEngine.Stop(SaveOptions.SaveChanges); } In VB:

UsingbtEngineAs NewEngine(True) ' Application specific code ' Explicitly start the engine btEngine.Start()

' Application-specific code

' Assuming the application wants to save changes, ' it can be easily done at Stop time. btEngine.Stop(SaveOptions.SaveChanges) End Using

In the above example, a new Engine is created, but not started until later. Some application activity is assumed to execute, then the Stop method is called. In this case, changes to labels done while using the engine are saved back to file. The SaveOptions enumeration specifies the operation concerning open label formats to perform during exit of BarTender. In the above examples and many other examples in this document, the Engine.Dispose method is called implicitly by a 'using' statement. While it is not always appropriate to utilize 'using', it is a convenient way to ensure Dispose is called even if the block is exited during an exception.

How To: Use Engine as a Field in a Class

The above examples, and most examples in this document, present use of an Engine instance in a single method. This approach is not practical for most real applications. Starting and stopping Engine objects, and by extension BarTender processes, should be done as rarely as possible for optimal performance. Ideally, Engine instances should be started once and only stopped at the end of the application to minimize the overhead of managing processes. The most straightforward object-oriented approach is make an Engine object a field in a class and allow the encapsulating class to determine the Engine object's lifetime.

The following is the minimal suggested code for making an Engine object a field in a class: In C#:

public classEngineWrapper : IDisposable {

// Engine Field

privateEnginem_engine = null;

// This property will create and start the engine the first time it is // called. Most methods in this class (and methods in child classes) // should utilize this property instead of the m_engine field. protectedEngineBtEngine { get {

// If the engine has not been created yet, create and start it. if (m_engine == null) {

m_engine = newEngine(true);

}

returnm_engine; } }

// Implement IDisposable public void Dispose() {

// The engine only needs to be stopped and disposed if it was // created. Use the field here, not the property. Otherwise, // you might create a new instance in the Dispose method! if (m_engine != null) {

// Stop the process and release Engine field resources. m_engine.Stop(); m_engine.Dispose(); } }

// Additional methods for specific work in your application. All additional

// methods should use the BtEngine property instead of the m_engine field. } In VB:

Public ClassEngineWrapper ImplementsIDisposable ' Engine Field

Privatem_engineAs Engine = Nothing

' This property will create and start the engine the first time it is ' called. Most methods in this class (and methods in child classes) ' should utilize this property instead of the m_engine field. Protected ReadOnly PropertyBtEngine() As Engine Get

' If the engine has not been created yet, create and start it. Ifm_engineIs Nothing Then m_engine = NewEngine(True) End If

Returnm_engine End Get End Property

' ImplementIDisposable

Public SubDispose() ImplementsIDisposable.Dispose

' The engine only needs to be stopped and disposed if it was ' created. Use the field here, not the property. Otherwise, ' you might create a new instance in the Dispose method! Ifm_engineIsNot Nothing Then

' Stop the process and release Engine field resources. m_engine.Stop() m_engine.Dispose() End If End Sub

' Additional methods for specific work in your application. All additional ' methods should use the BtEngine property instead of the m_engine field.