Click to See Complete Forum and Search --> : Is this good programming?
khanafer
July 13th, 2005, 06:25 PM
Hello,
When I click a button in my form, the command prompt will run and then through it another application will run as well. The thing is, When I click the button again an error pops up!! you know that windows error that asks you if you wanna let MS know about it?
After some experiments, I dicoveered that the problem is happeneing because the process is not brought to end even though I was using CloseMainWindow(). So I decided to use kill(). Now it works fine no matter how many times I click on the button. Now, I was reading that thread and I got the feeling that using kill() is no good?
Does that mean that the following code is bad:
private void Button_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process prc = new System.Diagnostics.Process();
System.Diagnostics.ProcessWindowStyle.Hidden;
prc.StartInfo.FileName = "C:\\WINDOWS\\system32\\cmd.exe";
prc.StartInfo.Arguments = "/K C:\\guido2gif.exe";
prc.Start();
prc.Kill();
}
Sorry for the long email!
Ali
darwen
July 13th, 2005, 06:30 PM
Running a seperate executable from your application is bad practice unless there's a very good reason to do so (e.g. bringing up an explorer window etc).
What is it you're trying to do ?
What does the application that you're trying to run do ? It must be something to do with images... I think.
Can the functionality of your exe that you're trying to run be brought into your executable - by means of having a shared dll for instance (i.e. a "class library" or in .NET speak an assembly).
Or are you trying to run an executable to get away from trying to write its functionality yourself in your own application ?
This sounds like a bodged solution to a requirement to me. Bodging solutions will always, always hit problems. You're better off learning how to do everything you need to do in code - then you won't hit these sorts of problems.
If I'm wrong I apologise.
Darwen.
khanafer
July 13th, 2005, 06:36 PM
Hello darwen,
What is it you're trying to do ?
What does the application that you're trying to run do ? It must be something to do with images... I think.
The application is to convert .gmn files (musical sheeets) generated by using a C++ library to .gif files. I am not sure how to use assemblies actually :S. I just decided to run the application not to waste too much time trying to learn something new because I am intersted to see some results through running this application.
This application runs only through the command prompt, that is why you saw my code runs the command prompt first.
It will be appriciated if you guide me to an alternative of running an external application.
Thanks!
aLi
darwen
July 13th, 2005, 06:39 PM
Do you have the C++ source code available for the executable you were talking about ?
Darwen.
khanafer
July 13th, 2005, 06:40 PM
Yup, It is an open source library.
aLi
darwen
July 13th, 2005, 06:55 PM
Well, write a dll to do what you want it to do, and interface this with C#.
Sorry, I could write a book on how to do this. You'll just have to work this one out for yourself.
Look at the File/New/Projects and create either (in VC6) a static library or in later versions (i.e. the .NET versions of VC++) a "Win32" project and then make sure it's a Win32 static DLL.
You're going to have to learn how to do a static dll yourself.
The other alternative is to create a COM dll - equally not as easy.
You have all the wizards available to you in a COM dll - to add methods etc.
Sorry, I can't really help further. You need to write a dll in C++ including the libraries etc which you already have.
You then interface with it using P/Invoke (this you can look up).
Good luck !
Darwen.
khanafer
July 13th, 2005, 07:02 PM
Aight Darwen,
Thanks for your input. I knew that I would have to do that. As I said earlier I wanted to save time for now since I am intersted in what I can get out of the application, no matter how I run it. Thanks anyways!
Now back to my question guys, is there a better way to shut down the comman prompt application without using kill(), again CloseMainWindow() didn't do the job!!
Thanks!
aLi
sudheeratwork
July 13th, 2005, 07:53 PM
I think this is what might be happening:
prc.StartInfo.FileName = "C:\\WINDOWS\\system32\\cmd.exe";
The command prompt is associated with your process type variable prc
and
prc.Start();
prc.Kill(); These two lines do the following
1. launch the command prompt.
2. you are trying to run this command ("/K C:\\guido2gif.exe";) and launch the music app.
3. close the command prompt.
Now were in your code are you trying to trying to close the music app.
Associate the music app to the process variable and then try closing/killing it.
This should work...
I have a question here for you: Are you sure that the music app is launching. because there is very little time for the application to launch, before which the cmd.exe is being killed ????
feelite
July 13th, 2005, 08:27 PM
This is verbatim from .NET SDK Doc that explains the difference between Process.Kill() and Process.CloseMainWindow()
Kill forces a termination of the process, while CloseMainWindow only requests a termination. When a process with a graphical interface is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system. Calling CloseMainWindow revokes all the running message loops for all threads and closes all windows. The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit if it is running a macro. To force the application to quit, use the Kill method. The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.
exterminator
July 14th, 2005, 12:42 AM
Go through this thread Is it good way to close application ? (http://www.codeguru.com/forum/showthread.php?t=348006)Hope this helps. :thumb:
khanafer
July 14th, 2005, 11:40 AM
Hello,
Ok! I have refined my code:
private void Button_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
prc.StartInfo.FileName = "C:\\WINDOWS\\system32\\cmd.exe";
prc.StartInfo.FileName = "C:\\guido2gif.exe";
prc.StartInfo.Arguments = "/K -optpagefill 0 -defph 6";
prc.Start();
if(!prc.HasExited)
{
prc.WaitForExit();
}
pictureBox.Image = Image.FromFile("C:\\temp.gif");
}
Like this, I will not have to use kill() or CloseMainWindow() since the app I am using exits when it finishes writing a file, temp.gif.
The problem turned out to be that when I press the button again, the application will generate the temp.gif again but it fails to overwrite the existing file.
Is there a way that I can delete temp.gif before I press the button again so that the application doesn't fail?
Thanks!
aLi
khanafer
July 14th, 2005, 12:35 PM
Now, I am trying to delete a file but I keep getting an exception telling me that another process is still using that file! Although the command prompt window has closed!
Here is how I am deleting:
private void Button_Click(object sender, System.EventArgs e)
{
pictureBox1.Image = null; //to make sure nobody is using temp.gif
pictureBox1.Invalidate();
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
prc.StartInfo.FileName = "C:\\guido2gif.exe";
if(System.IO.File.Exists("C:\\temp.gif"))
System.IO.File.Delete("C:\\temp.gif");
prc.StartInfo.Arguments = "/K -optpagefill 0 -defph 6 -out temp.gif";
prc.Start();
if(!prc.HasExited)
{
prc.WaitForExit();
}
pictureBox.Image = Image.FromFile("C:\\temp.gif");
}
sudheeratwork
July 14th, 2005, 03:17 PM
I dont think U need this line of code at all:
prc.StartInfo.FileName = "C:\\WINDOWS\\system32\\cmd.exe";
after you have executed the code and process is dead(according to you), check the task manager to see if the process is really dead(according to the OS).
If the process is dead(according to the OS) try deleting the gif file manually to confirm that it is not being currently used by any other process.
khanafer
July 14th, 2005, 05:49 PM
Hello sudheeratwork
after you have executed the code and process is dead(according to you), check the task manager to see if the process is really dead(according to the OS).
If the process is dead(according to the OS) try deleting the gif file manually to confirm that it is not being currently used by any other process.
I have tried to delete manually but I couldn't delete it! It said: "Another process is using temp.gif"
Anyways, I downloaded a tool that tells you which process is preventing your file from being deleted. It can be found at : http://www.dr-hoiby.com/WhoLockMe/
And it showed that MyProject.exe is the one who is preventing the delete!
This is because I do this:
pictureBox.Image = Image.FromFile("C:\\temp.gif");
But I also do this before trying to delete:
pictureBox1.Image = null;
pictureBox1.Invalidate();
But still! I can't delete the file!
I dont think U need this line of code at all:
prc.StartInfo.FileName = "C:\\WINDOWS\\system32\\cmd.exe";
I figured that, please note that I already edited my last reply. Thanks anyways.
aLi
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.