0

I am working on a custom keycasting program for tut videos and I am using MouseKeyHook and I am using the example code found here: https://github.com/gmamaladze/globalmousekeyhook/blob/vNext/Demo/Main.cs to get the basic construction working.

As the example was intended for win forms I am having trouble with one line in particular. I have made everything work by omitting - if (IsDisposed) return; line 176.

How do i replicate this code for wpf?

 private void Log(string text)
    {
       if (IsDisposed) return;
        textBoxLog.AppendText(text);
        textBoxLog.ScrollToLine(textBoxLog.LineCount - 1);
    }

EDIT: This was not related to garbage collection it is because if the form is disposed textBoxLog will throw a ObjectDisposedException.

drume
  • 33
  • 10
  • There is extensive discussion on MSDN and on Stack Overflow (including the marked duplicate) explaining `IDisposable`, what it's used for, how to use it, etc. and of course understanding all that will ensure you also understand what the `IsDisposed` property means and why you would or would not use it. – Peter Duniho Sep 05 '16 at 16:39
  • @PeterDuniho My question was how to replicate it in wpf. not so much what it is. but thanks. If i ever use win forms know where to go. – drume Sep 05 '16 at 16:50
  • Your question is about `IDisposable`, and `IDisposable` isn't about WPF or Winforms. That your object in question happens to be a Winforms object is irrelevant. Any object that implements `IDisposable` could also implement an `IsDisposed` property, and it would have the same meaning. And understanding how that property is used and whether you might need to use it in your code is answered easily by yourself once you know what `IDisposable` is for (and what it's not for). – Peter Duniho Sep 05 '16 at 16:53
  • 1
    By the way, based on the question you posted here, I suspect that you have tried to convert your Winforms code to WPF code by simply transliterating the Winforms code into what you think are the directly corresponding equivalents in WPF. But doing so, you will wind up with not a WPF program but rather just a Winforms program that uses the WPF API instead. In the long run, you will get much better results by spending the time to learn WPF, and then also spending the time to learn what the program you're trying to convert actually _does_, and reimplement that using correct WPF idioms. – Peter Duniho Sep 05 '16 at 16:55
  • btw @PeterDuniho the answer is not on your marked duplicate, could you kindly unmark it so. ty – drume Sep 05 '16 at 17:06
  • The answer most certainly is on the marked duplicate. You even agree, because you've accepted as an answer to _this_ question an answer that basically is just telling you the same thing the marked duplicate tells you, i.e. that `IDisposable` doesn't involve garbage collection, and that `IsDisposable` is just protecting against an exception should you try to use a disposed object. – Peter Duniho Sep 05 '16 at 17:09
  • That said, I would argue that the second half of the answer you accepted, you would be foolish to actually use, because it just perpetuates the mistaken idea that you actually _want_ your WPF code to work just like the Winforms code...but that's your own choice. Scott is doing you no favors at all by helping you on this cargo-cultish quest to convert your Winforms code to WPF code. – Peter Duniho Sep 05 '16 at 17:10
  • Dude, you need to chill out! I accepted his answer because he explained what was happening and how to replicate it if I want. It is correct! I am using my program personally so will just continue to omit it, And i totes can't see the answer on the dup page. I see an explanation on what it is for, an expansion on the explanation its for, and links to implement it in winforms. – drume Sep 05 '16 at 17:16

1 Answers1

1

It's not for garbage collection, it is because if the form is disposed textBoxLog will throw a ObjectDisposedException if you try to call AppendText or ScrollToLine after the form has been disposed and Log gets called after the fact.

WPF windows and controls are not disposable like winforms is, however if you want to recreate the behavior just override to the OnClosed method and set a flag.

private bool _isClosed = false;   

protected override void OnClosed(EventArgs e)
{
    _isClosed = true;
    base.OnClosed(e);     
}

private void Log(string text)
{
   if (_isClosed) return;
    textBoxLog.AppendText(text);
    textBoxLog.ScrollToLine(textBoxLog.LineCount - 1);
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • So 67mb memory usage is okay? – drume Sep 05 '16 at 16:39
  • Yes, WPF is a lot "Heavier" than winforms. 76mb is perfectly normal for a GUI application. The thing you should be concerned about is if you see that number constantly growing and never leveling off to a stable number. – Scott Chamberlain Sep 05 '16 at 16:40
  • @drume if you are truly concerned about what is using your memory in the program then use the [Memory Profiler](https://blogs.msdn.microsoft.com/visualstudioalm/2014/11/13/memory-usage-tool-while-debugging-in-visual-studio-2015/) built in to visual studio or use a 3rd party one like [DotMemory](https://www.jetbrains.com/dotmemory/) which offers a 5 day trial. – Scott Chamberlain Sep 05 '16 at 16:46