For my senior design project at Wentworth, we are in need of a micro controller. I ordered the Arduino from SparkFun.com and it arrived yesterday. At the core is the ATMega328 which is plenty powerful for our application (which I’ll introduce in a later post).
The device has so many uses with its various shields and add ons such as ethernet and wifi. If you ever wanted to start getting into electronics, hardware or if you ever wanted to make something you’ve always wanted, the Arduino is a great start.
The ATMega328 contains the Arduino bootloader so we can flash it with the WIRE programming language (it’s pretty much C++, just dumbed down). It comes with plenty of examples so you can tweak existing code or piece together you own application.
I’ve been busy the past few weeks, mainly with school but I’ve had spare time to have fun with.
I went to NYC for “A State of Trance 450″ which was a big party celebrating an electronic radio show. I had a blast, 9PM-5AM was not enough! Wished I could’ve gone to the next night, but I had to get back for Easter.
The other day I had a simple problem that I couldn’t seem to find an answer to online. I had a background worker running on a thread that needed to access a collection of listview items. The problem should be evident to most developers, I cannot access a control that was created via another thread.
If you try, you will get this all too familiar error:
“Cross-thread operation not valid: Control ‘listView1′ accessed from a thread other than the thread it was created on.”
What was my solution?
Create a delegate that returns the collection in a string list. Simple, yes, effective? You bet.
private delegate List GetListViewDelegate();
private void btnStartThread_Click(object sender, EventArgs e)
{
//Start the worker thread
var myWorker = new BackgroundWorker();
myWorker.DoWork += WorkerThread;
myWorker.RunWorkerAsync();
}
private void WorkerThread(object sender, DoWorkEventArgs e)
{
//Grab the list via the delegate I created
var myList = (List)Invoke(new GetListViewDelegate(GetListViewItems));
foreach (string item in myList)
{
MessageBox.Show(item);
}
}
public List GetListViewItems()
{
//Return the list back to the delegate
var pathList = new List();
foreach (ListViewItem item in listView1.Items)
pathList.Add(item.Text);
return pathList;
}
PLEASE NOTE: My syntax highlighter omitted the <string> type elements. “List” should be “List<string>”!
Solution Files
I’ve included a source file so you can try it out yourself. Download it here