2
$\begingroup$

I've created two packages file as

CompnentALink.m
CompnentBLink.m

the functions InstallNET[] and ReinstallNET[], both of them are used in the upper packages, when ReinstallNET is invoked in current package, the objects created previouly in the other packages shall crash, vise versa. and ReinstallNET is nessary because dead link might comes up unexpectedly now and then. How should I do to avoid the conflicts or make them working in independent workspaces? Thanks!

$\endgroup$
1
$\begingroup$

If there be no ways to restrict each package has indepent NETLink workspace, what I could do is to make sure that the NETLink instance runs only one time through the invoking.

checkupNETLink[] :=
     Module[ {bIsLoaded,result},
        (*check NETLink Package*)
         bIsLoaded = Length@Options@InstallNET > 0;
         If[ !bIsLoaded,
             Needs["NETLink`"]
         ];
         (*recheck*)
         result = Length@Options@InstallNET > 0;
         Return[result];
     ]

then going to check whether the NETLink instance is in Links and system processes lists or not.

checkupNETInstance[] :=
    Module[ {bIsInLinks,bIsInProcesses,result},
        (*check up NETLink*)
        If[ !checkupNETLink[],
            Return[$Failed]
        ];

        (*check Links list*)
        bIsInLinks = Length@Select[
           Length /@ (StringPosition[#, "InstallableNET"] & /@ (#[[1]] & /@ 
                Links[])), # > 0 &] > 0;

        (*check in case of process terminated unexpectedly but Links might have dead link*)
        bIsInProcesses = Length@Select[
           Length /@ (StringPosition[#, 
                "InstallableNET"] & /@ (First@Take[#, {-3}] &@
                  Values@#[[1]] & /@ SystemProcesses[])), # > 0 &] > 0;

        (*Uninstall to kill dead link*)
        If[ Or[!bIsInLinks,!bIsInProcesses],
            UninstallNET[];
            ReinstallNET[]
        ];

        (*validating*)
        result = BooleanQ@NETObjectQ@NETNew["System.Windows.Forms.Form"];
        Return[result];
    ]

when to setup NETLink instance,checkupNETInstance[] should be invoked. Hope there be better codes on this problem.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.