-
-
Silverlight 2 does support threads through much of the same API as the big .NET FX. So you use classes in System.Threading such as Thread and ThreadPool. Or you can use the higher-level BackgroundWorker type from System.ComponentModel. This is anyway the recommended approach.
private
void btnWorker_Click(object sender, RoutedEventArgs e)
{
_bkgndWorker = new BackgroundWorker();
_bkgndWorker.DoWork += new DoWorkEventHandler(_bkgndWorker_DoWork);
_bkgndWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bkgndWorker_RunWorkerCompleted);
_bkgndWorker.ProgressChanged += new ProgressChangedEventHandler(_bkgndWorker_ProgressChanged);
_bkgndWorker.WorkerReportsProgress = true;
_bkgndWorker.WorkerSupportsCancellation = true;
_bkgndWorker.RunWorkerAsync(10000);
}
From the various event handlers, you can freely access any UI elements because the class magic makes your code run within the UI thread. In .NET FX, when you use plain threads, or the thread pool, you can't directly access UI elements because of thread is different from the UI thread and the UI elements are not thread-safe.
Silverlight 2.0 offers a facility to cross the thread boundaries and reach directly the UI thread. It's not magic or a different runtime. It's simply a facility to make your coding easier. Here's how it works.
private
void btnThread_Click(object sender, RoutedEventArgs e)
{
ThreadStart code = new ThreadStart(DoSomething);
Thread t = new Thread(code);
t.Start();
}
private
void DoSomething()
{
Thread.Sleep(5000);
this.Dispatcher.BeginInvoke( delegate {
lblThreadOutput.Text = "Updated from a background, non-UI thread.";
});
}
The Dispatcher property on the UserControl class does the trick. It refers to a System.Windows.Threading.Dispatcher object that offers a BeginInvoke method through which you specify the code that it has to dispatch to the right UI thread. You pass BeginInvoke either an anonymous delegate or a function pointer. Nice and effective. One of those things that Silverlight does better than the big .NET FX.
-
-
Coming to Dublin in just a few weeks (May 27) for a full day of public, 9-to-5, LINQ, LINQ-to-SQL from ASP.NET and Silverlight. Read all details here.
A pattern-oriented look at LINQ and LINQ-to-SQL from within Web and Silverlight applications
We'll start the day with a 10,000 look at LINQ. Hey, I can hear some of you complaining that it is just the "usual" boring stuff about LINQ being so cool etc. Things that you've probably heard a zillion times. Well, sort of. As Don Box told me once, it's all about perspective. So in the first module you'll hardly find me discussing how to group/join data. I'll focus on the architectural model and drive you naturally to understand the various flavors of LINQ. A full understanding of the IQueryable interface will open up a new perspective on LINQ-to-SQL and LINQ-to-Entities.
In the end, LINQ-to-SQL is two-folded. On one end, it is a programming tool for a DAL, but if you know it well it can morph into a simplified, but not less effective, O/RM tool.
After due lunch, discussing data access strategies from ASP.NET and Silverlight with patterns. Which patterns? MVP and MVVM. And services. And data feeds.
It'll be fun. Don't hesitate to ask more information :)