c# - About Async Tasks and Disposal -


in mainwindow have button can used open process (native openprocess call) , perform checks on it's memory, method called on click asynchronous:

<button content="attach" click="onclickattach"/>  private async void onclickattach(object sender, routedeventargs e) {     attachmentresult result = await m_viewmodel.attach();      switch (result)         // different messagebox depending on result. } 

now, let's see viewmodel portion of code...

// memoryprocess class wrapper process' handle , memory regions. private memoryprocess m_memoryprocess;  public async task<attachmentresult> attach() {     attachmentresult result = attachmentresult.success;     memoryprocess memoryprocess = nativemethods.openprocess(m_selectedbrowserinstance.process);      if (memoryprocess == null)         result = attachmentresult.failprocessnotopened;     else     {         boolean check1 = false;         boolean check2 = false;          foreach (memoryregion region in memoryprocess)         {             // perform checks on process' memory regions , change value of check1 or check2...             await task.delay(1);         }          if (!check1 && !check2)         {             nativemethods.closehandle(memoryprocess.handle);             result = attachmentresult.failprocessnotvalid;         }         else         {             // keep process opened further use. save private variable.             m_memoryprocess = memoryprocess;             m_memoryprocess.check1 = check1;             m_memoryprocess.check2 = check2;         }     }      return result; } 

now... here comes problem. when user closes application, if process opened, must close handle. in mainwindow have following code:

protected override void onclosing(canceleventargs e) {     m_viewmodel.detach();     base.onclosing(e); } 

and in viewmodel have following code:

public void detach() {     if (m_memoryprocess != null)     {         if (m_memoryprocess.check1)             // something...          if (m_memoryprocess.check2)             // something...          nativemethods.closehandle(m_memoryprocess.handle);         m_memoryprocess = null;     } } 

the attach() method can take long time, more 2 minutes sometimes. need find solution following issues:

  • if user closes application while attach() method running , before memoryprocess saved private variable, process handle not closed.
  • if save memoryprocess instance private variable @ beginning of attach() method, there risk user nullreferenceexception if closes application while attach() method processing foreach loop.
  • i absolutely don't want make user wait attach() method complete before letting him close application. that's horrible.

how can this?

imo, if not explicitly , target create separate detached/independent processes like, example, through:

  • using pinvoke.createprocess
  • using

    (new system.management.managementclass("win32_processstartup")) .properties["createflags"].value = 8; 
  • or maintaining child process alive upon app closing launching them through separate shell scripts or other processes remaining run after app closing;

  • creating new thread in independent process using createremotethread
  • etc.

or finding run independently processes, don't need , probably should not "close" or dispose spawned app processes. windows (operting system) close unclosed spawned app processes.

also, believe impossible execute code in application once has started exiting or being closed.

ps (off-topic comment):
not see close (really 1 should kill) or dispose processes in code...


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -