3

プログラムのメモリ外をスキャンしています。

コードは次のようになります。

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;
    }
4

1 に答える 1

0

コードをタイマーに入れて、他のプログラムが少なくともそれ自体を更新するための変更を取得するまで、処理を(次の反復まで現在のプロセスを停止するDoEvents()を使用して)一時停止することはできません。

それはシステムを動かし続けるでしょう。

低水準言語では、メモリが変更されたかどうかのチェックもループで行われることがよくあります。プロセッサは、メモリの変更をアクティブにアナウンスしません。キーボードのように、メモリは入力ではありません。

このコードをスレッドで開始し、thread.Priority=ThreadPriority.BelowNormal;を使用して優先度を低くすることもできます。システムをさらに緩和します。

そうすることで、行われた変更を見逃す可能性があります。したがって、それが重要である場合は、少なくとも他のプログラムと同じ優先順位を持つことが重要です。

于 2012-11-05T22:14:03.243 に答える