4

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

MPelletier
  • 16,256
  • 15
  • 86
  • 137
user254875486
  • 11,190
  • 7
  • 36
  • 65

2 Answers2

2

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
  • 6,405
  • 21
  • 29
  • This sounds okay, however, it is very possible that the script will run synchronous in different threads. – user254875486 Mar 15 '10 at 11:48
  • 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 Mar 15 '10 at 12:14
  • Okay, that sounds good. Do you have any advice on how to create a critical section in VB (syntactically I mean)? – user254875486 Mar 15 '10 at 12:37
  • See this http://stackoverflow.com/questions/915850/lock-statement-in-vb-net. VB.NET use SyncLock statement – Fadrian Sudaman Mar 15 '10 at 13:21
  • Thanks, still looking for better answers though, this workaround will work, but there has to be a better solution..... – user254875486 Mar 15 '10 at 14:59
  • 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 Mar 16 '10 at 05:44
  • I'm trying to implement it now, I noticed that you mistyped Diagnostic, it's Diagnostics.... – user254875486 Mar 17 '10 at 13:44
1

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
  • 4,174
  • 20
  • 31
  • Thanks, maybe I'm gonna try this. I've already implemented Fadrain's suggestion, which works. – user254875486 Mar 18 '10 at 07:47
  • 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 Mar 18 '10 at 15:17
  • 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. – user254875486 Mar 19 '10 at 10:42