cmd.exe doesn't work unless....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Primo

    cmd.exe doesn't work unless....

    Hi,

    I'm relatively new to C# so apopogies in advance if I am missing something
    obvious. I am trying to run a command, which I would normally run from the
    command line, from within my C# application. Here is my code:

    private void FileMonitor_Cha nged(object sender,
    System.IO.FileS ystemEventArgs e)
    {
    string renamed = e.FullPath;

    System.Diagnost ics.Process process1;
    process1= new System.Diagnost ics.Process();
    process1.Enable RaisingEvents = false;
    string strCmdLine;

    strCmdLine = "/C Dumper.exe \"" + renamed + "\"";

    // MessageBox.Show ("");

    process1.StartI nfo.FileName = "CMD.exe";
    process1.StartI nfo.Arguments = strCmdLine;
    process1.StartI nfo.UseShellExe cute = false;
    process1.StartI nfo.RedirectSta ndardOutput = true;
    process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
    process1.StartI nfo.CreateNoWin dow = true;
    process1.Start( );

    StreamReader myStreamReader = process1.Standa rdOutput;
    string rdfID = myStreamReader. ReadLine();
    process1.Close( );
    }

    The command fails to execute. However, if I were to uncomment
    "MessageBox.Sho w("");" then the command executes correctly. I do not want to
    pause my application with a message box, however.

    Any ideas as to where I am going wrong? Many thanks in advance.
  • Shiva

    #2
    Re: cmd.exe doesn't work unless....

    MessageBox.Show () does not return until the user has responded to it. So,
    the subsequent lines will not run until the messagebox is dismissied.

    "Primo" <Primo@discussi ons.microsoft.c om> wrote in message
    news:B2E99CC6-BFD1-4EBE-AC0A-29E07562ECF3@mi crosoft.com...
    Hi,

    I'm relatively new to C# so apopogies in advance if I am missing something
    obvious. I am trying to run a command, which I would normally run from the
    command line, from within my C# application. Here is my code:

    private void FileMonitor_Cha nged(object sender,
    System.IO.FileS ystemEventArgs e)
    {
    string renamed = e.FullPath;

    System.Diagnost ics.Process process1;
    process1= new System.Diagnost ics.Process();
    process1.Enable RaisingEvents = false;
    string strCmdLine;

    strCmdLine = "/C Dumper.exe \"" + renamed + "\"";

    // MessageBox.Show ("");

    process1.StartI nfo.FileName = "CMD.exe";
    process1.StartI nfo.Arguments = strCmdLine;
    process1.StartI nfo.UseShellExe cute = false;
    process1.StartI nfo.RedirectSta ndardOutput = true;
    process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
    process1.StartI nfo.CreateNoWin dow = true;
    process1.Start( );

    StreamReader myStreamReader = process1.Standa rdOutput;
    string rdfID = myStreamReader. ReadLine();
    process1.Close( );
    }

    The command fails to execute. However, if I were to uncomment
    "MessageBox.Sho w("");" then the command executes correctly. I do not want to
    pause my application with a message box, however.

    Any ideas as to where I am going wrong? Many thanks in advance.


    Comment

    • Ignacio Machin \( .NET/ C#  MVP \)

      #3
      Re: cmd.exe doesn't work unless....

      Hi,

      The existence of that line does not imply nothing , unless that you are
      trying to access a file (the method name suggest it's a response to a
      FileSystemWatch er event) and the file is still being used by another
      program. in this escenario the MessageBox has a delay effect while it the
      file may get closed by the other application and it works fine.

      try to replace the MessageBox for a Thread.Sleep and see what happen

      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Primo" <Primo@discussi ons.microsoft.c om> wrote in message
      news:B2E99CC6-BFD1-4EBE-AC0A-29E07562ECF3@mi crosoft.com...[color=blue]
      > Hi,
      >
      > I'm relatively new to C# so apopogies in advance if I am missing something
      > obvious. I am trying to run a command, which I would normally run from the
      > command line, from within my C# application. Here is my code:
      >
      > private void FileMonitor_Cha nged(object sender,
      > System.IO.FileS ystemEventArgs e)
      > {
      > string renamed = e.FullPath;
      >
      > System.Diagnost ics.Process process1;
      > process1= new System.Diagnost ics.Process();
      > process1.Enable RaisingEvents = false;
      > string strCmdLine;
      >
      > strCmdLine = "/C Dumper.exe \"" + renamed + "\"";
      >
      > // MessageBox.Show ("");
      >
      > process1.StartI nfo.FileName = "CMD.exe";
      > process1.StartI nfo.Arguments = strCmdLine;
      > process1.StartI nfo.UseShellExe cute = false;
      > process1.StartI nfo.RedirectSta ndardOutput = true;
      > process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
      > process1.StartI nfo.CreateNoWin dow = true;
      > process1.Start( );
      >
      > StreamReader myStreamReader = process1.Standa rdOutput;
      > string rdfID = myStreamReader. ReadLine();
      > process1.Close( );
      > }
      >
      > The command fails to execute. However, if I were to uncomment
      > "MessageBox.Sho w("");" then the command executes correctly. I do not want
      > to
      > pause my application with a message box, however.
      >
      > Any ideas as to where I am going wrong? Many thanks in advance.[/color]


      Comment

      • Primo

        #4
        Re: cmd.exe doesn't work unless....

        Hi Ignacio,

        Your suggestioned worked. I replaced the MessageBox by Thread.Sleep(40 00)
        and I'm now a happy bunny. Thanks so much for your help.

        Kind regards,

        Omer

        "Ignacio Machin ( .NET/ C# MVP )" wrote:
        [color=blue]
        > Hi,
        >
        > The existence of that line does not imply nothing , unless that you are
        > trying to access a file (the method name suggest it's a response to a
        > FileSystemWatch er event) and the file is still being used by another
        > program. in this escenario the MessageBox has a delay effect while it the
        > file may get closed by the other application and it works fine.
        >
        > try to replace the MessageBox for a Thread.Sleep and see what happen
        >
        > Cheers,
        >
        > --
        > Ignacio Machin,
        > ignacio.machin AT dot.state.fl.us
        > Florida Department Of Transportation
        >
        >
        >
        > "Primo" <Primo@discussi ons.microsoft.c om> wrote in message
        > news:B2E99CC6-BFD1-4EBE-AC0A-29E07562ECF3@mi crosoft.com...[color=green]
        > > Hi,
        > >
        > > I'm relatively new to C# so apopogies in advance if I am missing something
        > > obvious. I am trying to run a command, which I would normally run from the
        > > command line, from within my C# application. Here is my code:
        > >
        > > private void FileMonitor_Cha nged(object sender,
        > > System.IO.FileS ystemEventArgs e)
        > > {
        > > string renamed = e.FullPath;
        > >
        > > System.Diagnost ics.Process process1;
        > > process1= new System.Diagnost ics.Process();
        > > process1.Enable RaisingEvents = false;
        > > string strCmdLine;
        > >
        > > strCmdLine = "/C Dumper.exe \"" + renamed + "\"";
        > >
        > > // MessageBox.Show ("");
        > >
        > > process1.StartI nfo.FileName = "CMD.exe";
        > > process1.StartI nfo.Arguments = strCmdLine;
        > > process1.StartI nfo.UseShellExe cute = false;
        > > process1.StartI nfo.RedirectSta ndardOutput = true;
        > > process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
        > > process1.StartI nfo.CreateNoWin dow = true;
        > > process1.Start( );
        > >
        > > StreamReader myStreamReader = process1.Standa rdOutput;
        > > string rdfID = myStreamReader. ReadLine();
        > > process1.Close( );
        > > }
        > >
        > > The command fails to execute. However, if I were to uncomment
        > > "MessageBox.Sho w("");" then the command executes correctly. I do not want
        > > to
        > > pause my application with a message box, however.
        > >
        > > Any ideas as to where I am going wrong? Many thanks in advance.[/color]
        >
        >
        >[/color]

        Comment

        • Ignacio Machin \( .NET/ C#  MVP \)

          #5
          Re: cmd.exe doesn't work unless....

          Hi,

          Good to know

          Please remember that its just a "patch" and you need to test it in your
          real deployment escenario, it does depend of the speed of the system and how
          busy it's so it may not work always.

          cheers,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation



          "Primo" <Primo@discussi ons.microsoft.c om> wrote in message
          news:044102D3-46ED-4C96-A866-BD8B85E08BFE@mi crosoft.com...[color=blue]
          > Hi Ignacio,
          >
          > Your suggestioned worked. I replaced the MessageBox by Thread.Sleep(40 00)
          > and I'm now a happy bunny. Thanks so much for your help.
          >
          > Kind regards,
          >
          > Omer
          >
          > "Ignacio Machin ( .NET/ C# MVP )" wrote:
          >[color=green]
          >> Hi,
          >>
          >> The existence of that line does not imply nothing , unless that you are
          >> trying to access a file (the method name suggest it's a response to a
          >> FileSystemWatch er event) and the file is still being used by another
          >> program. in this escenario the MessageBox has a delay effect while it the
          >> file may get closed by the other application and it works fine.
          >>
          >> try to replace the MessageBox for a Thread.Sleep and see what happen
          >>
          >> Cheers,
          >>
          >> --
          >> Ignacio Machin,
          >> ignacio.machin AT dot.state.fl.us
          >> Florida Department Of Transportation
          >>
          >>
          >>
          >> "Primo" <Primo@discussi ons.microsoft.c om> wrote in message
          >> news:B2E99CC6-BFD1-4EBE-AC0A-29E07562ECF3@mi crosoft.com...[color=darkred]
          >> > Hi,
          >> >
          >> > I'm relatively new to C# so apopogies in advance if I am missing
          >> > something
          >> > obvious. I am trying to run a command, which I would normally run from
          >> > the
          >> > command line, from within my C# application. Here is my code:
          >> >
          >> > private void FileMonitor_Cha nged(object sender,
          >> > System.IO.FileS ystemEventArgs e)
          >> > {
          >> > string renamed = e.FullPath;
          >> >
          >> > System.Diagnost ics.Process process1;
          >> > process1= new System.Diagnost ics.Process();
          >> > process1.Enable RaisingEvents = false;
          >> > string strCmdLine;
          >> >
          >> > strCmdLine = "/C Dumper.exe \"" + renamed + "\"";
          >> >
          >> > // MessageBox.Show ("");
          >> >
          >> > process1.StartI nfo.FileName = "CMD.exe";
          >> > process1.StartI nfo.Arguments = strCmdLine;
          >> > process1.StartI nfo.UseShellExe cute = false;
          >> > process1.StartI nfo.RedirectSta ndardOutput = true;
          >> > process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
          >> > process1.StartI nfo.CreateNoWin dow = true;
          >> > process1.Start( );
          >> >
          >> > StreamReader myStreamReader = process1.Standa rdOutput;
          >> > string rdfID = myStreamReader. ReadLine();
          >> > process1.Close( );
          >> > }
          >> >
          >> > The command fails to execute. However, if I were to uncomment
          >> > "MessageBox.Sho w("");" then the command executes correctly. I do not
          >> > want
          >> > to
          >> > pause my application with a message box, however.
          >> >
          >> > Any ideas as to where I am going wrong? Many thanks in advance.[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          Working...