前提・実現したいこと
プログラムが応答なしとなったときにプロセスを終了させるコードを書きたいです
発生している問題・エラーメッセージ
sendmessageTimeout関数を使ってタイムアウト時の戻り値「0」を使ってif文で判定しようとしているのですが、プロセスが実行中にも関わらずタイムアウト時の0が返ってしまっているようです。
該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.Runtime.InteropServices; 11 12namespace WindowsFormsApp1 13{ 14 public class sendMessage 15 { 16 17 } 18 19 public partial class Form1 : Form 20 { 21 [DllImport("user32.dll")] 22 public static extern int SendMessageTimeout(IntPtr hWnd, int msg, int wParam, int lParam, int fuFlags, int uTimeout, out UIntPtr lpdwResult); 23 24 [DllImport("user32.dll")] 25 static extern IntPtr FindWindow(string className, string windowName); 26 27 public Form1() 28 { 29 InitializeComponent(); 30 } 31 32 private void Form1_Load(object sender, EventArgs e) 33 { 34 const int SMTO_NORMAL = 0x0000; 35 IntPtr hWnd = FindWindow(null, "notepad"); 36 int time = SendMessageTimeout(hWnd, 0x0000, 0, 0, SMTO_NORMAL, 5000, out UIntPtr lpdwResult); 37 38 //notepadのプロセスを取得 39 System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("notepad"); 40 41 if (time == 0) { 42 foreach(System.Diagnostics.Process p in ps) 43 { 44 //プロセスを強制的に終了させる 45 p.Kill(); 46 } 47 } 48 } 49 } 50}
試したこと
ウィンドウの種類を色々試しましたが、戻り値が0になってしまうのは変わらないようです。
補足情報(FW/ツールのバージョンなど)
開発環境:visual studio 2017 community

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。