tags:

views:

393

answers:

2

Hi!

I'm using VB.NET for a web-application that starts some process using CreateObject, like this:

Dim AVObject = CreateObject("avwin.application")

After all is done everything get closed down en stopped using the proper release functions, however, for some reason the process remains.

Is there some way in which I can get the process id of the started process, in order to explicitly kill it just before termination?

Thanks

+1  A: 

I never come across a specific solution to this problem, but logically I will create a workaround using below pseudo logic (I think it should work in a controlled environment)

  • Wrap the CreateObject in say CustomCreateObject(...)
  • In the method implements below in the same sequence
  • Call System.Diagnostics.Process.GetProcessesByName(nameOfYourProcess), stores all the process ids
  • Call CreateObject (...)
  • Call GetProcessesByName again and now get the new list of process ids
  • Process id that is in the later list and not in the first one, will be the one you are after. Return that so that the caller know the process id to monitor.
Fadrian Sudaman
This sounds okay, however, it is very possible that the script will run synchronous in different threads.
Lex
as long as you put these set of code in a critical section (lock) as an atomic operation and centralized all your application creation here, it should be then be thread safe
Fadrian Sudaman
Okay, that sounds good. Do you have any advice on how to create a critical section in VB (syntactically I mean)?
Lex
See this http://stackoverflow.com/questions/915850/lock-statement-in-vb-net. VB.NET use SyncLock statement
Fadrian Sudaman
Thanks, still looking for better answers though, this workaround will work, but there has to be a better solution.....
Lex
Great that it works for you. I will be keen to know if there is a cleaner solution. This is not an uncommon problem at all in system that requires integration to external programs.
Fadrian Sudaman
I'm trying to implement it now, I noticed that you mistyped Diagnostic, it's Diagnostics....
Lex
I just fixed the typo.
Fadrian Sudaman
A: 

Hi.

After all is done everything get closed down en stopped using the proper release functions, however, for some reason the process remains.

Have you tried to force a GC or called ReleaseCOMObject() method?

Best

Adriano

Vagaus
Thanks, maybe I'm gonna try this. I've already implemented Fadrain's suggestion, which works.
Lex
I tried this approach, it doesn't stop the processes..
Lex
Interesting. COM servers should go when its reference count drops to zero. Maybe you have some other object holding references to it? What happens if you just call ReleaseCOMObject() right after instantiating the component (without calling any methods / properties in the component) ?
Vagaus
The same result, doesn't stop.... As I've mentioned in a comment to nobugz' post, on my local test environment it does work (the COM server stops), but on the live server it doesn't stop.
Lex