Events
The code snippet below (from the AutomateAuthenticDesktop example) lists the code for two event handlers. The AutomateAuthenticDesktop example (see the file Form1.cs) is located in the C# subfolder of the API Examples folder:
Windows 7, Windows 8, Windows 10, Windows 11 | C:\Users\<username>\Documents\ |
You can compile and run the project from within Visual Studio 2012/2013/2015/2017/2019/2022.
Code snippet
delegate void addListBoxItem_delegate(string sText);
// Called from the UI thread
private void addListBoxItem(string sText)
{
listBoxMessages.Items.Add(sText);
}
// Wrapper method to call UI control methods from a worker thread
void syncWithUIthread(Control ctrl, addListBoxItem_delegate methodToInvoke, String sText)
{
// Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
// Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
if (ctrl.InvokeRequired)
ctrl.BeginInvoke(methodToInvoke, new Object[] { sText });
}
// Event handler for OnDocumentOpened event
private void handleOnDocumentOpened(XMLSpyLib.Document i_ipDocument)
{
String sText = "";
if (i_ipDocument.Name.Length > 0)
sText = "Document " + i_ipDocument.Name + " was opened!";
else
sText = "An empty document was created.";
// Synchronize the calling thread with the UI thread because
// COM events are triggered from a working thread
addListBoxItem_delegate methodToInvoke = new addListBoxItem_delegate(addListBoxItem);
// Call syncWithUIthread with the following arguments:
// 1 - listBoxMessages - list box control to display messages from COM events
// 2 - methodToInvoke - a C# delegate which points to the method which will be called from the UI thread
// 3 - sText - the text to be displayed in the list box
syncWithUIthread(listBoxMessages, methodToInvoke, sText);
}
private void checkBoxEventOnOff_CheckedChanged(object sender, EventArgs e)
{
if (AuthenticDesktop != null)
{
if (checkBoxEventOnOff.Checked)
AuthenticDesktop.OnDocumentOpened += new XMLSpyLib._IApplicationEvents_OnDocumentOpenedEventHandler(handleOnDocumentOpened);
else
AuthenticDesktop.OnDocumentOpened -= new XMLSpyLib._IApplicationEvents_OnDocumentOpenedEventHandler(handleOnDocumentOpened);
}
}