Click here to Skip to main content
15,911,139 members
Home / Discussions / C#
   

C#

 
QuestionHow to kill Excel process properly? Pin
teagermylk28-Dec-03 10:31
teagermylk28-Dec-03 10:31 
I wrote an application that writes to disk a certain excel document from sql database, then opens it edit.
After user finished editing the document in Excel, the application asks in a dialog if user wants to update the changes to DB. If yes, updates the file in the database.
The problem is, that after Excel window is closed and file updated to db, the Excel process is still running. It can be seen in Process Manager,
and causes incorrect behaviour of new Excel windows. After opening 50 documents, there are 50 processes. That causes serious errors in Excel, up to "OLE library error", which can be cured so far by re-installing Office. Frown | :(

The question is: How to kill Excel properly?
TIA

Here is the code:

private void OpenExcel(string filename)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();

myproc.EnableRaisingEvents = true;

//a special exitbox is assigned to each document
ExitBox windowAfterExit = new ExitBox(filename,this,myproc);
myproc.Exited+=new EventHandler(windowAfterExit.Excel_Exited);

myproc.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
myproc.StartInfo.FileName = filename;
myproc.StartInfo.Verb = "open";

//open excel
myproc.Start();

// at a moment we can work with one document only
myproc.WaitForExit();

}

public class ExitBox : System.Windows.Forms.Form
		{ 

...

		public void Excel_Exited(object sender,EventArgs e)
		{	
                //Excel exited, so show this window
		this.Visible = true;


		}

		private void Exit()

		{	
			//tried this to kill Excel
			myproc.Dispose();



			//all necessary changes are submitted to Db, so delete the file
			DeleteFile(filename);

			//close the form(hope it disposes all resources too :) )		
			this.Close();
	
		}
	}

QuestionWIN32 DrawText() .NET equivalent?? Pin
ShaneStump28-Dec-03 8:11
ShaneStump28-Dec-03 8:11 
AnswerRe: WIN32 DrawText() .NET equivalent?? Pin
Heath Stewart28-Dec-03 9:11
protectorHeath Stewart28-Dec-03 9:11 
GeneralRe: WIN32 DrawText() .NET equivalent?? Pin
ShaneStump28-Dec-03 10:48
ShaneStump28-Dec-03 10:48 
GeneralRe: WIN32 DrawText() .NET equivalent?? Pin
Nick Parker28-Dec-03 11:02
protectorNick Parker28-Dec-03 11:02 
GeneralRe: WIN32 DrawText() .NET equivalent?? Pin
ShaneStump28-Dec-03 11:11
ShaneStump28-Dec-03 11:11 
GeneralRe: WIN32 DrawText() .NET equivalent?? Pin
Nick Parker28-Dec-03 11:34
protectorNick Parker28-Dec-03 11:34 
GeneralRe: WIN32 DrawText() .NET equivalent?? Pin
ShaneStump28-Dec-03 11:46
ShaneStump28-Dec-03 11:46 
GeneralTreeView Pin
Luther Baker28-Dec-03 7:48
Luther Baker28-Dec-03 7:48 
GeneralRe: TreeView Pin
Tristan Rhodes28-Dec-03 8:37
Tristan Rhodes28-Dec-03 8:37 
GeneralRe: TreeView Pin
Heath Stewart28-Dec-03 8:58
protectorHeath Stewart28-Dec-03 8:58 
GeneralYe - That one Pin
Tristan Rhodes28-Dec-03 14:22
Tristan Rhodes28-Dec-03 14:22 
GeneralThanks Pin
Luther Baker30-Dec-03 5:09
Luther Baker30-Dec-03 5:09 
GeneralRe: TreeView Pin
Luther Baker30-Dec-03 9:51
Luther Baker30-Dec-03 9:51 
GeneralRe: TreeView Pin
Heath Stewart30-Dec-03 10:59
protectorHeath Stewart30-Dec-03 10:59 
GeneralRe: TreeView Pin
Luther Baker31-Dec-03 4:47
Luther Baker31-Dec-03 4:47 
GeneralRe: TreeView Pin
Luther Baker31-Dec-03 4:56
Luther Baker31-Dec-03 4:56 
GeneralC Pre Processor executable Pin
Tristan Rhodes28-Dec-03 5:38
Tristan Rhodes28-Dec-03 5:38 
GeneralRe: C Pre Processor executable Pin
Tristan Rhodes28-Dec-03 5:44
Tristan Rhodes28-Dec-03 5:44 
GeneralRe: C Pre Processor executable Pin
Nick Parker28-Dec-03 5:56
protectorNick Parker28-Dec-03 5:56 
GeneralRe: C Pre Processor executable Pin
Tristan Rhodes28-Dec-03 6:08
Tristan Rhodes28-Dec-03 6:08 
GeneralRe: C Pre Processor executable Pin
leppie28-Dec-03 5:57
leppie28-Dec-03 5:57 
GeneralRe: C Pre Processor executable Pin
Heath Stewart28-Dec-03 6:58
protectorHeath Stewart28-Dec-03 6:58 
GeneralRe: C Pre Processor executable Pin
S. Senthil Kumar29-Dec-03 1:47
S. Senthil Kumar29-Dec-03 1:47 
Generalobjidl Type Libraries Query Pin
28-Dec-03 4:07
suss28-Dec-03 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.