4
Answers

How to suspend current windows thread in C#

diya sher

diya sher

17y
5.8k
1

hello

the program give below display the information of process and threads that started in the system, i want to suspend the thread of process that is recently started is functio is available bu i dotknow how to use it or may beother function available to suspend thread if any oneknows then plz reply me how to suspend current opened thread

bye



/*http://msdn2.microsoft.com/En-US/library/ms684847.aspx

* Process ad Thread functions

*/

using System;

using System.Collections.Generic;

using System.Text;

using System.Management;

using System.Runtime.InteropServices;

using System.Threading;

namespace ThreadStartDemo

{

class Program

{

/* [DllImport("Kernel32.dll")]

static extern Boolean Sleep(UInt32 duration);*/

int state;

ManagementClass TClass;

ManagementObject TInstance;

ManagementClass PClass;

ManagementObject PInstance;

int processid = 0;

int Tprocessid = 0;

void Run()

{

WqlEventQuery q = new WqlEventQuery();

q.EventClassName = "Win32_ThreadStartTrace";

ManagementEventWatcher w = new ManagementEventWatcher(q);

w.EventArrived += new EventArrivedEventHandler(this.ThreadEventArrived);

WqlEventQuery qT = new WqlEventQuery();

qT.EventClassName = "Win32_ProcessStartTrace";

ManagementEventWatcher wT = new ManagementEventWatcher(q);

wT.EventArrived += new EventArrivedEventHandler(this.ProcessEventArrived);

w.Start();

wT.Start();

}

public void ProcessEventArrived(object sender, EventArrivedEventArgs e)

{

PClass = new ManagementClass("Win32_Process");

PInstance = PClass.CreateInstance();

int a = 0;

foreach (PropertyData pd in e.NewEvent.Properties)

{

if (a == 2)

{

//retriving id of Started process.

processid = (int)pd.Value;

Console.WriteLine("{0} = {1}",pd.Name,pd.Value);

break;

}

a++;

}

}

public void ThreadEventArrived(object sender, EventArrivedEventArgs e)

{

TClass = new ManagementClass("Win32_Thread");

TInstance = TClass.CreateInstance();

state = (int) TInstance.GetPropertyValue("ThreadState");

Console.WriteLine("\n\n");

int a = 0;

foreach(PropertyData pd in e.NewEvent.Properties)

{

if (a == 0)

{

Tprocessid = (int)pd.Value;

Console.WriteLine("{0} = {1}", pd.Name, pd.Value);

break;

}

}

//compare the started process id and started thread process id

// and state of thread then sleep the process

if(processid == Tprocessid && state == 0)

{

/* Method is:

DWORD WINAPI SuspendThread(

HANDLE hThread

);

problem: how to use in program

*

TInstance.InvokeMethod("SuspendThread");*/

// Sleep(2000);

}

}

static void Main(string[] args)

{

Program ev = new Program();

ev.Run();

Console.ReadLine();

}

}

}

Answers (4)