I'm experiencing a weird error. I've developed a custom AddOn window as a PopUp Dialog. I'm calling it from within an indicator and sometimes this indicator throws the error that some other thread owns this object already. I cannot replicate it reliably, but I can narrow it down to the general use of that custom NTWindow and the witch between different charts or tabs. As far as I've observed it, it doesn't matter whether a PopUp window ist shown or not, i.e. if it was once shown then closed this error still could occur. DO I have to handle switches between tabs or something similar?
This is how the window gets called:
Globals.RandomDispatcher.InvokeAsync(new Action(() =>
{
if (PopUp != null)
PopUp.Close();
PopUp = new PopUpWindow()
{
Caption = "SuperTrenderMA Pullback Alert",
message = message,
TextColor = ChartControl != null ? ChartControl.Properties.ChartText : Brushes.Black
};
PopUp.Show(); // open the window
PopUp.Activate(); // bring to the top
}));
namespace NinjaTrader.NinjaScript.AddOns
{
public class PopUpWindow : NTWindow
{
private Button okButton;
private TextBlock messageBlock;
public string message;
public Brush TextColor;
public PopUpWindow()
{
Caption = "PopUp v1.0.1";
Width = 200;
Height = 120;
TextColor = Brushes.Black;
Loaded += OnWindow_Loaded;
Closing += OnWindow_Close;
}
private void OnWindow_Loaded(object sender, RoutedEventArgs e)
{
Content = LoadXaml();
}
private void OnWindow_Close(object sender, System.ComponentModel.CancelEventArgs e)
{
if (okButton != null)
okButton.Click -= OnOkButton_Click;
}
private DependencyObject LoadXaml()
{
Page page = new Page();
FileStream fs = new FileStream(System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, @"bin\Custom\AddOns\PopUpWindowContent.xaml"), FileMode.Open);
page = (Page)XamlReader.Load(fs);
if (page == null)
return null;
messageBlock = LogicalTreeHelper.FindLogicalNode(page, "MessageBlock") as TextBlock;
if (messageBlock != null)
{
messageBlock.Text = message;
messageBlock.Foreground = TextColor;
}
okButton = LogicalTreeHelper.FindLogicalNode(page, "OkButton") as Button;
if (okButton != null)
okButton.Click += OnOkButton_Click;
DependencyObject pageContent = page.Content as DependencyObject;
return pageContent;
}
private void OnOkButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}

Comment