Is there a way to detect a recompile or something like a Terminate state for a non-window based AddOn?
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
How Can AddOn Detect Recompile?
Collapse
X
-
How Can AddOn Detect Recompile?
I have some AddOn functionality that is provided with some statics. All works great, but would be nice to have a way to detect a recompile, so could trigger some cleanup of the previous instance and allow the new instance to take over. Occasionally, I end up with two instances still running.
Is there a way to detect a recompile or something like a Terminate state for a non-window based AddOn?
Tags: None
-
It would be nice and probably not very difficult to add something like an OnCompileFinished event that can be subscribed to.
A different solution is to have code that uses static variables in a separate DLL, therefore it won't be affected by NT code recompilations. Actually the same thing applies addons that are imported, so it only matters during development.
Code:namespace NTDataStore { public static class StaticCounter { public static int counter = 0; } }
Code:protected override void OnBarUpdate() { if(CurrentBar != 0) { return; } Print(NTDataStore.StaticCounter.counter++); }
Without these steps using a .netstandard 1.0 DLL, I was only able to load it via reflection E.g.
Code:Assembly.LoadFile
Last edited by MojoJojo; 06-16-2020, 06:48 AM.
- Likes 1
Comment
-
The best I have been able to come up with is to use the AddOn State transitions. The AddOn will go thru Terminated state on a recompile, and then the new instance is created and goes thru normal state progression. You still need to deal with cleanup properly, as some other user may be using the static instance, but that would be true of a recompile event also.
Comment
-
I've tested this code
Code:protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"NinjaTrader Data Store"; Name = "NinjaTraderDataStore"; } else if (State == State.Configure) { AppDomain.CurrentDomain.AssemblyLoad += MyAssemblyLoadEventHandler; } else if (State == State.Transition) { Print("Transitioning"); } } private void MyAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args) { Print("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName); }
The imported addon because it is loaded from a DLL will only output information once on each recompilation, because NT does not create a new instance of it. While code that is merely in the addon folder will create a new instance of that class and therefore also a new AssemblyLoad listener increasing output on each compilation.Last edited by MojoJojo; 06-16-2020, 10:05 AM.
Comment
-
You obviously know your stuff. My only idea is to use an external dll that will be unaffected by recompilation and therefore keep its static values to communicate between instances. This only matters if the addon is not residing in a DLL itself.
Otherwise I hope the NT will be able to help you out .
Comment
-
Hello aslane,
This request for an event that runs on compile which can reload NinjaScripts is being tracked with ID# SFT-480.
As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.
Release Notes — https://ninjatrader.com/support/help...ease_notes.htmChelsea B.NinjaTrader Customer Service
Comment
-
This would be a most useful state to be able to detect. Did anyone come up with a decent solution for this? It is a right pain for me when developing. If you store data in an add-on and then recompile, the need to go and manually refresh every single chart etc., to try and make sure they all load the same new code is a royal pain! I am currently looking at an external data store, but with all the performance implications,etc.
Comment
-
My vote too.
And some relevant additional reading here: https://ninjatrader.com/support/foru...ning-instances
My own approach is as follows.
In my AddOnBase class:
bool IsRecompilation = false; bool IsNinjaTraderStartup = false;
In my AddOnBase class during invocation:
IsRecompilation = false; IsNinjaTraderStartup = false;
In OnWindowCreated, near the top:
if (Globals.AllWindows.Count == 1) IsNinjaTraderStartup = true;
In OnWindowCreated, when it's the ControlCenter window:
IsRecompilation = !IsNinjaTraderStartup;
if (IsRecompilation) { /* Do whatever you want, then ... */ IsRecompilation = false; }
In OnWindowDestroyed, when it's the ControlCenter window:
IsRecompilation |= Globals.AllWindows.Count > 0;
Completely unsupported, not "perfect", subject to change without notice, of course ... but it works for now. A supported mechanism is preferred.
Thanks.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by The_Sec, Today, 03:53 PM
|
0 responses
1 view
0 likes
|
Last Post
by The_Sec
Today, 03:53 PM
|
||
Started by love2code2trade, Today, 01:45 PM
|
2 responses
13 views
0 likes
|
Last Post Today, 03:29 PM | ||
Started by stafe, 04-15-2024, 08:34 PM
|
4 responses
25 views
0 likes
|
Last Post
by stafe
Today, 03:27 PM
|
||
Started by mmenigma, Today, 03:25 PM
|
0 responses
1 view
0 likes
|
Last Post
by mmenigma
Today, 03:25 PM
|
||
Started by SugarDefenderGetNow, Today, 03:21 PM
|
0 responses
9 views
0 likes
|
Last Post Today, 03:21 PM |
Comment