Results 1 to 14 of 14

Thread: [RESOLVED] Unload on console possible?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [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?

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Unload on console possible?

    Quote Originally Posted by MonkOFox View Post
    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.

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Unload on console possible?

    Your link is t3h broken.

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Unload on console possible?

    Quote Originally Posted by MaximilianMayrhofer View Post
    Your link is t3h broken.
    ONOE!!!

    Well the OP can do as I did and search Google.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Unload on console possible?

    Thanks jmcilhinney .

    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 .

    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?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Unload on console possible?

    Quote Originally Posted by Slyke View Post
    Lol, why would you give the link but block the domain weirddemon? If I knew what to search for I wouldn't have asked .
    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=
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Unload on console possible?

    Quote Originally Posted by Slyke View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Unload on console possible?

    Sweet. This is working great .

    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 .

  13. #13
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Unload on console possible?

    Quote Originally Posted by Slyke View Post
    Sweet. This is working great .

    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
    Last edited by weirddemon; May 25th, 2010 at 03:59 PM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width