Wie kann ich den untergeordneten Excel-Prozess beenden [Duplikat]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie kann ich den untergeordneten Excel-Prozess beenden [Duplikat]

Post by Anonymous »

Ich verwende Microsoft.Office.Interop.Excel.Application

Meine MyMainFunction()-Funktion wird ausgeführt.

Wenn unmittelbar nach der Ausführung von excelApp = new Microsoft.Office.Interop.Excel.Application(); eine unerwartete erzwungene Beendigung (Absturz) auftritt, gibt es für mich eine Möglichkeit, diesen excelApp-Prozess abzubrechen?
Ich habe einen Ansatz verwendet, bei dem ich es tun würde Ich erhalte die PID jedes Mal, wenn ich eine Excel-Instanz erstellt habe.

Ich würde diese PID behalten, um alle verbleibenden Prozesse später abzubrechen.

Das Problem bei dieser Methode besteht jedoch darin, dass ich bei einem erzwungenen Abbruch (einem Absturz) bevor ich die PID registrieren kann, keine Möglichkeit habe, den verwaisten Excel-Prozess abzubrechen, der noch ausgeführt wird.

Code: Select all

public class TestClass
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

System.Collections.Concurrent.ConcurrentDictionary pidDictionary = new System.Collections.Concurrent.ConcurrentDictionary();
public TestClass()
{

}
~TestClass()
{
KillRemainingProcesses();
}
private void RegistPID(uint pid)
{
pidDictionary.TryAdd(pid, 0);
}
private void KillExcelAndRemove(uint processId)
{
try
{
var process = Process.GetProcessById((int)processId);
if (process != null && !process.HasExited && process.ProcessName.Equals("EXCEL", StringComparison.OrdinalIgnoreCase))
{
process.Kill();
pidDictionary.TryRemove(processId, out byte notUse);
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}, Kill Error");
}
}
private void KillRemainingProcesses()
{
foreach (var processId in pidDictionary.Keys)
{
try
{
Process process = Process.GetProcessById((int)processId);
if (process != null && !process.HasExited &&  process.ProcessName.Equals("EXCEL", StringComparison.OrdinalIgnoreCase))
{
process.Kill();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public void MyMainFunction()
{
Microsoft.Office.Interop.Excel.Application excelApp = null;
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
uint processId = 0;
try
{
excelApp = new Microsoft.Office.Interop.Excel.Application();
GetWindowThreadProcessId(new IntPtr(excelApp.Hwnd), out processId);
RegistPID(processId);
// Do Excel Edit
workbook.Save();
}
catch (Exception ex)
{
}
finally
{
if (worksheet != null)
{
Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
if (workbook != null)
{
workbook.Close(false);
Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (excelApp != null)
{
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
excelApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

KillExcelAndRemove(processId);
}

}
}
Gibt es nicht einen einfacheren Weg als die Verwendung von „Links“?

Untergeordneten Prozess beenden, wenn übergeordneter Prozess beendet wird

https://learn.microsoft.com/en-us/windo ... jobobjectw

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post