This blog isn’t updated anymore – New address : http://proofofconcepts.wordpress.com/
C# Dispatcher
Posted by Guillaume on December 5, 2007
As I’ve already said I’m pretty new to WPF and I got stuck with an "System.InvalidOperationException" exception in one of my program.
Let’s say you want to write a text editor app and you want to schedule an auto save process every x second. The task is pretty easy you just have to create a Thread that start an infinite loop and within grab the text from the textbox, save it somewhere, and sleep for x second.
UI of our Text editor :

.xaml
code
I you try this code it will CRASH with the message "The calling thread cannot access this object because a different thread owns it."
The UI thread owns the component we are trying to retrieve data from, and another thread can’t access it. The only way to deal with this problem is to use the main thread Dispatcher to place something in the queue. Then we can ask the UI thread that have delegated, to asynchronously start a task (BeginInvoke).
We can also specify a priority for the task. There are 12 levels you can see here, but in our example since saving is very important I set the highest priority (10).
Here is the code that fix the problem :

Tags: C#, WPF, Exception, Dispatcher, BeginInvoke, Thread
Posted in my.Net Labs | 4 Comments »
Unlock iPhone
Posted by Guillaume on December 4, 2007
Few time ago when the first iPhone have been unlocked, I was impressed and wondered how the guys did it. I grabbed a copy of iUnlock src code (that seems to be the core of anySIM) in order to see the hack. It was pretty interesting but I looked forward for the anySIM src code to be un leash.
This morning while I was reading the hackint0sh forum I discovered that anySIM src code will be released friday on this GOOGLE code page.
Stay tuned.
FYI, I don’t and won’t own an iPhone so please do not send me email asking how to unlock your phone.
Posted in Bloggin' | Leave a Comment »
FlickvieweR .NET 3.0
Posted by Guillaume on November 20, 2007
It’s been a long time since I post something here, but I’ve been too busy these days. I promised to talk about my AD Grabber but I’ve another project in mind about quite the same subject so I’ll have another opportunity to speak about. C# and Active Directory .
Today I would like to show you my last night craft. It is called FlickvieweR and it is ‘yet another FlickR app’.
I provide the whole source code in this post but be warned that it is an alpha prototype release, so some bugs may will occur.
FlickvieweR display a ‘stacked photo slideshow’ from FlickR by providing Tags or an username in order to display a photoset. In this post I’m going to focus on the animation and display part of the program since grabing data from FlickR is pretty easy.(Maybe it will be the subject of a next post).
Ok so first of all, what FlickvieweR looks like ? (More screen shots here or here)
As you can see all the pictures are stacked with a random rotate transform and you can also see that a drop shadow is applied to each photo. To make it cool to watch we are going set a fadein transition effect when a new pic. is displayed.
In the project the display zone is handled by myImageViewer.xaml & myImageViewer.xaml.cs.
Ok let’s start coding
First of all we need to create a transparent window, I usually use Microsoft Expression Blend to create the UI because it makes the job so easy
and we can quicly have a real cool looking.
Here are my properties :
The animation :
All the photo we are going to display are stored in List and we want each photo to : FadeIn (so an opacity from 0 to 1) during 3s. and then wait for 5s. to display the next one.
In order to create this loop I’m going to show you a trick that I often always use in that case. Playing with Timers or Threads is sometimes tricky and the animation process allow use to create a ‘timer’ by using a ‘transparent’ animation. For instance setting an opacity from 1 to 1 during 5000ms and then fire up a completed event creates a timer. This is what we are going to use.
Animation code :
//The storyboard that house all our animations
Storyboard myStoryboard = new Storyboard();
//The fadeIn animation
DoubleAnimation myDoubleAnimation = newDoubleAnimation();
myDoubleAnimation.From = 0; //The start opacity value. 0 means the photo is transparent
myDoubleAnimation.To = 1; //The end opacity value. 1means the photo is visible
myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000)); //Run the animation during 3 seconds
myDoubleAnimation.AutoReverse = false; //Don’t want the animation to run in reverse
myDoubleAnimation.Completed += new EventHandler(wait);//fire a completed event in order to start the ‘Wait timer’
myStoryboard.Children.Add(myDoubleAnimation);//add the animation the story board
Storyboard.SetTargetName(myDoubleAnimation, img.Name);//set animation target on the image we want to animate
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Image.OpacityProperty));//set the propety that will be used by the animation(here Opacity)
img.BeginAnimation(Image.OpacityProperty, myDoubleAnimation); //Start the animation
The fadeIn animation is now complete, we create the same for the wait animation but with
myWaitanimation.From = 1 //The start opacity value. 1 means the photo is visible
myWaitanimation.To= 1;//The end opacity value. 1 means the photo is visible
myWaitanimation.Completed += new EventHandler(goNextt);//fire a completed event in order to display another photo
The Display zone:
On the transparent we created at the begin, we going to add the photos, so to add a new pic we need 3 important things: photo url, photo width, photo height. Then we are going to apply 2 differents effects a rotate transform and then a drop shadow
add image code:
Random rdm = new Random(); //use to randomize the angle
BitmapImage bi = new BitmapImage(new Uri(imgUrl)); //Create a new bitmapimage with the flickr url
img = newImage();//create a ui image component
DropShadowBitmapEffect shadow = new DropShadowBitmapEffect();//create the drop shadow effect
img.BitmapEffect = shadow; //apply the drop shadow the the image ui component
img.Opacity = 0; //set the component opacity to 0 since the animation start from 0
img.Source = bi;//fill the component with the bitmap image
img.Name = "_" + Convert.ToString(imgNum);//set an image name
img.Width = (double)p_Width;
img.Height = (double)p_Height;
int angle = isPositive ? rdm.Next(9) * (-1) : rdm.Next(9);//Pick a angle from -9 to +9°
isPositive = !isPositive;//swich the rotation
img.RenderTransform = new RotateTransform(angle);//apply the rotation
//Here goes the animation code !!
LayoutRoot.Children.Add(img);//Add the ui image component to the main window layout
That’s pretty much for this time, we see how to create an C# animation, how to apply some component transformations (dropshadow, rotation) but you can craft your own animation based on other properties.
You can grab the VS project here or download FlickvieweR.exe here. DO NOT FORGET TO INSTALL the .NET 3.0 Framework.
The really beautyful photos we can see on the screenshot are from one of Romain Guy’s photo photosets.
Tags: c#, animation, FlickR, Storyboard, DoubleAnimation, Image
Posted in my.Net Labs | Leave a Comment »
On my X-mas wish list
Posted by Guillaume on November 8, 2007
Here is what I definitely want for X-Mas this year, I know it’s pretty expensive but it will fit so nice my apartment
Posted in Bloggin' | Leave a Comment »
Lets dig our way to LDAP
Posted by Guillaume on November 7, 2007
In this first ‘coding post’ we will see how to reach and retrieve data from an Active directory server using C#.
First of all we have to use the Active.DirectoryServices namespace in order to play with some of the AD Classes. Let start with a little sample of code that reach an AD server and retrieve a list of all Organizational Units we can find on the first level.
using System.DirectoryServices;
namespace myNS
{
public class c{
DirectoryEntry myLDAP;
DirectorySearcher mySearcher;
public c()
{
connectLDAP();
}
private void connectLDAP(){
myLDAP = new DirectoryEntry("LDAP://myServerAddr" , user, passwd);
mySearcher = new DirectorySearcher(myLDAP);
mySearcher.Filter = "(objectClass=organizationalUnit)";
mySearcher.SearchScope = SearchScope.OneLevel;
mySearcher.Sort = new SortOption("ou", SortDirection.Ascending);
foreach (SearchResult res in mySearcher.FindAll())
{
DirectoryEntry _e = res.GetDirectoryEntry();
Console.WriteLine(_e.Properties["ou"].Value);
}
myLDAP.Close();
}
}
Ok, now we are going to explain each line of connectLDAP().
myLDAP = new DirectoryEntry("LDAP://myServerAddr" , user, passwd);
myLDAP is an AD hierachy object/node we will use to browse the categories.
mySearcher = new DirectorySearcher(myLDAP);
mySearcher is an object created to perform queries against an AD hierarchy Object (myLDAP)
mySearcher.Filter = "(objectClass=organizationalUnit)";
We can set filters to the DirectorySeacher object in order to only retrieve specific objects. In the sample we only want Organizational units object. We’ll see later what other kind of filter we can use.
mySearcher.SearchScope = SearchScope.OneLevel;
Another option of the DirectorySeacher object, by default the search will be on every level of the AD hierarchy. In the example we only want the immediate OU child so we chose OneLevel. Other scopes : Base and Subtree (check msdn for more details)
mySearcher.Sort = new SortOption("ou", SortDirection.Ascending);
We specify the result of the search to be sorted some DirectoryEntry properties values, here we want the result to be sorted on the Organizational Unit name. The SortDirection can be Ascending or Descending.
Now we have defined the search details, we can process the search
foreach (SearchResult res in mySearcher.FindAll())
The search process starts with mySearcher.FindAll() and return a collection of SearchResults objects stored in res. Here the search will return all objects(FindAll()) that fit the filter but we can also ask for only the first object of the result by using FindOne().
DirectoryEntry _e = res.GetDirectoryEntry();
During the iteration we define an DirectoryEntry object that represents an AD elements where we gonna grab data.
Console.WriteLine(_e.Properties["ou"].Value);
Once the DirectoryEntry object is created we can now get some AD information about it. Here we want to display the name of the OU. (see msdn for the list of the properties)
This a rough intro on how to get info from an Active Directory structure next time, I ‘ll go deeper into the filter and how to find users info.
Tags: C#, LDAP, query, DirectoryEntry, DirectorySearcher
Posted in my.Net Labs | 1 Comment »
Active directory Grabber intro.
Posted by Guillaume on November 6, 2007
As I said on my old blog, I will show and share the latest .net app I’m working on, called Active Directory Grabber. AD Grabber is a tool that extract data from LDAP and export to Excel via .csv files.
The main goal of this app is to help the IT to be in compliance with some policies (Sarbane-Oxley,..etc.) We often need to know the a user account’s status (does it have a password, is it locked, is it disabled..etc.) and AD Grabber try to give an quick answer to these questions.
AD Grabber is based on .NET 3.0 and have been designed with Microsoft Expression Blend, I’ll talk about it for sure in few next posts.
I’ll post the whole code here but before I gonna write on some part of the source.
Here is a screen shot of AD Grabber :
![]()
Posted in my.Net Labs | Tagged: , .net, Active directory Grabber, C#, LDAP, Microsoft expression blend | 1 Comment »
