기본 윈도우 핸들은 가비지 수집되지 않습니다. 그래서 인터넷에서 검색 한 후 MSDN 문서에서 SafeProcessHandle에 대해 알게되었습니다. 그것을 구현하는 시도하지만 난 현재 열려있는 창을 얻기 위해 'System.Runtime.InteropServices.SEHException을'Intptr 핸들이 가비지 수집되지 않음
https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace TestHandle
{
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
static void Main(string[] args)
{
while (true)
{
IntPtr hWnd = GetForegroundWindow();
int processID = 0;
int threadID = GetWindowThreadProcessId(hWnd, out processID);
using (Process p = Process.GetProcessById(processID))
{
StringBuilder text = new StringBuilder(256);
GetWindowText(hWnd, text, 256);
Console.WriteLine(text.ToString());
}
Console.WriteLine(hWnd);
Thread.Sleep(100);
}
char tmp = 'q';
while (Console.Read() != tmp) ;
}
}
}
수신하고, 나는 타이머를 통해 구현하는 노력도 동안을 시도하고있다 루프하지만 메모리 풋 프린트가 증가하고 PC가 실행될 때까지 작동하는 오랫동안 실행되는 콘솔 응용 프로그램을 설계한다면 상황이 점점 악화되고 있습니다. 누군가 나를 도와 줄 수 있습니까?
이 창을 소유하지 않으므로 정리할 필요가 없습니다. 여기서 코드는 정리하려고하지 않습니다. 샐비어 핸들을 사용할 수 없습니다. 서비스는 대화 형 데스크톱에 액세스 할 수 없습니다. 달성하고자하는 것과 문제가 무엇인지 명확하게 밝혀주십시오. –
실행중인 창을 얻으려면 장기간 실행되는 콘솔 응용 프로그램입니다. 예, 당신은 사실이지만 타이머에서이 코드를 실행하면 메모리 풋 프린트가 계속 증가하는 것을 볼 수 있습니다. – Jef