[RESOLVED] Unload on console possible?
I have aconsole application that executes 2 other processes, and then closes the processes once it's finished its work.
If the user closes the console application prematurely, the 2 other processes go rogue and sit idle (As they are expecting to be closed by the console).
Is it possible to detect when the user clicks the X or closes the console app in another fashion, so I can also destroy the spawned processes?
Re: Unload on console possible?
You could create a fork maybe, that keeps checking the process list for cmd.exe and as long as it's still there, just continue the for every so often. When it's not detected, kill the other two processes and finish the forked sub.
I say fork, because I think a thread runs on the same process as the application that created it.
Justin
Re: Unload on console possible?
Quote:
Originally Posted by
MonkOFox
You could create a fork maybe, that keeps checking the process list for cmd.exe and as long as it's still there, just continue the for every so often. When it's not detected, kill the other two processes and finish the forked sub.
I say fork, because I think a thread runs on the same process as the application that created it.
Justin
So basically create another process that watches the first one? Then close itself & the spawned program when it no longer detects my original application's PID or something?
Could you point me in the right direction to create this, I mean, without having to create an entire new program.
Re: Unload on console possible?
Um... I've never built a console application... but do console apps have events? If so, it most likely has a closing event, which can check whether or not the app is closing. Then you can close the other apps if that's the case.
If not events, there has to be something like it. I'll do a Google search.
Re: Unload on console possible?
Eureka!
It turns out it's way harder in console applications, but this link should give you what you need.
Re: Unload on console possible?
Re: Unload on console possible?
Quote:
Originally Posted by
MaximilianMayrhofer
Your link is t3h broken.
ONOE!!!
Well the OP can do as I did and search Google.
Re: Unload on console possible?
I happen to know which competing forum weirddemon linked to, so here's the code that was posted in that thread:
Code:
Imports System.Threading
Module Module1
Declare Function SetConsoleCtrlHandler Lib "kernel32.dll" ( _
ByVal HandlerRoutine As ControlEventHandler, _
ByVal Add As Int32) As Int32
Public Enum ConsoleEvent
CTRL_C = 0
CTRL_BREAK = 1
CTRL_CLOSE = 2
CTRL_LOGOFF = 5
CTRL_SHUTDOWN = 6
End Enum
Public Delegate Sub ControlEventHandler(ByVal consoleEvent As ConsoleEvent)
Public Sub OnControlEvent(ByVal consoleEvent As ConsoleEvent)
Console.WriteLine("Event: {0}", consoleEvent)
t.Abort()
Console.WriteLine("Thread closing gracefully in 1000ms")
Threading.Thread.Sleep(1000)
End Sub
Dim t As New Thread(AddressOf looping)
Sub Main()
SetConsoleCtrlHandler(New ControlEventHandler(AddressOf OnControlEvent), True)
t.Start()
Console.ReadKey()
t.Abort()
t.Join()
End Sub
Sub looping()
Do
Console.WriteLine("Thread - working.")
Thread.Sleep(100)
Loop
End Sub
End Module
Re: Unload on console possible?
Thanks jmcilhinney :D.
I'll give that a try when I next work on it.
Lol, why would you give the link but block the domain weirddemon? If I knew what to search for I wouldn't have asked :p.
Got a quick question, are public variables and arrays passed into the new thread as well? Or do I have to pass them as a arguement in the sub's declaration?
Re: Unload on console possible?
Quote:
Originally Posted by
Slyke
Lol, why would you give the link but block the domain weirddemon? If I knew what to search for I wouldn't have asked :p.
It was the site, not the poster. It's posted at a competing VB.NET forum site so it's masked out by the server.
It doesn't really take much of a stretch to work out what to search for:
http://www.google.com.au/search?hl=e...=&oq=&gs_rfai=
Re: Unload on console possible?
Quote:
Originally Posted by
Slyke
Got a quick question, are public variables and arrays passed into the new thread as well? Or do I have to pass them as a arguement in the sub's declaration?
Data doesn't exist on any specific thread. If it's a local variable then it's available to the current method only. If it's a member variable then it's available to any method. Threads have no bearing on those facts.
Re: Unload on console possible?
Sweet. This is working great :D.
I get different Google results then you guys I think, as I checked the first 5 pages and there's no URL that looks like the one he posted XD.
He gets rep for trying though :).
Re: Unload on console possible?
Quote:
Originally Posted by
Slyke
Sweet. This is working great :D.
I get different Google results then you guys I think, as I checked the first 5 pages and there's no URL that looks like the one he posted XD.
He gets rep for trying though :).
I Googled this: "vb .net console app closing event"
Result number 3 gave me this: v b d o t n e t f o r u m s . c o m /console-application/13920-event-handler-closing-console.html
Which led me to this: v b d o t n e t f o r u m s . c o m /console-application/16951-close-style.html
Re: [RESOLVED] Unload on console possible?
Yah. Result 2 & 3 for me goes to some page in bytes.com.
Very strange. Probably geo-location or something.