Options

Make sure Navision processes your c# automation events

janpieterjanpieter Member Posts: 298
edited 2010-11-13 in NAV Tips & Tricks
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;
  }
}
In a world without Borders or Fences, who needs Windows and Gates?

Comments

  • Options
    ara3nara3n Member Posts: 9,255
    Thank you for sharing this.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    kenethkeneth Member Posts: 3
    edited 2017-12-27
    Hi janpieter, I created an automation DLL with event and used the automation in NAV. Whenever the event is fired, NAV crashes. Do you have any advice? I am programming for NAV 2009 R2 Classic.
Sign In or Register to comment.