Options

Practical use of Codeunit Instances

AkilaAkila Member Posts: 19
Hi All,
Codeunit has two instances. Single instance and Multiple Instance. But I don't know the practical use of it. If anyone explains with example, it will be very useful to me.

Thanks in Advance..........
Dream is not the thing, what you see in the sleep. Dream is the thing, which does not let you sleep.

Comments

  • Options
    ara3nara3n Member Posts: 9,255
    Here is an example of single instance CU.


    http://www.mibuso.com/forum/viewtopic.p ... empglentry


    Also Single Instance CU are used on NAS.
    To accept connection.

    Run a timer for scheduler
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Options
    AkilaAkila Member Posts: 19
    Hi ara3n,
    i am new to navision. i didn't get clear idea of single instance practical use from that link.
    Could you explain in simple way with example. Please Add Multi instance also............
    Dream is not the thing, what you see in the sleep. Dream is the thing, which does not let you sleep.
  • Options
    DenSterDenSter Member Posts: 8,304
    Single instance codeunits can 'live' in only one instance per session. When you start a single instance codeunit, it will not be destroyed from memory, so all global variables in it are alive and available to other objects. When you start a single instance codeunit for the second time, the system know that it is already running and it will use the one that's already running.

    Normal codeunits can live in multiple instances at the same time. This means that when you start the codeunit for a second time, it will be created in its own memory space, and it will keep its own set of variables.
  • Options
    jlandeenjlandeen Member Posts: 524
    I have found 2 uses for single instance codeunits.

    The first case is when working with NAS to do any sort of integration with external applications (like Business Notifications, Employee Portal, Sharepoint, Biztalk, etc) there needs to be a single instance codeunit that is tied to Microsoft Message Queue. This means that when the MSMQ Connector (a dll provided by Microsoft that hooks up Navision to MSMQ) triggers an event that it has someplace to go to. If there were no single instance codeunit option, the Codeunit would start, hook up to the DLL get to the end of the code in the OnRun trigger and shut down. Thus there would be nothing in memory to receive the events from the DLL.

    The second case I have found is when I want to persist data between 2 seperate objects. The most recent case has been XML ports as I have not found a way to pipe any data to the outstream when running XMLPORT.IMPORT. So what I do is use a single instance codeunit to persist any values across objects.

    Example:
    - Codeunit A is single instance
    - Codeunit A has functions LoadValue and RetrieveValue (those values are stored in global variables inside Codeunit A)
    - XMLPORT 1 calls Codeunit A.RetrieveValue and uses that value to determine what it returns.
    - Form B can now have a button that updates a value in Codeunit A (via LoadValue function) and then call XMLPORT.EXPORT(1,<some outstream variable>) and write the outstream to a file.

    This means I can now use XMLPORT.EXPORT(1,<some outstream variable>) and it will always ge values from Codeunit A (which can be set by any other table, form, object). I no longer have to declare a variable for the XMLPort and write functions in this XML Port. This was required if I wanted to write any Generic XML Port handling code that does not expclicitly reference any XML ports.
    Jeff Landeen - Sr. Consultant
    Epimatic Corp.

    http://www.epimatic.com
  • Options
    krikikriki Member, Moderator Posts: 9,096
    Keep an eye on the tips&tricks and the download-section and also the how-to section (I don't know yet where I will put it :roll: ).
    When I find the time I will post there another pratical use of codeunit instances.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Options
    AkilaAkila Member Posts: 19
    Thanks to All............ =D>
    Dream is not the thing, what you see in the sleep. Dream is the thing, which does not let you sleep.
Sign In or Register to comment.