This topic is for the .NET geeks who are among us.
We will assume you know basics of C# and Navision automation.
I'm developing a lot with Navision and Navision automation. On my previous projects I had already discovered that not all events that are fired by the automation components actually are processed by Navision. This wasn't a really big deal because losing some wasn’t a huge disaster in these projects.
I never really understood why this happened mainly because I was unable to debug my C# projects when called from Navision. In debug mode suddenly the automation components is not available in Navision. (maybe someone else knows a solution for that).
My latest project however did require a more reliable way of sending events to Navision. So I dug into this problem.
I set up a testing environment trying to reproduce the problem. Reproducing was quite simple. Just put up a message box in Navision, and leave it there. At the same moment shoot an automation event into Navision.
Other attempts were executing a heavy Navision procedure.
So the behaviour wasn’t that hard to simulate!
Then I suddenly had a bright idea to turn the try {} catch {} system in c# off where the raising of the event was sitting in. This returned an exception indicating the message could not be fired because the receiving application was busy.
Sounds very simple to me know.!
The solution was easy. In your .NET project where you call the event delegate, repeat this until there will be no exception anymore.
Code examples:
Declarations. We all know that!
public event OnReceiveDelegate OnReceive;
[ComVisible(true)]
[Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface MessageEvents
{
[DispId(200)]
void OnReceive(IStream IS);
}
The code that calls the event
// this code can fail, thats why it is slashed out
// OnReceive(IS);
// this is the replacing code. Will continue until message is processed
while (TrySend(IS) == false);
We wrapped a function arround the actual calling of the event catching the error and report a bad execution to the calling function
private bool TrySend(IStream IS)
{
try
{
OnReceive(IS);
return true;
}
catch
{
return false;
}
}
Comments
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n