locked
Process.Exit event is not fire RRS feed

  • Question

  • Hi ,

    i'm working on c# ,i run an  another .exe using Process .But after i  close the .exe, the process.exit event is not fire..

    Dim prcinfo As ProcessInfo = new ProcesInfo(@D:/Folder/File1.exe)
    prcinfo.UseShellExecute= true
    prcinfo.WorkingDirectory=@"filepath"
    prcinfo.argument=@"//d//d/d/d"
    
     Dim process1 As Process= new Process(prcinfo)
     process1.EnableRaisingEvent =true
     process1.Exited += new EventHandler( RuntheMethod11)
     process1.WaitForExit()
     process1.Close()
    


    Thursday, September 29, 2016 10:50 AM

Answers

  • Hmm. I was really wondering about your Code - that is a mix of C# and VB.Net. It should not compile at all.

    Most stuff is VB.Net: The Dim statements, no ";" and so on.

    But the event registration is simply wrong. You do not register an event handler that way in VB.Net. If you did that in VB.Net, then the core problem might be, that you did some code that did something but not register an handler.

    A possible VB.Net Console application could be:

    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            If Environment.GetCommandLineArgs().Length = 1 then
                Console.Out.WriteLine("Parent - starting child")
                Dim info As ProcessStartInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().Location)
                info.UseShellExecute = True
                info.Arguments = "Test"
    
                Dim process As Process = new Process()
                process.StartInfo = info
                process.EnableRaisingEvents = true
                AddHandler process.Exited, AddressOf ProcessExited
                process.Start()
            Else 
                Console.WriteLine("Child!")
            End If
            
            Console.Out.WriteLine("Press enter to exit!")
            Console.In.ReadLine()
        End Sub
    
        Private Sub ProcessExited() 
            Console.WriteLine("Process exited")
        End Sub
    End Module
    

    Important changes to my C# code and your code:

    a) The command line arguments are fetcht through the Environment class. But that also includes the command itself. So instead of checking for 0 arguments, we have to check for "just the command without arguments" so I check for equals 1.

    b) Adding the handler is done with AddHandler and the AddressOf the method that should be called. (Documentation is behind the links!)

    And in case that you want to look at the vb forum: This can be found at
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=vbgeneral

    With kind regards,

    Konrad

    Thursday, September 29, 2016 2:53 PM

All replies

  • Hi digitgeek,

    I think you "EnableRaisingEvent" spelling might be wring. It should be "EnableRaisingEvents". Please check the below article. Hope this helps you.

    Process.EnableRaisingEvents

    Dim prcinfo As ProcessInfo = new ProcesInfo(@D:/Folder/File1.exe)
    prcinfo.UseShellExecute= true
    prcinfo.WorkingDirectory=@"filepath"
    prcinfo.argument=@"//d//d/d/d"
    
     Dim process1 As Process= new Process(prcinfo)
     process1.EnableRaisingEvents =true //A "s" was missing
     process1.Exited += new EventHandler( RuntheMethod11)
     process1.WaitForExit()
     process1.Close()

    Thanks,

    Sabah Shariq

    Thursday, September 29, 2016 11:03 AM
  • sorry ,While typing i missed that ...but in  my code i written  porper(EnableRaisingEvents)

    Thursday, September 29, 2016 11:29 AM
  • Hi,

    I didn't do some tests but the Process.Exited Event documentation writes:

    "There are two ways of being notified when the associated process exits: synchronously and asynchronously. Synchronous notification means calling the WaitForExit method to block the current thread until the process exits. Asynchronous notification uses the Exited event, which allows the calling thread to continue execution in the meantime. In the latter case, EnableRaisingEvents must be set to true for the calling application to receive the Exited event."

    So from my understanding, the Exited Event is not triggered if you call WaitForExit. (And in my eyes, this makes sense, because WaitForExit waits for the process to end so you can do averything you want after that call! You do not need an event!)

    With kind regards,

    Konrad

    Thursday, September 29, 2016 11:48 AM
  • yes...that's correct.i removed the WaitforEvent method ,but Exited event is not fire...

    i have a small confusion , the another process. exe is made of native c++,so is that problem of unmanaged  code is not given response to managed code while exit the process...


    • Edited by digitgeek Thursday, September 29, 2016 1:27 PM
    Thursday, September 29, 2016 1:27 PM
  • Hi,

    can you give more details about the current solution? So please post again the code that you used to start the process, about the time the process you are starting is running and what kind of process is it. And what kind of application did you write?

    Thank you in advance.

    With kind regards,

    Konrad

    Thursday, September 29, 2016 1:46 PM
  • I wrote a simple example console application which starts itself. When the child is closed, a message is printed.

    The example is C# (Not VB.net as your code) because we are inside the C# Forum here. If required, I could provide the VB.net code, too (But then I would also request a moderator to move the thread to the VB.net forum).

    What I saw in your code might be a few more typing mistakes:
    - ProcessInfo seems wrong - Did you use ProcessStartInfo?
    - The Process constructor didn't take the ProcessStartInfo as parameter. So maybe you used it in the Start call? Or you could set the property (which I did in my code).

    So my console application is simply:

    using System;
    using System.Diagnostics;
    using System.Reflection;
    
    namespace ConsoleApplication
    {
        public class Program
        {
            static void Main(string[] args)
            {
                if (args.Length == 0)
                {
                    Console.Out.WriteLine("Parent - starting child!");
    
                    var info = new ProcessStartInfo(Assembly.GetExecutingAssembly().Location);
                    info.UseShellExecute = true;
                    info.Arguments = "Test";
    
                    var process = new Process();
                    process.StartInfo = info;
                    process.EnableRaisingEvents = true;
                    process.Exited += ProcessExited;
                    process.Start();
                }
                else
                {
                    Console.Out.WriteLine("Child!");
                }
                Console.Out.WriteLine("Press enter to exit!");
                Console.In.ReadLine();
            }
    
            private static void ProcessExited(object sender, EventArgs e)
            {
                Console.WriteLine("Process exited");
            }
        }
    }
    

    I simply used the console application as child, too. (So without argument it will be a parent and calls itself with an argument.)

    Tetsing this example worked fine - closing the child first brought the message on the first application.

    With kind regards,

    Konrad

    • Proposed as answer by Christopher84 Thursday, September 29, 2016 2:31 PM
    Thursday, September 29, 2016 2:27 PM
  • Hmm. I was really wondering about your Code - that is a mix of C# and VB.Net. It should not compile at all.

    Most stuff is VB.Net: The Dim statements, no ";" and so on.

    But the event registration is simply wrong. You do not register an event handler that way in VB.Net. If you did that in VB.Net, then the core problem might be, that you did some code that did something but not register an handler.

    A possible VB.Net Console application could be:

    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            If Environment.GetCommandLineArgs().Length = 1 then
                Console.Out.WriteLine("Parent - starting child")
                Dim info As ProcessStartInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().Location)
                info.UseShellExecute = True
                info.Arguments = "Test"
    
                Dim process As Process = new Process()
                process.StartInfo = info
                process.EnableRaisingEvents = true
                AddHandler process.Exited, AddressOf ProcessExited
                process.Start()
            Else 
                Console.WriteLine("Child!")
            End If
            
            Console.Out.WriteLine("Press enter to exit!")
            Console.In.ReadLine()
        End Sub
    
        Private Sub ProcessExited() 
            Console.WriteLine("Process exited")
        End Sub
    End Module
    

    Important changes to my C# code and your code:

    a) The command line arguments are fetcht through the Environment class. But that also includes the command itself. So instead of checking for 0 arguments, we have to check for "just the command without arguments" so I check for equals 1.

    b) Adding the handler is done with AddHandler and the AddressOf the method that should be called. (Documentation is behind the links!)

    And in case that you want to look at the vb forum: This can be found at
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=vbgeneral

    With kind regards,

    Konrad

    Thursday, September 29, 2016 2:53 PM