-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();
 }

}
5
  • Nothing happens to MainWindow and its value will be preserved Commented Mar 1, 2023 at 7: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). Commented Mar 1, 2023 at 7:38
  • What is the relationship between MainWindow and automation? Does MainWindow care about automation at all? Commented Mar 1, 2023 at 7: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. Commented Mar 1, 2023 at 7:43
  • I replaced my code. the quesiton is not about the implementation of automation. Commented Mar 1, 2023 at 7:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

this was just a syntactic example. I replaced my code to avoid confusion
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: stackoverflow.com/questions/538060/…
I updated my answer to show how it works without "using" statement. They are equivalents.
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.
I am sorry I can't help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.