C# - Number Of Cores and Processors

27. October 2011 13:55

 

If your working with threads in c# is can be useful to be able to find out the number of processors and the total number of cores avilable to an application so that you can adjust the maximum number of threads to a sensible value when an application starts. After all there is no point in attempting to execute 100 high processor intensive task's in a system with only 2 cores. It will just grind your application and possibly windows to a complete halt.

 

Heres is some code to find that information out

 

public static class ThreadSystemInfo
{
	private static bool HasData = false;

	private static void Init()
	{
		if (HasData)
			return;

		ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_Processor");
		ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
		ManagementObjectCollection results = searcher.Get();

		foreach(ManagementObject result in results) {
			_NumberOfCores = int.Parse(result["NumberOfCores"].ToString());
			_NumberOfLogicalProcessors = int.Parse(result["NumberOfLogicalProcessors"].ToString());
			break;
		}

		HasData = true;
	}

	private static int _NumberOfCores = 0;
	public static int NumberOfCores
	{
		get
		{
			Init();
			return _NumberOfCores;
		}
	}

	private static int _NumberOfLogicalProcessors = 0;
	public static int NumberOfLogicalProcessors
	{
		get
		{
			Init();
			return _NumberOfLogicalProcessors;
		}
	}
}

E-mail Kick it! DZone it! del.icio.us Permalink


C# - ThreadPool and Windows Forms

23. August 2011 22:31

 

This is a more complex example of using the c# thread pool in a windows forms application. The goal here is to background a task but still keep some / part of the interface active and not freeze as a task is running.

 

To get this example to work you will need to create a project and create a button and a text box. I have used btnRun and txtCount in this example. The real purpose of this example is to demonstrate how to disable / enable the gui and to background the task as well as provide status updates to the interface from the background task and to handle an error should it occur.

 

I should probably point out that you cannot directly update the forms gui in c# from a background thread. The Forms part of the .net frame work is not thread safe. If you attempt to do so it will however create an error such as, Cross-thread operation not valid: Control 'txtCount' accessed from a thread other than the thread it was created on.

 

To get around this issue you can test the property InvokeRequired. If this property is true then you know that you should not update the interface directly. Knowing this it is possible to still create functions that can still perform the functions on either the main program thread or the background thread by calling invoke when the InvokeRequired properly is true. When Invoke is called the framework will send a message then call the function on the correct thread.

 

The following example shows this.

 

 

public partial class frmMain : Form
{
	public frmMain()
	{
		InitializeComponent();
	}

	private void btnRun_Click(object sender, EventArgs e)
	{
		SetGUI(false);
		ThreadPool.QueueUserWorkItem(new WaitCallback(this.Run), null);
	}

	void Run(object args)
	{
		try
		{
			for (int i = 0; i < 10; i++)
			{
				UpdateStatus(i.ToString());
				Thread.Sleep(1000);
			}
			UpdateStatus("Complete");
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
		
		SetGUI(true);
	}

	void UpdateStatus(object args)
	{
		if (InvokeRequired)
		{
			Invoke(new WaitCallback(UpdateStatus), args);
			return;
		}

		txtCount.Text = (string)args;
	}

	void SetGUI(object args)
	{
		if (InvokeRequired)
		{
			Invoke(new WaitCallback(SetGUI), args);
			return;
		}

		btnRun.Enabled = (bool) args;
	}
}

 

 

Note. I would normally create another class for the background task. Then drive the status updates, error and completion from events. It makes the code more maintainable and easyier to both read and test the background processes in the application. If you are intrested I will post an example of this as well.

 

E-mail Kick it! DZone it! del.icio.us Permalink