NotifyIcon은 응용 프로그램을 닫은 후에도 트레이에 남아 있지만 마우스를 가리키면 사라집니다.
그래서 같은 의심을 묻는 많은 질문이 있습니다. 이에 대한 해결책은
notifyIcon.icon = null
Dispose
FormClosing 이벤트에서 호출 합니다.
내 응용 프로그램에는 그러한 양식이 없지만 이벤트에 업데이트되는 알림 아이콘이 있습니다. 창조시 나는 내 양식을 숨기고 ShowInTaskbar
재산을 만든다 false
. 따라서 "FormClosing"또는 "FormClosed"이벤트를 가질 수 없습니다.
이 응용 프로그램이 종료 할 이벤트를 받으면 종료를 호출 Process.GetCurrentProcess().Kill();
합니다.
notifyIcon.icon = null
죽이기 전에 Dispose를 추가 했지만 아이콘 위로 마우스를 가져갈 때까지 작업 표시 줄에 남아 있습니다.
편집 :이 동작이 호출로 인한 것이라고 가정하면 GetCurrentProcess().Kill()
모든 리소스를 지우고 시스템 트레이에서 아이콘을 제거하는 응용 프로그램을 종료하는 우아한 방법이 있습니까?
당신은 설정할 수 있습니다
notifyIcon1.Visible = false;
또는
notifyIcon.Icon = null;
양식 마감 이벤트에서.
나를 위해 일한 유일한 해결책은 Closed 이벤트를 사용하고 아이콘을 숨기고 삭제하는 것입니다.
icon.BalloonTipClosed += (sender, e) => {
var thisIcon = (NotifyIcon)sender;
thisIcon.Visible = false;
thisIcon.Dispose();
};
이벤트 notifyIcon.Visible = False
에서 사용FormClosing
구성 요소는 다음과 같이 올바른 순서로 처리되어야합니다.
NotifyIcon.Icon.Dispose();
NotifyIcon.Dispose();
MainWindow
클로징 이벤트에 추가합니다 .
이것이 도움이되기를 바랍니다.
종료 또는 닫기 버튼을 누를 때이 코드를 사용하십시오.
private void ExitButton_Click(object sender, EventArgs e)
{
notifyIcon.Dispose;
Application.Exit(); // or this.Close();
}
양식이 닫힐 때 수행하려는 경우이 코드를 사용하십시오.
private void Form1_FormClosing(object sender, EventArgs e)
{
notifyIcon.Dispose;
Application.Exit(); // or this.Close();
}
중요한 코드는 다음과 같습니다.
notifyIcon.Dispose;
불행히도 이것은 정상적인 동작입니다. Windows가 작동하는 방식 때문입니다. 당신은 그것에 대해 무엇이든 할 수 있습니다.
몇 가지 제안 사항 은 Winforms 앱에서 NotifyIcon이 사라지지 않는 문제를 참조하십시오 .
알림 아이콘이 응용 프로그램의 시스템 트레이에 유지됨을 참조하십시오.
Microsoft는 Microsoft Connect에서이 문제를 "수정할 수 없음"으로 표시했습니다.
WPF에 자체 NotifyIcon이 있다고 생각하지 않습니다. 타사 Harcodet.Wpf.TaskbarNotification을 사용하는 경우 다음을 시도하십시오.
창을 닫을 때 (백그라운드에서 실행) 내 앱이 닫히는 것을 방지하기 위해 창을 닫는 로직 (오른쪽 상단의 x 버튼 누름)과 실제로 종료하는 로직 (컨텍스트 메뉴를 통해)을 분리했습니다. 이 작업을 수행하려면 컨텍스트 메뉴 _isExplicitClose
를 true로 설정하십시오 . 그렇지 않으면 창을 숨기고 계속 실행합니다.
이것이하는 일은 명시 적으로 닫을 때 닫기 전에 트레이 아이콘과 양식을 숨기는 것입니다. 이렇게하면 응용 프로그램이 종료 된 후에도 아이콘이 계속 표시되지 않습니다.
private bool _isExplicitClose;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (!_isExplicitClose)
{
e.Cancel = true;
Hide();
}
}
protected void QuitService(object sender, RoutedEventArgs e)
{
_isExplicitClose = true;
TaskbarIcon.Visibility = Visibility.Hidden;
Close();
}
.dispose () 메서드를 사용하여 간단히 문제를 해결할 수 있다고 말할 수 있지만 응용 프로그램을 종료하는 대신 프로세스를 종료하면 호출되지 않습니다.
간단한 Windows Form 응용 프로그램을 빌드 한 경우 Application.Exit 를 참조하고 , 그렇지 않으면 보다 일반적인 Environment.Exit 를 참조하십시오 .
시도 Application.DoEvents();
설정 한 후 notifyIcon.Icon
에 null
와 폐기 :
notifyIcon.Icon = null;
notifyIcon.Dispose();
Application.DoEvents();
그리고 고려 Environment.Exit(0);
대신 Process.GetCurrentProcess().Kill()
.
I tried all of these and none of them worked for me. After thinking about it for a while I realized that the application creating the "balloon" was exiting before it had a chance to actually dispose of the balloon. I added a while loop just before Application.Exit()
containing an Application.DoEvents()
command. This allowed my NotifyIcon1_BalloonTipClosed
to actually finish disposing of the icon before exiting.
while (notifyIcon1.Visible)
{
Application.DoEvents();
}
Application.Exit();
And the tip closed method: (You need to include the thisIcon.visible = false
in order for this to work)
private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
var thisIcon = (NotifyIcon)sender;
thisIcon.Icon = null;
thisIcon.Visible = false;
thisIcon.Dispose();
}
I had the exact same problem as you.
The proper way are send WM_CLOSE message to a process.
I use the c# code I found in this article.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way
The right answer has already been given. But you must also provide a delay, for example with a timer. Only then the application can still remove the icon in the background.
private System.Windows.Forms.Timer mCloseAppTimer;
private void ExitButton_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false; notifyIcon.Dispose;
mCloseAppTimer = new System.Windows.Forms.Timer();
mCloseAppTimer.Interval = 100;
mCloseAppTimer.Tick += new EventHandler(OnCloseAppTimerTick);
}
private void OnCloseAppTimerTick(object sender, EventArgs e)
{
Environment.Exit(0); // other exit codes are also possible
}
edit codes of ...Designer.cs as below coding.
protected override void Dispose(bool disposing)
{
if (disposing )
{
this.notifyicon.Dispose();
}
base.Dispose(disposing);
}
'Nice programing' 카테고리의 다른 글
빈 행렬 곱셈을 통해 배열을 초기화하는 더 빠른 방법? (0) | 2020.12.01 |
---|---|
Vim에 NERD Commenter를 사용하는 방법 — 사용 방법 (0) | 2020.12.01 |
Android Studio에서 기존 프로젝트를 라이브러리 프로젝트로 변환 (0) | 2020.12.01 |
ffmpeg에서 특정 기간 동안 단일 이미지로 비디오 만들기 (0) | 2020.12.01 |
.tfstate 파일을 Git에 커밋해야합니까? (0) | 2020.12.01 |