Archive for the Category tricks

 
 

Google AdSense preview tool

The Google AdSense preview tool is an addition to the right-click menu for Windows Internet Explorer 6.x, allowing you to preview the ads that may show on
any web page. With just a few clicks, you can see what ads may appear on your new web pages, or make an educated decision on whether to add AdSense to your
existing site pages.To install it, you’ll need to add a registry file to your Windows system.

Use BackgroundWorker with any class

BackgroundWorker works only with the classes that implements interface ISynchronizeInvoke – in order to use BackgroundWorker with any class necessary to make the following changes in the code.

The code after the changes ( BackgroundWorker.cs ):

private void InvokeDelegate(Delegate del, params object[] args)
{
    ISynchronizeInvoke synchronizer = del.Target as ISynchronizeInvoke;
    try
    {
        if(synchronizer != null) //A Windows Forms object
        {
            if (synchronizer.InvokeRequired == false)
            {
                del.DynamicInvoke(args);
                return;
            }
            synchronizer.Invoke(del, args);
        }
        else //Not a Windows Forms object
        {
            del.DynamicInvoke(args);
        }
    }
    catch (Exception e)
    {
        Trace.WriteLine(e.Message);
    }
}

 

 

Thanks to Roy Osherove for help in the solution of this problem

System.Drawing.Color serialization

The Person class includes a HairColor property that stores a Color object. Because a Color cannot be automatically serialized/deserialized using the .NET System.Xml.Serialization.XmlSerializer class, the Color object is saved to the XML file as an HTML string, and converted back into a Color when the XML file is loaded.

sing System;
using System.Drawing;
using System.Xml.Serialization;
 
 
//To support binary serialization you need to 
// add the "[Serializable]" attribute
[Serializable]
public class Person
{
    private Color _HairColor = Color.Empty;
        
    // Initializes a new instance of the Person class.
    // (required for serialization)
    public Person()
    {
            
    }
        
        
    // Set serialization to ignore this property
        // because the 'HairColorHtml' property
        // is used instead to serialize
        // the 'HairColor' Color as an HTML string.
    [XmlIgnoreAttribute()]
    public Color HairColor
    {
        get
        {
            return _HairColor;
        }
        set
        {
            _HairColor = value;
        }
    }
        
        
    // Serializes the 'HairColor' Color to XML.
    [XmlElement("HairColor")]
    public string HairColorHtml
    {
        get 
        { 
            return
                ColorTranslator.ToHtml(_HairColor);
        }
        set 
        { 
            _HairColor = ColorTranslator.FromHtml( value );
        }
    }
        
}

Installing Enterprise Library on shared web hosting

If you develop web applications which use Enterprise Library that it is necessary to prepare it, but you will need to recompile the code .Just go into the Project Properties dialog for the Common project, and under Configuration Properties\Build, find the Conditional Compilation Properties property and remove ;USEWMI;USEEVENTLOG;USEPERFORMANCECOUNTER . Once you recompile, the  Enterprise Library will be ready for deployment.

How to catch Exception in Background Worker

Juval Lowy provides the BackgroundWorker component to facilitate easy asynchronous invocation with Windows Forms in Net 1.1, but when an exception occurs in BackgroundWorker(DoWork event) – it behaves as though happened nothing.It is possible to understand what happened just by analyzing Error
argument as shown below:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
 throw new ApplicationException("exception");
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
 string message = string.Empty;
 if( e.Cancelled )
 {
   message = "Cancelled";
 }
 else
 {
  if(e.Error !=null)
  { // analyze error here
    message = "Error";
  }
  else
  {
    message = "Stoped";
  }
 }
 
 MessageBox.Show(message);
}


Page 3 of 512345