Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Executing a remote program from a thread within a service (http://www.programmingforums.org/showthread.php?t=12984)

Druid Apr 12th, 2007 5:34 PM

Executing a remote program from a thread within a service
 
I have a C# client/server program that functions as expected. In attempts to get rid of the console window being displayed, I tried nesting the server code into a service with threads and such.

The problem occurs when the client tries to send across an "execute transaction", which executes a client specified program on the server. The remote process terminates but somehow doesn't let my program know, so the process hangs until I kill it. I'm exectuing the remote program by using the Process method:

:

Process myProcess;
myProcess = new Process();
myProcess = Process.Start("c:\\cycle.exe");
myProcess.WaitForExit(35000);
int errCk = myProcess.ExitCode;
myProcess.Close();


No matter how many milliseconds I use, the thread never receives the notice of termination of the remote process so the client hangs.

Is there any way I can kill the process after a specified interval and notify the thread that it died?

I am also open to eliminating the console window another way, outside of services and threads... if anyone has any suggestions.

Infinite Recursion Apr 12th, 2007 6:04 PM

Quote:

Originally Posted by Druid (Post 126649)
I am also open to eliminating the console window another way, outside of services and threads... if anyone has any suggestions.

That is a lot of effort if your only intention was to hide the console window from the user's view.

Try this:

...
using System.Runtime.InteropServices;
...

--------------------------------

And at the top of your class, put this:

:

[DllImport("user32.dll")]
publicstaticextern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll")]
staticexternbool ShowWindow(IntPtr hWnd, int nCmdShow);


--------------------------------

And within the Main function put this:

:

IntPtr hWnd = FindWindow(null, System.Windows.Forms.Application.ExecutablePath ); //put your console window caption here

if(hWnd != IntPtr.Zero)
{
  //Hide the window
  ShowWindow(hWnd, 0); // 0 = SW_HIDE
}


--------------------------------

If you wanted it to show again, you could do this:

:

if(hWnd != IntPtr.Zero)
{
  //Show window again
  ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}


Druid Apr 12th, 2007 10:25 PM

Thank you so much IR!!! This is exactly what I needed.


All times are GMT -5. The time now is 2:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC