The main application must start the embedded UA server and must stop it before the application terminates.
Windows Forms Sample Code that starts the UA server
Start the server by pressing a button in the application user interface
ApplicationInstance UaApp;
//------------------------------------
private void btnStart_Click(object sender, EventArgs e)
{
try
{
uaPLUS.daRootName = "Device";
uaPLUS.hdaRootName = "Device"; // same as DA for HDA access to DA items
uaPLUS.useInternalHDAbase = true; // use the built-in HDA processing methods
uaPlusMain serverMain = new uaPlusMain();
serverMain.AppShutdown += Server_Exit; //install the shutdown request event handler
DAHandler daDevice = new DAHandler(serverMain.uaPLUSinstance);
HdaDBAccess.HdaDBHandler hdaDBDevice = new HdaDBAccess.HdaDBHandler(serverMain.uaPLUSinstance); // only for HDA functionality
UaApp = serverMain.StartAsProcess();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Starting UA server");
}
}
Start the UA server at the Forms application startup
The server startup can take a few seconds. To prevent the application from becoming unresponsive the server start can be done in a background thread.
//--------------------------------------------------
// Form Load event handler
private void Form1_Load(object sender, EventArgs e)
{
// start the server in the background. The startup may take a few seconds.
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(startServer));
}
//---------------------------------------------
// background worker thread
private void startServer(object tag)
{
btnStart_Click(null, null); // use the manual startup code
}
Windows Forms Sample Code to stop the UA server:
//---------------------------------------------
// Event handler for NSPlugin requested server shutdown
private void Server_Exit()
{
if (InvokeRequired)
{
BeginInvoke(new ApplicationShutdown(Server_Exit));
return;
}
if (UaApp != null)
UaApp.Stop();
Close();
}
//---------------------------------------
// Button event handler
private void btnTerminate_Click(object sender, EventArgs e)
{
if (UaApp != null)
UaApp.Stop();
Close();
}