Tuesday, July 22, 2008

Get List of threads in Windows MObile.

Hi Guys,

it's another nice good morning, because of when i look MS forums last night, one guy asked , how can get list of threads in .net CF. then i tried work out in my home. then it's come sounds is good.

Here code for you.

first i want to create threadentry32 structure.
class THREADENTRY32
{

public uint cntUsage;
public uint dwFlags;
public uint dwSize;
public uint th32AccessKey;
public uint th32CurrentProcessID;
public uint th32OwnerProcessID;
public uint th32ThreadID;
public int tpBasePri;
public int tpDeltaPri;
public THREADENTRY32()
{
}
public THREADENTRY32(byte[] aData)
{
this.dwSize = BitConverter.ToUInt32(aData, 0);
this.cntUsage = BitConverter.ToUInt32(aData, 4);
this.th32ThreadID =BitConverter.ToUInt32(aData, 8);
this.th32OwnerProcessID = BitConverter.ToUInt32(aData, 12);
this.tpBasePri = BitConverter.ToInt32(aData, 0x10);
this.tpDeltaPri = BitConverter.ToInt32(aData, 0x18);
this.dwFlags = BitConverter.ToUInt32(aData, 0x20);
this.th32AccessKey = BitConverter.ToUInt32(aData, 0x24);
this.th32CurrentProcessID = BitConverter.ToUInt32(aData, 40);
}

public byte[] ToByteArray()
{
byte[] aData = new byte[0x2c];
Util.SetUInt(aData, 0, 0x2c);
return aData;
}

}

then i want to get snapshot for thread collections.

if i want to get thread snapshot olny , flag should be 4.


[DllImport("toolhelp.dll", EntryPoint = "CreateToolhelp32Snapshot", SetLastError = true)]
private static extern IntPtr CreateToolhelp32SnapshotAPI(uint flags, uint processid);

[DllImport("toolhelp.dll", EntryPoint = "CloseToolhelp32Snapshot", SetLastError = true)]
private static extern int CloseToolhelp32SnapshotAPI(IntPtr handle);

[DllImport("toolhelp.dll", EntryPoint = "Thread32First", SetLastError = true)]
private static extern int Thread32FirstAPI(IntPtr handle, byte[] te);

[DllImport("toolhelp.dll", EntryPoint = "Thread32Next", SetLastError = true)]
private static extern int Thread32NextAPI(IntPtr handle, byte[] te);

public static THREADENTRY32[] GetThreads(uint processID)
{
ArrayList list = new ArrayList();
IntPtr handle = CreateToolhelp32Snapshot(4, processID);
if (((int) handle) <= 0)
{
throw new Exception("Unable to create snapshot");
}
try
{
byte[] te = new THREADENTRY32().ToByteArray();
for (int i = Thread32First(handle, te); i == 1; i = Thread32Next(handle, te))
{
THREADENTRY32 threadentry = new THREADENTRY32(te);
if ((processID == 0) || (threadentry.th32OwnerProcessID == processID))
{
THREADENTRY32entry = new THREADENTRY32(threadentry);
list.Add(entry);
}
}
}
catch (Exception exception)
{
throw new Exception("Exception: " + exception.Message);
}
CloseToolhelp32Snapshot(handle);
return (THREADENTRY32[]) list.ToArray(typeof(THREADENTRY32));
}


That's all

try and give a feedback.

2 comments:

Gayani Devapriya said...

Hi Rave,

So nice to see your Blog's new look and feel and the fresh posts...keep it up. Good work...

Rgs,
Gayani

Unknown said...

I tried this example and got this error:
"The name 'Util' does not exist in the current context"