Ads by Google

Donate

If you like articles and you want an increase of them, please make a little donation. If you make a donation you can submit us by mail an argument for next articles!

Importo: 

add RSS

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati

Subscribe to Blog

Follow us on Twitter

follow us

Manfriday blog gadget

C#


C# 4.0 Parallel Computing

I think this is the best news of .NET 4.0, because allows to improve performance
of your application in a very simple and safe way and in many situations.
 
Safe is the key, infact I've never seen anyone to parallelize a for statement using
the ProcessorCount and delegate approach , because nobody want introduce
side effects in its code to parallelize a stupid for... anyway now with the native
implementation of Foreach and for statements is really safe and the improvement
of performance is real good.
 
// Sequential version             
foreach (var item in sourceCollection) 
{     
Process(item); 
} 
 // Parallel equivalent  
Parallel.ForEach(sourceCollection, item => Process(item));
 
When your action on a single item inside a for loop is independent from other items,
you can easily parallelize it as in the above example.
In this example if you have 4 processors in your workstation, the speedup will be
quite near to 4, that's pretty good. 
 
 
String to DateTime
Venerdì 10 Aprile 2009 13:22
 
Only a little example about how to use the ParseExact method of the DateTime Class.
Usually if you have to parse a string to obtain a DateTime suitable with SQLServer format, you probably build your own string dissecting dd, MMM, yyyy, etc...
 
for example with 'MMM' you can parse month strings too, like Sep, OCt, Nov, Dec , etc... 
 
Using this utility you can specify the string format of your string and obtain the DateTime automatically. Check MSDN to see all possible string formats. 
 
return DateTime.ParseExact(yourstring, "dd'-'MMM'-'yyyy' 'HH':'mm':'ss'.'fff", System.Globalization.CultureInfo.InvariantCulture);
 
 
Line Chart
Lunedì 26 Gennaio 2009 20:34
We are going to explore the graphics world with .NET framework ( in this case 2.0 compatible )  .
 
I will try to build a simple 2D line chart class that will be embedded in a panel. Then the paintable surface will be of the same size of the panel.
To draw elements you need to convert all values into a physycal position ( expressed in pixels starting from the top-right corner of the panel) .
 
 

With this instruction _panel.Tag = this; you could recall the graph istance from the panel resource, without make a global instance of the graph.
 
The big problem of .NET drawing objects comes out when you want delete something in the scene. Infact you can't handle every single object in the scene like in WPF.
A first easy solution could be repaint all the scene every time you want move or delete someting in the scene. I used this approach for this example ( as you can see in the mousemove handler function ),
because is very simple, but if you need to repaint several times and your scene is very rich, you can't use this technique because the scene will flick.
 
A most powerful approach could be to save a bitmap of the scene before the repaint, then you set this bitmap as background layer and repaint only what you need. probably you will need more memory, but you will see a big improvement of performance .
 
Follow comments into the code
 
//this is a class to represent a point with its physical position and real values. 
   class myPoint
   {
public myPoint(int _x,int _y,double _Xval,double _Yval){
X = _x;
Y = _y;
Xval = _Xval;
Yval = _Yval;
}
public int X;
public int Y;
public double Xval;
public double Yval;
   
   }
   
 
Open a generated Kml file by code
Mercoledì 07 Gennaio 2009 22:34

Now, we need to see how to call a generated kml file ( see this article ) to open it directly with google earth using suitable API.
 
After including the reference to the api we include right namespaces
 
using GoogleEarthCOMWrapper;
using EARTHLib; 
 
Then in our program we add a main function that must be threated. Using the class Kml we can create a kml file and then call it.
Creating an istance of GoogleEarthAPI you can open the file and set the camera, google earth will start in a separate thread and you will finish.  
 
 [STAThread]
        static void Main()
        {
 
 
Generate Kml file
Martedì 06 Gennaio 2009 23:20

We are going to see how to generate a Kml file that could be open with Google earth.
 
A Kml file is an xml file, but it's  characterized by some complex elements that have always the same shape, then it's possible to make the building of elements easier.
 
Now we are going to build a class to generate the Kml, then we need to use the System.Xml namespace. I fill the code with some comments, but it should be clear.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace KmlProgram
{
    class Kml
    {
//this will be the contenitor 
        public XmlDocument doc = new XmlDocument();
//this is Document element
        XmlNode DocumentNode;
        XmlNode kmlNode;
 
 
<< Inizio < Prec. 1 2 Succ. > Fine >>

Pagina 1 di 2
Ricerca personalizzata