I am creating window in another thread. After closing the thread some of the resources window is not released from the memory. Because of this growing counter GDI Objects and User Objects in windows task manager. Graphics that not released are font and region. I haven't idea what is going on...
public class WaitingWindowManager
{
private Thread thread;
private bool canAbortThread = false;
private Window waitingWindow;
public void BeginWaiting()
{
this.thread = new Thread(this.RunThread);
this.thread.IsBackground = true;
this.thread.SetApartmentState(ApartmentState.STA);
this.thread.Start();
}
public void EndWaiting()
{
if (this.waitingWindow != null)
{
this.waitingWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { this.waitingWindow.Close(); }));
while (!this.canAbortThread) { };
}
this.thread.Abort();
}
public void RunThread()
{
this.waitingWindow = new Window();
this.waitingWindow.Closed += new EventHandler(waitingWindow_Closed);
this.waitingWindow.ShowDialog();
}
void waitingWindow_Closed(object sender, EventArgs e)
{
this.canAbortThread = true;
}
}
And call :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WaitingWindowManager waitingWindowManager = new WaitingWindowManager();
waitingWindowManager.BeginWaiting();
Application.Current.Dispatcher.Thread.Join(5000);
waitingWindowManager.EndWaiting();
}
}
Remove your Closed eventhandler in your waitingWindow_Closed Event. It is causing your window to not be disposed. If you manually add events you need to make sure you remove them when finished.
I also noticed another Stackoverflow question that was pertaining to memory leaks in wpf. It referenced this article maybe this will help you.
Add Dispatcher.CurrentDispatcher.InvokeShutdown(); in your closing code. That should take care of any leaking memory.
Related
I need my WPF exe to start monitoring a queue as soon as it starts up, and then respond to messages as they come in.
The way I have it now is:
public partial class App
{
readonly BackgroundWorker _worker = new BackgroundWorker();
protected override void OnStartup(StartupEventArgs e)
{
_worker.DoWork += worker_DoWork;
_worker.RunWorkerCompleted += worker_RunWorkerCompleted;
_worker.RunWorkerAsync();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_worker.DoWork += worker_DoWork;
_worker.RunWorkerAsync();
}
static void worker_DoWork(object sender, DoWorkEventArgs e)
{
//do work
}
Obviously, this is pretty pathetic IMO.
Also, I don't like the approach of using while(true), inside the thread to keep it running indefinitely..
It's enough to read the messages from queue not permanently but periodically.
The common approach for this is using timer. For example, System.Threading.Timer.
public partial class App : Application
{
private System.Threading.Timer _msmqReadTimer;
public App()
{
_msmqReadTimer = new System.Threading.Timer(MsmqReadTimerTick);
_msmqReadTimer.Change(0, 1000); // call MsmqReadTimerTick immediatelly and each 1000 ms
}
private void MsmqReadTimerTick(object state)
{
// do work
// if you want to update some UI components after work, you should post this to UI thread dispatcher:
this.Dispatcher.Invoke(()=>
{
// logic for updating UI should be here
},
System.Windows.Threading.DispatcherPriority.Background);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Thread HeartRateThread = new Thread(startThread);
HeartRateThread.Name = "Class1";
HeartRateThread.Start();
}
private void startThread(object obj)
{
new Class1();
}
}
public class Class1
{
public Class1()
{
DispatcherTimer timer1 = new DispatcherTimer();
timer1.Interval = new TimeSpan(0,0,0,1);
timer1.Tick += timer1_tick;
timer1.Start();
}
private void timer1_tick(object sender, EventArgs e)
{
Debug.WriteLine("timer called");
}
}
I am trying to enable this timer_tick function fromanother thread as it is obvious in the code section of maInWindow. However, the Class1 constructor is called but timertick functin is not enabled. However if i do this on the main thread, everything works fine. Any reason for this.And how can I get it working?
DispatcherTimer can only work run on a UI thread. However, in your case you are creating a DispatcherTimer on a background thread. DispatcherTimer, internally tries to get Dispatcher.CurrentDispatcher, in your case it gets dispatcher for the background thread, not for the main UI thread.
Do you really need DispatcherTimer? If you are not going to manipulate any UI elements in the timer1_tick method, then you are better off to go with a different timer, like System.Timers.Timer.
Refer to this to read more about available Timers in .net.
Maybe you can try something like this:
private void timer1_tick(object sender, EventArgs e)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => Debug.WriteLine("timer called")));
}
Without having tested it, I guess you have to pass the MainWindow's Dispatcher to the DispatcherTimer on construction. Otherwise it will create its own:
private void startThread(object obj)
{
new Class1(Dispatcher);
}
...
public Class1(Dispatcher dispatcher)
{
DispatcherTimer timer1 =
new DispatcherTimer(DispatcherPriority.Background, dispatcher);
timer1.Interval = new TimeSpan(0,0,0,1);
timer1.Tick += timer1_tick;
timer1.Start();
}
you can use Dispatcher for call startThread method.
object objParameter = "parametervalue";
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(
() => startThread(objParameter)));
I have some window, which can have many instances opened in single application.
I track all of them in a static dictionary.
The window must be closed when escape pressed if the window is active. And if the window is closed through escape i need activate other window, stored in a dictionary if there is one. I also need to consider current windows z-order and activate the top most of them, but for now it doesn`t matter.
So, when i have some windows opened and try to close them subsequently pressing escape what i get is that at some moment all of the windows left are closed simultaneously.
Here is the code example:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private int _count;
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1.Run(this, ++_count);
}
}
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private static Dictionary<int, Window1> _opened =
new Dictionary<int, Window1>();
private int _key;
public Window1()
{
InitializeComponent();
KeyDown += OnKeyDown;
}
private void OnKeyDown(object sender, KeyEventArgs ea)
{
if (ea.Key == Key.Escape)
{
Close();
}
}
public static void Run(Window owner, int key)
{
Window1 w = null;
if (_opened.TryGetValue(key, out w))
{
w.Activate();
}
else
{
w = new Window1{_key = key};
w.Closed += (s, e) =>
{
var win = s as Window1;
_opened.Remove(win._key);
if (_opened.Count > 0)
{
_opened.First().Value.Activate();
}
};
_opened.Add(key, w);
w.Show();
}
}
}
}
Update#1
Thanks to Potecaru Tudor, he found another solution:
The solution I found was to set e.Handled = true in your KeyDown
handler after calling Close()
Solution Code:
private void OnKeyDown(object sender, KeyEventArgs ea)
{
if (ea.Key == Key.Escape)
{
ea.Handled = true;
Close();
}
}
Update#2
And here is another, not so elegant solution, just for the record
...
_opened.Remove(win._key);
if (_opened.Count > 0)
{
// i suppose here is the error hidden
var w2 = _opened.First().Value;
w2.Dispatcher.BeginInvoke(new Action(() => w2.Activate()));
}
...
I tried to reproduce your issue with no success, the windows are closing just fine one by one. You could try calling the Close() method only if Window.IsActive property is set on true.
EDIT I managed to reproduce your issue.
The solution I found was to set e.Handled = true in your KeyDown handler after calling Close()
I suppose the issue is that the windows become active one after another before the first one gets to be closed, so they also received the KeyDown event firing. The weird thing is it only happens in my sample for the first windows you're opening. After you're closing all of them and start opening others this will not reproduce.
Only way i have found is to delay next window activation, like this:
...
_opened.Remove(win._key);
if (_opened.Count > 0)
{
// i suppose here is the error hidden
var w2 = _opened.First().Value;
w2.Dispatcher.BeginInvoke(new Action(() => w2.Activate()));
}
...
Any other suggestions would be appreciated! Thank you!
In my application's Business Logic layer I have the following classes:
public class EocMonitor : DeviceMonitor {
public BackgroundWorker BackendWorker { get; set; }
public BackgroundWorker EocWorker { get; set; }
public EocMonitor() {
BackendWorker = new BackgroundWorker {
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
BackendWorker.DoWork += BackendWorker_DoWork;
EocWorker = new BackgroundWorker {
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
EocWorker.DoWork += EocWorker_DoWork;
}
private void BackendWorker_DoWork( object sender, DoWorkEventArgs e ) {
// Does some lengthy operation
}
void EocWorker_DoWork( object sender, DoWorkEventArgs e ) {
// Does some lengthy operation
}
public void GetDiagnostics() {
BackendWorker.RunWorkerAsync( new DiagnosticsInfo() );
EocWorker.RunWorkerAsync( new DiagnosticsInfo() );
}
}
public class DiagnosticsInfo {
public int DataTypeCount { get; set; }
public int DataTypesProcessed { get; set; }
}
The BackgroundWorkers are used to query information over the wire from 2 other processes running in my application. The responses can take a while to come back. Plus the data can take a while to come back.
I have a WPF UserControl in my application's main window called Dashboard. The Dashboard has a DataGrid on it that displays the results of the lengthy operations. Because they are lengthy, it also has a Button on it called Refresh that starts the process off. And, because it can take a long time to run, there's a UserControl I wrote called a ProgressControl on the form. This consists of a Cancel Button, a ProgressBar, and a TextBlock where messages can be displayed. When the user clicks on the Cancel Button, the refresh stops.
Here's some code from Dashboard:
public partial class Dashboard : UserControl {
public Dashboard() {
InitializeComponent();
}
private Dashboard_Loaded( object sender, RoutedEventArgs e ) {
if ( !setupProgress && EocMonitor != null ) {
EocMonitor.BackendWorker.ProgressChanged += BackendWorker_ProgressChanged;
EocMonitor.BAckendWorker.RunWorkerCompleted += BackendWorker_RunWorkerCompleted;
EocMonitor.EocWorker.ProgressChkanged += EocWorker_ProgresChanged;
EocMonitor.EocWorker.RunWorkerCompleted += EocWorker_RunWorkerCompleted;
}
}
private void BackendWorker_ProgressChanged( object sender, ProgressChangedEventArgs e ) {
DiagnosticsInfo info = e.UserState as DiagnosticsInfo;
// Other processing to notify the user of the progress
}
private void BackendWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) {
// Processing to do when the back-ground worker is finished
}
private void DiagnosticsProgressCtrl_Click( object sender, RoutedEventArgs e ) {
EocMonitor.BackendWorker.CancelAsync();
EocMonitor. EocWorker.CancelAsync();
DiagnosticsProgressCtrl.Visibility = Visibility.Collapsed;
e.Handled = true;
}
void EocWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) {
// Processing to do when the back-ground worker is finished
}
private void RefreshButton_Click( object sender, RoutedEventArgs e ) {
DiagnosticsProgressCtrl.Maximum = DiagnosticsProgressCtrl.Minimum = DiagnosticsProgressCtrl.Value = 0.0;
DiagnosticsProgressCtrl.Visibility = Visibility.Visible;
backendDataTypeCount = eocDataTypeCount = 0;
backendWorkerCompleted = eocWorkerCompleted = false;
EocMonitor.GetDiagnostics();
e.Handled = true;
}
}
The problem is that I have placed breakpoints in the DoWork methods and watched them run to completion, yet the RunWorkerCompleted methods are not being called. No errors are occurring or being thrown. This thing is the EocMonitor class and the Dashboard class are in two different DLLs. Does that make a difference? As far as I know it shouldn't, but I don't understand why the completed event handlers aren't getting called. Should I instantiate the BackgroundWorkers in the front-end application?
Tony
The event is raised, but you don't see it because you didn't subscribe to the RunWorkerCompleted event...
BackendWorker.RunWorkerCompleted += BackendWorker_RunWorkerCompleted;
EocWorker.RunWorkerCompleted += EocWorker_RunWorkerCompleted;
Well, after I posted the above, I went back and changed things a bit. I now instantiate the BackgroundWorker objects in the Dashboard control and pass them to the EocMonitor's GetDiagnostics method. The properties in EocMonitor that hold these objects have private setters, so the only way to use them is to create them & pass them to that method. The code in the Dashboard_Loaded is now moved in the RefreshButton_Click method and runs after the objects are instantiated, before they're passed to GetDiagnostics.
This all works now! I see the Progress_Changed methods and the RunWorkerCompleted methods run.
It just hit me why it's probably not working. The EocMonitor object is created on a non UI thread during my program's initalization phase. Since it's calling methods in a UI object, the methods probably can't be called. An Invalid operation exception of some sort is probably being thrown, but there's no place to catch it.
So let that be a lesson: The BackgroundWorker has to be instantiated in code on the UI thread.
I'm getting this Exception
System.InvalidOperationException was
unhandled by user code Message="The
calling thread cannot access this
object because a different thread owns
it."
whenever I run the following code
public partial class MainScreen : Window
{
Timer trm;
public MainScreen()
{
InitializeComponent();
trm = new Timer(1000);
trm.AutoReset = true;
trm.Start();
trm.Elapsed += new ElapsedEventHandler(trm_Elapsed);
}
void trm_Elapsed(object sender, ElapsedEventArgs e)
{
lblTime.Content = System.DateTime.Now;
}
}
guys any solution... I badly wann come out of it :(
Use DispatcherTimer instead:
public partial class MainScreen : Window{
DispatcherTimer tmr;
public MainScreen() {
InitializeComponent();
tmr = new DispatcherTimer();
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Start();
}
void tmr_Tick(object sender, EventArgs e) {
lblTime.Content = System.DateTime.Now;
}
}
Any time you modify Windows controls you must do so on the UI thread (the one that created the controls).
See this question for lots of details.
To be short, you should use Dispatcher.Invoke method to update UI elements.