如何从任务管理器中删除excel .exe [英] How to delete excel .exe from task manager
问题描述
hiiii
i使用此代码
hiiii
i used this code
private void button3_Click(object sender, EventArgs e)
{
string path = @"C:\KBE\solid piston1.xls";
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false);
//Get all the sheets in the workbook
mWorkSheets = mWorkBook.Worksheets;
//Get the allready exists sheet
mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("INPUT");
mWSheet1.Cells[3, 3] = cbf.Text;
mWSheet1.Cells[8, 3] = comboBox2.Text;
// mWSheet1.Cells[12, 3] = comboBox3.Text;
mWSheet1.Cells[14, 3] = comboBox4.Text;
mWSheet1.Cells[27, 3] = comboBox5.Text;
mWSheet1.Cells[13, 3] = textBox46.Text;
mWSheet1.Cells[4, 3] = textBox1.Text;
mWSheet1.Cells[5, 3] = textBox2.Text;
mWSheet1.Cells[6, 3] = textBox3.Text;
mWSheet1.Cells[12, 3] = cbr.Text;
mWSheet1.Cells[10, 3] = textBox6.Text;
mWSheet1.Cells[11, 3] = textBox7.Text;
mWSheet1.Cells[15, 3] = textBox8.Text;
mWSheet1.Cells[26, 3] = textBox19.Text;
mWSheet1.Cells[28, 3] = textBox20.Text;
mWSheet1.Cells[29, 3] = textBox21.Text;
mWSheet1.Cells[36, 3] = textBox22.Text;
mWSheet1.Cells[31, 3] = textBox23.Text;
mWSheet1.Cells[32, 3] = textBox24.Text;
mWorkBook.Save();
mWorkBook.Close();
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false);
//Get all the sheets in the workbook
mWorkSheets = mWorkBook.Worksheets;
mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("INPUT");
string j2 = (mWSheet1.Cells[12, 4].Text);
label57.Text = j2.ToString();
mWorkBook.Close();
oXL.Quit();
GC.Collect();
Marshal.FinalReleaseComObject(mWSheet1);
Marshal.FinalReleaseComObject(mWorkBook);
Marshal.FinalReleaseComObject(oXL);
每当我运行此代码并单击按钮一次或多次。在任务管理器中打开两个或更多excel.exe
so请给我建议在给定的代码中需要什么类型的更改
谢谢
whenever i run this code and click button one or more times .so two or more excel.exe is open in task manager
so please give me suggestion what type of change is required in the given code
thanks
推荐答案
Dhaval,
我看了两次:
Dhaval,
I see this two times :
oXL = new Microsoft.Office.Interop.Excel.Application();
但是,我只看到一组
However, I only see one group of
oXL.Quit();
GC.Collect();
Marshal.FinalReleaseComObject(mWSheet1);
Marshal.FinalReleaseComObject(mWorkBook);
Marshal.FinalReleaseComObject(oXL);
请记住,在重新分配oXL之前,你必须释放它,否则第一个引用将丢失并且对象仍然存在。
Remember that before reasigning oXL, you must release it, otherwise the first reference is lost and the object remains.
是的,您的代码缺少某些内容。如果您多次运行代码,这将是您在任务管理器中获得的代码:
http://www.prairiefyre.com/KB/Uploads/Images/ excelprocesses.jpg
您最终在任务管理器中运行了几个 EXCEL.EXE 。
所以,这就是你可以做的:
在Windows中运行的每个进程都将被分配一个进程ID 。捕获正在运行的excel的进程ID,然后在完成工作后终止它。
使这样的变量不会终止进程:
Yes, your code is missing something. If you run your code multiple times, this is what you will get in task manager:
http://www.prairiefyre.com/KB/Uploads/Images/excelprocesses.jpg
You end up running several EXCEL.EXE in Task Manager.
So, this is what you can do:
Each process running in Windows will be assigned a Process ID. Capture the Process ID of the excel that you are running, then kill it when you have done your job.
Making the variable like this won't kill the process:
oXL = null;
是示例代码:
This is the sample codes:
using System.Diagnostics;
在班级的顶层创建一个词典:
Create a Dictionary at top level of your class:
Dictionary<int,int> dicExcel = Dictionary(int,int>();
int MyExcelProcessId = 0;
开始创建的实例之前Microsoft.Office.Interop.Excel
object,检查当前正在运行的现有excel:
Before you start creating an instance of Microsoft.Office.Interop.Excel
object, you check for existing excel that are currently running:
private void WriteExcel()
{
// do something...
CheckForExistingExcellProcesses();
oXL = new Microsoft.Office.Interop.Excel.Application();
// do something...
}
void CheckForExistingExcellProcesses()
{
Process[] AllProcesses = Process.GetProcessesByName("excel");
foreach (Process ExcelProcess in AllProcesses)
{
dicExcel.Add(ExcelProcess.Id, 1);
}
}
创建的实例后,Microsoft.Office.Interop.Excel
对象,捕获进程ID:
After creating your instance of Microsoft.Office.Interop.Excel
object, capture the process ID:
private void WriteExcel()
{
// do something...
oXL = new Microsoft.Office.Interop.Excel.Application();
GetTheExcelProcessIdThatUsedByThisInstance();
// do something...
}
void GetTheExcelProcessIdThatUsedByThisInstance()
{
Process[] AllProcesses = Process.GetProcessesByName("excel");
// Search For the Right Excel
foreach (Process ExcelProcess in AllProcesses)
{
if (dicExcel == null)
return;
if (dicExcel.ContainsKey(ExcelProcess.Id) == false)
{
// This is the Process ID that used by your instance
MyExcelProcessId = ExcelProcess.Id;
break;
}
}
AllProcesses = null;
}
用Excel完成所有操作后,最后终止进程:
After you have done everything with your Excel, kill the process at the end:
void KillExcelProcessThatUsedByThisInstance()
{
Process[] AllProcesses = Process.GetProcessesByName("excel");
foreach (Process ExcelProcess in AllProcesses)
{
if (ExcelProcess.Id == MyExcelProcessId)
{
ExcelProcess.Kill();
break;
}
}
AllProcesses = null;
}
最终代码如下:
The final code will be something like this:
private void button3_Click(object sender, EventArgs e)
{
CheckForExistingExcellProcesses();
// do something...
oXL = new Microsoft.Office.Interop.Excel.Application();
GetTheExcelProcessIdThatUsedByThisInstance();
// execute your excel task ...blah..blah..blah... until end ..
KillExcelProcessThatUsedByThisInstance();
}
我曾经遇到过和你一样的问题。世界各地的一些程序员已经向我分享了这些知识,现在轮到我分享了这个。
我写了一个基于此的免费软件,你可能想拥有一看:
简单Microsoft Excel文档转换器的源代码 [ ^ ]
mWorkBook.SaveAs(ref outputFileName,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing);
mWorkBook.Close(ref _oMissing, ref _oMissing, ref _oMissing);
oXL.Quit(ref _oMissing, ref _oMissing, ref _oMissing);
这篇关于如何从任务管理器中删除excel .exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!