실행환경검사 invoke 오류 수정

This commit is contained in:
ChiKyun Kim
2025-07-29 08:38:21 +09:00
parent e309864262
commit c0eb33f60a
22 changed files with 773 additions and 209 deletions

View File

@@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO.Compression;
using System.IO;
using System.DirectoryServices;
using System.ServiceModel.Configuration;
using System.Threading;
using System.Diagnostics;
namespace Project
{
@@ -17,94 +17,87 @@ namespace Project
[STAThread]
static void Main()
{
// COM 초기화 (WebView2 오류 방지)
System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
// AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
var fi = new System.IO.FileInfo("WebView2Runtime.zip");
if (fi.Exists)
// 중복실행 방지 체크
if (!CheckSingleInstance())
{
var existlen = 0;
var webruntimepath = new System.IO.DirectoryInfo("WebView2Runtime");
if (webruntimepath.Exists)
return; // 프로그램 종료
}
// WebView2Runtime 압축해제와 SqlServerTypes 로드는 fWarning에서 처리
// fWarning 폼을 먼저 표시하여 실행환경 체크
using(var f = new Dialog.fSystemCheck())
{
Application.Run(f);
if (f.environmentCheckCompleted)
Application.Run(new fMain());
}
}
/// <summary>
/// 중복실행 방지 체크
/// </summary>
/// <returns>단일 인스턴스인 경우 true, 중복실행인 경우 false</returns>
static bool CheckSingleInstance()
{
string processName = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length > 1)
{
// 중복실행 감지
string message = "⚠️ GroupWare 프로그램이 이미 실행 중입니다!\n\n" +
"동시에 여러 개의 GroupWare를 실행할 수 없습니다.\n\n" +
"해결방법을 선택하세요:";
var result = MessageBox.Show(message + "\n\n예(Y): 기존 프로그램을 종료하고 새로 시작\n아니오(N): 현재 실행을 취소",
"중복실행 감지",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
var filen = new System.IO.FileInfo("WebView2Runtime\\version.txt");
if(filen.Exists)
{
var verstr = System.IO.File.ReadAllText(filen.FullName);
int.TryParse(verstr, out existlen);
}
}
if(existlen != fi.Length)
{
//fi 파일의 압축을 해제한다.
var fprogress = new Dialog.fUnZip();
fprogress.TopMost = true;
fprogress.Show();
Application.DoEvents();
// 기존 프로세스들을 종료
try
{
if (webruntimepath.Exists)
int currentProcessId = Process.GetCurrentProcess().Id;
foreach (Process process in processes)
{
fprogress.label1.Text = "기존폴더삭제";
Application.DoEvents();
webruntimepath.Delete(true);
}
var diruntime = new System.IO.DirectoryInfo("runtimes");
if (diruntime.Exists) diruntime.Delete(true);
webruntimepath.Create();
diruntime.Create();
using (var archive = ZipFile.OpenRead(fi.FullName))
{
int totalFiles = archive.Entries.Count;
int currentFile = 0;
long totalSize = archive.Entries.Sum(e => e.Length);
long extractedSize = 0;
foreach (var entry in archive.Entries)
if (process.Id != currentProcessId)
{
currentFile++;
extractedSize += entry.Length;
int progress = (int)((extractedSize * 100) / totalSize);
fprogress.label1.Text = $"압축 해제 중... ({currentFile}/{totalFiles})";
fprogress.label2.Text = $"{entry.FullName} ({progress}%)";
fprogress.progressBar1.Value = progress;
Application.DoEvents();
string destinationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, entry.FullName);
string destinationDir = Path.GetDirectoryName(destinationPath);
if (!Directory.Exists(destinationDir))
Directory.CreateDirectory(destinationDir);
if (entry.Length > 0)
entry.ExtractToFile(destinationPath, true);
process.Kill();
process.WaitForExit(3000); // 3초 대기
}
}
System.IO.File.WriteAllText("WebView2Runtime\\version.txt", fi.Length.ToString());
// 잠시 대기 후 계속 진행
Thread.Sleep(1000);
return true;
}
catch (Exception ex)
{
fprogress.Hide();
MessageBox.Show($"WebView2 Runtime 압축 해제 중 오류가 발생했습니다: {ex.Message}", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
fprogress.Dispose();
MessageBox.Show($"기존 프로그램 종료 중 오류가 발생했습니다:\n{ex.Message}\n\n" +
"작업관리자에서 수동으로 종료해주세요.",
"오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
else
{
// 현재 실행을 취소
return false;
}
}
SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
Application.Run(new fMain());
return true; // 단일 인스턴스
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)