Sunday, December 27, 2009

My Solution: Cross-thread operation not valid error while setting a value to control through property


Last week I was working on a code in which a control's property was being accessed from a thread. Obviously as it was a multithreaded application, it should throw the "Cross-thread operation not valid" error. I know that if we are accessing any method from a different thread, we need to use the delegates and events. But my concern was how I can do the same with property as there are no delegates associated with properties of a control.

I created one simple VS 2008 application which set the text property of a text box on a different thread, when I ran the program, I got the following exception.

"Cross-thread operation not valid: Control 'textBox' accessed from a thread other than the thread it was created on."

To tackle this I was thinking about a way which is similar to the techniques we use while handling the same error for methods i.e. make use of events and delegates.

So I created a simple delegate and event as follows

private delegate void SetControlValue(Control controlType,
string propertyName, object value);
private event SetControlValue SetControlValueEvent = null;


And wrote a new method as follows

private void SetControlValueMethod(Control controlType,
string propertyName, object value)

{ 
if (this.InvokeRequired)
{
this.Invoke(new SetControlValue(SetControlValueMethod),new object[]   

{controlType, propertyName, value });
}
else 

{
        PropertyInfo[] props = controlType.GetType().GetProperties(); 

foreach (PropertyInfo pi in props)
{
    if (pi.Name.ToUpper().Trim() == propertyName.ToUpper().Trim())
       {
            pi.SetValue(controlType, value, null);
       }
}
}
}


Here I have used reflection to get the PropertyInfo and used the Setvalue method to assign the appropriate value to the control.

And I used instead of calling

textBox.Text = _number.ToString();

I called the new method to set the values

SetControlValueMethod(textBox, "Text", _number.ToString());


This need to modify to add intellisense to the method so that the control type and their respective properties should come automatically and any error that may cause due to typo can be avoided.

Thursday, December 3, 2009

Primitive types


The data types which are directly supported by compiler are called as the primitive datatypes. These are directly mapped to the type from base class library, you don't need to call new of that data type to create a new primitive object.

E.g int I = 10;

Defines an integer and map int to System.Int32

Declaration of primitive types makes code more readable compared to other types.

Following are the primitive types defined in .NET

  • Sbyte
  • Byte
  • Short
  • Ushort
  • Int
  • Uint
  • Long
  • Ulong
  • Char
  • Float
  • Double
  • Bool
  • Decimal
  • Object
  • String
These are sealed classes and can not be inherited by any other class.




Saturday, March 1, 2008

Deepfish

Microsoft has unveiled the beta version of its new Mobile Web Browser “Deepfish” for the Windows Mobile.
Deepfish is a lightweight client application that leverages a powerful server side technology for delivery of content such as web pages to a Windows Mobile device. Content is displayed in a familiar desktop format that requires no additional work by the content or site author.
Mobile browsers required content to be specifically tailored to the mobile device, which does not really include rich page layouts. It also required developers and designers to do additional work, although the user base of mobile browsers is limited. To combat this limitation, the majority of today’s browsers use a single-column format which dynamically reformats existing pages by repositioning the content to fit in the limited screen size. This essentially “crushes” the page to fit the small screen. This approach, while an improvement in some cases, generally results in a difficult-to-view page that requires excessive scrolling in order to use the portions of the page. And when you see the page, it isn’t presented in the way the Web designer intended. Deepfish’s interface addresses this problem by providing users with a simple way to zoom in and out on the part of the Web page that interests them, while presenting the information as it was intended. All of this adds up to an easier and faster way to navigate through a page.

Deepfish provides:
  • A familiar look and feel of web pages on mobile as seen on desktop.
  • Bandwidth optimized rendering for faster content delivery.
  • Address bar web navigation.
  • Intuitive zooming, panning and cue map for quick navigation and browsing.
  • Support for simple link navigation and form submission.
Here is a cool video that demonstrates the capabilities of “Deepfish”

Saturday, December 29, 2007

ref vs out parameter

ref
Before passing ref parameter the sender class must instantiate the ref argument, so ref requires additional bandwidth to send the object both ways.
ref allows the called methods to modify the object to which the reference refers because the reference itself is being passed by reference.
out
An out argument is created by the called method and it is returned to the caller.

Power toy for .NETCF 3.5

Microsoft has released the Power Toy for .NETCF 3.5, it includes few important features that were available with .NET but missing from .NETCF like

· Remote Performance Monitor and GC Heap Viewer

· NETCF CLR Profiler

· App Configuration Tool (NetCFcfg.exe)

For additional details please refer http://blogs.msdn.com/netcfteam/archive/2007/09/12/power-toys-for-net-compact-framework-3-5-ctp-released.aspx