Next step of programming

Just another WordPress.com weblog

Archive for January 2009

Creating Simple Application in Silverlight

without comments

For creating silverlight project you should visual studio 2008 sp1 and download sliverlight tools for vs2008 from this link
Microsoft® Silverlight™ Tools for Visual Studio 2008 SP1

After installation. Open visual studio 2008 and click create new project window and select silver tree node under the visual C# category. Select silver light application and create project.

Silverlight Application Project

It will ask how you want to run your silver light application. There are two option first to add new project in which your application will be hosted or in second the it will automatically generate one dummy test page for you. We will select first option.

Silverlight Supporting/Hosting web page

Silverlight Supporting/Hosting web page

Now we have two projects listed in solution explorer.

Two Project Silverlight Application and Web Application

Two Project Silverlight Application and Web Application

Select silverlight application and open page.xaml. This is are main page, Here we will write some of the xaml commands as written below. With the help of these commands we added textbox and button to our page.

XAML code for the controls

XAML code for the controls

On click of this button we will set the content of the textbox to the button caption. For that we will right code in the button click event in page.xaml.cs file

Button click event method

Button click event method


If you want to design your application more professional you can use Microsoft Expression Blend 2. Also install Service Pack 1 for Blend 2 from following link

Microsoft Expression Blend™ 2 Service Pack

Written by A.Sethi

January 29, 2009 at 11:11 am

Posted in Silverlight

Tagged with ,

Handling services with the help of ServiceController from your application

without comments

You may need to get the information regarding service or need to start or stop it. Doing it from the C# is very easy with the help of ServiceController. Just create the object of the servicecontroller with the specified service and you can control that service with the help of that object


ServiceController serviceController = new ServiceController("servicename"); 

//Start it now
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);

//Stop it now
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);

Also you can get verious other information from this object like status, display name, dependencies and so on.

Written by A.Sethi

January 28, 2009 at 5:46 am

Posted in C#, General

Tagged with ,

Accessing Controls running in different Thread

with 2 comments

Controls can be accessed and manipulated only from those thread in which it is created. If we are in some other thread and want to access the controls of another thread we need to follow on different way. Directly accessing will generate the exception saying control were created in another thread so can’t be accessed.

To sort our this first we have to check the function “IsInvokeRequired” of the control. if it is true then it is in another thread and we need to call it with the help of “Invoke” or “BeginInvoke” methods.

private void Form1_Load(object sender, EventArgs e)
{
NewThread();
}

public void NewThread()
{
Thread thread = new Thread(new ThreadStart(threadMethod));
thread.Start();
}

public void threadMethod()
{
if (this.InvokeRequired)
{ 
this.Invoke(new MethodInvoker(threadMethod));
}
else            
{
this.Text = "Form caption is changed";
}
}

Written by A.Sethi

January 27, 2009 at 8:48 am

Compression & Decompression using System.IO.Compression namespace (GZip Format)

with 2 comments

Microsoft has include the compression classes in the framework itself. Now we can perform compression without having the 3-party libraries. One of the format GZip is given below with a sample class

public class GZipCompression    
{
    /// 
    /// Compress the data from specified file to new file        
    /// 
    /// File to be compressed
    /// Compressed file        
    public void CompressFile(string inputFile, string outputFile)
    {
    //Load the content from the file            
    FileStream inputStream = new FileStream(inputFile, FileMode.Open,
    FileAccess.Read,FileShare.Read);
    byte[] buffer = new byte[inputStream.Length];

    int readCount = inputStream.Read(buffer, 0, buffer.Length);
    if (readCount == 0)
        return;
    inputStream.Close();

    //Compress the data in the memory stream            
    MemoryStream memoryStream = new MemoryStream();
    GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
    gzipStream.Write(buffer, 0, buffer.Length);
    gzipStream.Close();

    //write all the compress data to new file            
    byte[] compressedBuff  = new byte[memoryStream.Length];
    int compressedCount = 0;
    memoryStream.Position = 0;
    compressedCount = memoryStream.Read(compressedBuff, 0, compressedBuff.Length);
    File.WriteAllBytes(outputFile, compressedBuff);
    memoryStream.Close();
    }

    /// 
    /// Decompress the compressed file for format GZip        
    /// 
    /// File to be decompressed
    /// file to be compressed        
    public void DecompressFile(string inputFile, string outputFile)
    {
    //Load the content from the file            
    FileStream inputStream = new FileStream(inputFile, FileMode.Open,
    FileAccess.Read, FileShare.Read);
    byte[] buffer = new byte[inputStream.Length];
    int readCount = inputStream.Read(buffer, 0, buffer.Length);
    if (readCount == 0)
        return;

    //decompress the data in the memory stream            
    MemoryStream memoryStream = new MemoryStream();
    memoryStream.Write(buffer, 0, buffer.Length);
    memoryStream.Position = 0;
    GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress);

    byte[] decompressedBuff = new byte[1024];
    string fileData = string.Empty;
    while (true)
    {
        int currentCount = gzipStream.Read(decompressedBuff, 0, decompressedBuff.Length);
        if (currentCount == 0)
            break;

        fileData += BytesToString(decompressedBuff, currentCount);
    }

    //Write the data to the new decompressed file            
    StreamWriter outputWriter = new StreamWriter(outputFile, false);
    outputWriter.Write(fileData);
    outputWriter.Close();

    //Close all the streams
    gzipStream.Close();
    memoryStream.Close();
    inputStream.Close();
    }

    private string BytesToString(byte[] buff, int length)
    {
    string content = string.Empty;
    for(int i = 0; i <length; i++)
    {
        content += Convert.ToString((char)buff[i]);
    }
    return content;
    }
}

Written by A.Sethi

January 27, 2009 at 8:27 am

Implicit Operator Overloading

without comments

We can assign values to the int, double, float, string data types directry by writing (int l = 0) and even we can do the same to String data type which is a reference type not the value type. How to implement this method to our reference data type. Solution for this kind of requirement is Implicit operator overloading. Now let us see how it works

public class Amount
{
double _value;

/// 
/// Gets or sets the Amount value.    
///     
public double Value
{
get        
{
return _value;
}
set        
{
_value = value;
}
}

public Amount(double value)
{
_value = value;
}

public override string ToString()
{
return string.Format("{0}", Value);
}

/// 
/// Set the amount value on the basis of the double value sent    
/// 
/// Amount in double data type.
/// Implicitly created instance.    
public static implicit operator Amount(double amount)
{
Amount amt = new Amount(amount);
return amt;
}

/// 
/// Get/Return the value set to the instance of the amount class    
/// 
/// Amount Instance
/// Amount value in double    
public static implicit operator double(Amount amount)
{
return amount.Value;
}
}

We were in need to new kind of data type which is specially required for our required. We designed a class name as Amount. But problem was of assigning the values. All the time we have to use new keyword. So we implimented this implicit overloading and we got a new data type for our self. Also we can add more functions to it

Written by A.Sethi

January 1, 2009 at 11:14 am