プログラムのメモリ外をスキャンしています。
コードは次のようになります。
while (true)
{
read(x)
if (x changed since last check)
{
x = new value;
.
.
.
}
Thread.Sleep(0);
}
CPUが常に1つの値を読み続けるのは大きな無駄だと思います。残念ながら、私はそこにもっと大きな睡眠を与えることはできません. CPU 使用率を下げ、プログラムの機能を維持する方法はありますか?
プロセスメモリを読み取るために使用している関数は次のとおりです。
// BOOL ReadProcessMemory(
// HANDLE hProcess, // handle to the process
// LPCVOID lpBaseAddress, // base of memory area
// LPVOID lpBuffer, // data buffer
// SIZE_T nSize, // number of bytes to read
// SIZE_T * lpNumberOfBytesRead // number of bytes read
// );
[DllImport("kernel32.dll")]
public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
public byte[] ReadProcessMemory(IntPtr MemoryAddress, uint bytesToRead, out int bytesRead)
{
byte[] buffer = new byte[bytesToRead];
IntPtr ptrBytesRead;
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, MemoryAddress, buffer, bytesToRead, out ptrBytesRead);
bytesRead = ptrBytesRead.ToInt32();
return buffer;
}