-1
class A
{
  TypeX PropertyX;

  void SomeMethod()
  {
    using (DisposableType y = new DisposableType())
    {
      PropertyX = y.GetX();
    }
  }
}

What happens to PropertyX when Y is being disposed? Would I rather do this, if I don't know what is being disposed with Y?

class A : IDisposable
{
  TypeX PropertyX { get; set;}
  DisposableType Y { get; set; }

  void SomeMethod()
  {
    using (Y = new DisposableType())
    {
      PropertyX = Y.GetX();
    }
  }

 void Dispose()
 {
   Y.Dispose();
 }

}
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Nothing happens to `MainWindow` and its value will be preserved – Hossein Sabziani Mar 01 '23 at 07:33
  • 2
    the purpose of a `using` isn't to dispose everything *within* the brackets, but just the *used* instance, in your case the instance of `UIA2Automation`. So `MainWindow` stays untouched, however the provided `automation` will be disposed and thus the member within your window (if it has such a member). – MakePeaceGreatAgain Mar 01 '23 at 07:38
  • What is the relationship between `MainWindow` and `automation`? Does `MainWindow` care about `automation` at all? – Sweeper Mar 01 '23 at 07:40
  • I don't know the relation between MainWindow and automation. That was just a syntactic example. Say I don't know if MainWindow will refer to something that is being disposed by the using statement. – Piglet Mar 01 '23 at 07:43
  • I replaced my code. the quesiton is not about the implementation of automation. – Piglet Mar 01 '23 at 07:59

1 Answers1

2

EDIT: OP changed the question

Your MainWindow will not get disposed, but automation instance will get disposed after the execution leaves using block. Another way to write this would be:

    using var automation = new UIA2Automation();
    MainWindow = launcher.App.GetMainWindow(automation);

A more verbose way to write the same thing would be:

    var automation = new UIA2Automation();
    MainWindow = launcher.App.GetMainWindow(automation);
    
    // At this point MainWindow is already instantiated.
    // We no longer need automation instance so we dispose of it.
    automation.Dispose();

Quick Google search lead me to FlaUI project and it looks like it is what you are using. Looking at the code samples, it looks like your approach is the correct one.

emt
  • 77
  • 8
  • this was just a syntactic example. I replaced my code to avoid confusion – Piglet Mar 01 '23 at 07:58
  • Nothing happens to property X. Only Y is disposed. Using pattern is just a fancy way to call .Dispose() on your IDisposable's once the execution leaves the using scope. So you either use "using(var x = new SomethingDisposable)" or you handle it yourself by calling the .Dispose(). No need to do both. I would look into following SO question: https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface – emt Mar 01 '23 at 08:04
  • I updated my answer to show how it works without "using" statement. They are equivalents. – emt Mar 01 '23 at 08:08
  • In know what using is doing and that my property is not being disposed by it. that's in the manual. the question what to do if I don't know wether Y will dispose something my member refers to. – Piglet Mar 01 '23 at 08:26
  • I am sorry I can't help you. – emt Mar 01 '23 at 10:51