Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C# How can I make this function run in parallel using more threads or processor cores to be faster?

C#
<pre> public void ReadAOB(string Scancode)
        {
            if ((int)processhandle > 0)
            {
                IntPtr bytesRead = IntPtr.Zero;
                long MaxAddress = 0;
                if(isbit64)
                     MaxAddress = 0x7fffffffffffffff;
                else
                    MaxAddress = 0x7fffffff;
                long address = 0;
                addresses.Clear();
                do
                {
                    MEMORY_BASIC_INFORMATION m;
                    int result = VirtualQueryEx(processhandle, (IntPtr)address, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)));

                    if (m.Protect == AllocationProtectEnum.PAGE_READWRITE)
                    {
                        byte[] buffer = new byte[(int)m.RegionSize];
                        // IntPtr buffer = IntPtr.Zero;
                        ReadProcessMemory(processhandle, m.BaseAddress, buffer, (int)m.RegionSize, out bytesRead);
                        BoyerAlgo(m.BaseAddress, buffer, Scancode, ref addresses);
                    }
                    if (address == (long)m.BaseAddress + (long)m.RegionSize)
                        break;
                    address = (long)m.BaseAddress + (long)m.RegionSize;
                } while (address <= MaxAddress);

            }
        }


What I have tried:

It works but when it goes to the memory scan takes a long time to finish.
Posted
Updated 31-Mar-18 8:06am

You can try to solve in few steps
1. Put your complete do loop in new method with parameters start and end address
2. Divide complete memory range (currently 0 to MaxAddress) into several ranges
3. Call new method with Task.Run and parameters start and end memory range from step 2
4. Await all tasks to finish with Task.Await

For best performance, you should try and determine how many tasks in parallel is optimal on you computer.
 
Share this answer
 
Comments
Jon McKee 31-Mar-18 15:10pm    
Using this kind of approach, Parallel.For would be easier and handle the optimal parallelism for you :)
ShakalX 1-Apr-18 11:51am    
My problem is that I do not know how to implement this. Where can I find a freelancer to do this for me?
I'm an apprentice yet.
Uroš Šmon 2-Apr-18 8:07am    
This is programmer helping programmer site, not freelancer find site.
So sory, you will have to look elsewhere
ShakalX 2-Apr-18 9:47am    
its ok. ^^
Not the answer you are looking for, but another problem.
C++
unsigned long MaxAddress = 0;
if(isbit64)
     MaxAddress = 0xffffffffffffffff;
else
    MaxAddress = 0xffffffff;

Addresses are never negative, they are unsigned, this error make you miss half of the range.
There is no need to initialize to zero because you store real value just after.
 
Share this answer
 
Comments
ShakalX 31-Mar-18 12:26pm    
oh thanks =) So can you start on how much?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900