Make sure Navision processes your c# automation events

janpieter
Member Posts: 298
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!
The code that calls the event
We wrapped a function arround the actual calling of the event catching the error and report a bad execution to the calling function
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?
1
Comments
-
Thank you for sharing this.0
-
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.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions