In the project I use third-party dll. I suspect that there is a leak there, I have another bottleneck

It means so.

  1. I create a stream

  2. In the created thread

    2.1 Create System.Windows.Window

    2.2 I expand to full screen (I take screen sizes and set them in window size)

    2.3 Make the style None and translucency

    2.4 I create a timer, start it and call it at the window ShowDialog ()

    2.4 The "x" hour comes and I make windows close in the timer

This is where the main problem is, if I don’t do a "dry up" flow

win.Hide(); win.Opacity = 1; win.Width = 1; win.Height = 1; win.WindowStyle = WindowStyle.None; win.ResizeMode = ResizeMode.NoResize; win.Topmost = false; 

then memory is accumulating wildly, I don’t understand why the window doesn’t remove itself from memory ...

Below is the class code.

 public class WaitWindows { protected WaitWindows() : base() { } protected object m_StopLock = new object(); protected object m_TextLock = new object(); protected string m_Text = ""; protected bool m_IsStop = false; public string TextMessage { get { string r = ""; lock (m_TextLock) r = m_Text; return r; } set { lock (m_TextLock) { m_Text = value; } } } public void Stop() { lock (m_StopLock) m_IsStop = true; } public bool IsStop { get { bool b; lock (m_StopLock) b = m_IsStop; return b; } } public static WaitWindows Create(bool isStopButton = false, int timeout = 1000) { WaitWindows w = new WaitWindows(); Thread t = new Thread(new ThreadStart(w.ClientsThread)); t.SetApartmentState(ApartmentState.STA); t.Priority = ThreadPriority.Normal; t.Start(); return w; } protected void ClientsThread() { if (IsStop) return; TextBlock text = new TextBlock() { FontSize = 60, FontWeight = FontWeights.Bold, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Text = TextMessage, TextWrapping = TextWrapping.Wrap }; Border border = new Border() { Background = Brushes.White, Child = text }; System.Drawing.Rectangle screenResolution = System.Windows.Forms.Screen.PrimaryScreen.Bounds; Window win = new Window() { AllowsTransparency = true, Opacity = 0.8, Content = border, Left = screenResolution.Left, Top = screenResolution.Top, Width = screenResolution.Width, Height = screenResolution.Height, WindowStyle = WindowStyle.None, ResizeMode = ResizeMode.NoResize, Topmost = true, ShowInTaskbar = false, Background = Brushes.White, }; DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 100) }; timer.Tick += delegate (object sender, EventArgs e) { string t = TextMessage; if (text.Text != t) text.Text = t; if (IsStop) { timer.Stop(); //Следующий код если не делать, то копится память, почему win.Hide();// win.Close(); то же самое win.Opacity = 1; win.Width = 1; win.Height = 1; win.WindowStyle = WindowStyle.None; win.ResizeMode = ResizeMode.NoResize; win.Topmost = false; } }; win.Closing += delegate (object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = !IsStop; }; timer.Start(); win.ShowDialog(); } } 

And even lower test program

 for (int i=0; i < 10;++i){ WaitWindows ww = WaitWindows.Create(); System.Threading.Thread.Sleep(1000); ww.Stop(); } 
  • And if instead of all this code just win.Close() ? - VladD
  • The same thing, I experimented - Dmitry Chistik
  • Unsubscribe from all events when they are no longer needed (closing the window, timer). - kmv
  • kmv all this is done, the timer is stopped, I did not subscribe to events ... - Dmitry Chistik

0