Thursday, 1 May 2008

Good Rhino.Mocks introduction / tutorial

Good Rhino.Mocks introduction / tutorial here

C# Threading : Workers processing list

Using ThreadSafeQueue class as list of items to process.

Class used as a worker, takes list as parameter, creates a thread to run in.

public class MyWorker
{
private Thread _myThread;
public Thread WorkerThread
{
get { return _myThread; }
set { _myThread = value; }
}

ThreadSafeQueue<int> _listOfNumbers;
public string NumbersProcess = string.Empty;
int total=0;

public MyWorker(ThreadSafeQueue<int> listOfNumbers)
{
_listOfNumbers = listOfNumbers;
_myThread = new Thread(new ThreadStart(this.DoProcess));
}

public void DoProcess()
{
int _localInt=0;
do
{
if (_listOfNumbers.TryDequeue(out _localInt))
{
total = total + _localInt;
NumbersProcess = NumbersProcess + _localInt.ToString() + ", ";
}
} while ((!_listOfNumbers.NoMoreItems) ||
(_listOfNumbers.NoMoreItems) && (_listOfNumbers.Count > 0));
}
}



 



Calling process creates 5 workers then populates list, then waits for workers to finish processing:



//Queue to hold numbers to process
ThreadSafeQueue<int> listOfInts = new ThreadSafeQueue<int>();

//5 worker objects (threads)
MyWorker[] workers = new MyWorker[5];
for (int i = 0; i < workers.Length; i++)
{
workers[i] = new MyWorker(listOfInts);
workers[i].WorkerThread.Start();
}

//Populate list to process
for (int i = 0; i < 1000; i++)
{
listOfInts.Enqueue(i);
}

//Flag that no more items will be added to queue
listOfInts.NoMoreItems = true;

//Wait for workers to finish then display details
foreach (MyWorker worker in workers)
{
worker.WorkerThread.Join(); //Wait for worker to finish processing.
tbxDetail.Text=tbxDetail.Text+string.Format("Numbers processsed {0} \n",worker.NumbersProcess);
}
tbxDetail.Text = tbxDetail.Text + string.Format("\n\n");

Tuesday, 29 April 2008

C# Constructors

See full discussion here

Base

public myClass() : base()

Overload

class myClass
{
public myClass()
{
//No parameters
}

public myClass(string param1) : this()
{
//No parameters will be called before me!
}
}

Monday, 28 April 2008

Asp.Net : "A first chance exception"

Getting huge amount of "A first chance exception" messages when running web-site under IDE for first time (after a rebuild).

These were being reported because programmer had turned off 'Just my code' within Tools->Options->Debugging->General

Tuesday, 22 April 2008

SQL Server : Query on date and HQL

Date format is 'YYYY-MM-DD'

e.g.

select * from orders where Date='2008-04-22'



HQL (as literal)

from Activity a where a.Date between '2008-01-01' and '2008-02-29'

Thursday, 3 April 2008

Bind Dictionary to WinForm ComboBox

To bind a dictionary to a combo box:

 

            cmbTable.DisplayMember = "Key";
cmbTable.ValueMember = "Value";
cmbTable.DataSource = new BindingSource(myDictionary, null);

Friday, 7 March 2008

Validators and hidden fields

It seems that there is a strange bug with validators.

If the field that is being validated is hidden in some circumstances a Java error " 'null' is null or not a object"

This doesn't happen on all servers / clients, the problem on site was produced on the clients IIS but on our one wasn't showing, this is with the same code running.
It also didn't happen with a previous version that had one less validator.

The solution was to move the validators next to the actual fields that were being validated. The validation messages were being shown in a validator summary.

The only way I could reproduce it in the office, was to move the post buttons off the Update panel and to do a full postback. This error continued to happen on site, even if I disabled the validators for the hidden control.s