Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: janos_murai on July 22, 2016, 10:19:15 pm

Title: Delete connector with c#
Post by: janos_murai on July 22, 2016, 10:19:15 pm
I would like to delete a connector in my add-in. I know that there are a few topic about this, but I'm facing a new problem. I know the exact index of the connector I’d like to delete in the Connectors collection. I don’t try to delete this link in a loop. I use all the possible updating functions.
My code is the following:
Code: [Select]
try
{
       short connectorNum = 0;
       string last error = “”;
       requirementElementTarget.Connectors.Delete(connectorNum);
       requirementElementTarget.Connectors.Refresh();
       requirementElementTarget.Update();
       requirementElementSource.Connectors.Refresh();
       requirementElementSource.Update();
       lastError = requirementElementTarget.Connectors.GetLastError();
}
catch
{
//No exception here
}

The type of the variable requirementElementTarget and requirementElementSource are EA.Element. This code works, so it deletes the appropriate link. It doesn’t throw any exception and the lastError string is empty.
The bad part:
I have a Windows Form, where I implemented a BackgroundWorker, and I would like to call the link deleting code from there. I call other functions from this BackgroundWorker, they work fine. I can also call the link deleting function, it returns without any problem or exception. Finally I call the Windows Form’s ShowDialog() function, and this throws a System.Runtime.InteropServices.SEHException. Well I’m not a .NET genius so I don’t really know what does this exception mean, but I could figure out that this exception was coming from an other thread.
The StackTrace is the following:
Code: [Select]
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at EADOORSLinkSync.EADOORSLinkSyncAddinClass.callSnycScript(String updateMode) in C:\murajade\EA_scripts\EADOORSLinkSyncAddIn\trunk\EADOORSLinkSync\EADOORSLinkSync_Solution\EADOORSLinkSync\EADOORSLinkSyncAddinClass.cs:line 189

I can catch this exception, but it will kill EA anyway. After restarting EA, the connector doesn’t exist anymore. If I remove the “requirementElementTarget.Connectors.Delete(connectorNum);”  line everything works without any problem.

I hope my problem is more or less clear.  :'(

Thank you in advance!
Title: Re: Delete connector with c#
Post by: qwerty on July 23, 2016, 12:50:25 am
I guess you need to supply a long (just a guess). All your Update/Refresh calls are superfluous.

q.
Title: Re: Delete connector with c#
Post by: Geert Bellekens on July 23, 2016, 04:00:38 pm
I don't think your problem has anything to do with deleting the link.
There's probably a problem calling a dialog from a background thread.

I think there are thread safe methods to do such a thing.

Geert
Title: Re: Delete connector with c#
Post by: McMannus on July 25, 2016, 11:31:19 pm
Yeah, Geert is right! Cross-thread invocation of dialogs/controls/forms is a weird thing in .NET.

Search for Control.Invoke() and Control.BeginInvoke() to find the method suiting your use case.

More information on the topic you can find here (http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c)

Jan
Title: Re: Delete connector with c#
Post by: janos_murai on July 26, 2016, 11:19:16 pm
Thank you for all the helpful replies!

You are right, the problem was with the thread handling.

My solution was to remove the background worker, and I called my link-deleting function with the Thread class. Although, I don’t understand why it works now, because as far as I know the background worker creates a new thread the same way as the Thread class does.
It is also strange that I could create connectors with the original method.

Thanks again,
Janos