Monday, August 18, 2014

Debugging Threads with Concurrency Visualizer

Features of Concurrency Visualizer:

  • Shift+Alt+F5
  • Fast processing, faster loading
           o Supports big traces
  • Supports big traces
  • Support s EventSource and custom markers
            o Built-in support for TPL, PLINQ, Sync Data Structures, and Dataflow
            o Built-in support for your own EventSource types
                    § Also provides Visual Studio markers API
  • New Visualizations
             o e.g. defender view

Demo:

Here are few steps to know that how to utilize “Concurrency Visualizer” for multithreaded applications.

  1. Create a Console project.
  2. Create custom EventSource to trace in the visualizer as below:
    [EventSource(Guid = "7BBB4E52-7DA7-45EB-B6C2-C01D460A087C")]
    class MyEventSource : EventSource
    {
    internal static MyEventSource log = new MyEventSource();

    [Event(1)]
    public void SomeEventHandled(int data)
    {
    WriteEvent(1, data);
    }
    }

    To provide GUID to the event source use “guidgen” and copy newly generated GUID. Then put it in the attribute of the EventSource class.

    clip_image002
  3. Now complete the test code to proceed the demo.

    Full code snippet:

    using System.Threading;
    using System.Threading.Tasks;

    namespace ConcurrencyVisualizerDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    var list = new List<Task>();
    for (int i = 0; i < 10; i++)
    {
    list.Add(Task.Run(() =>
    {
    //Loging here
    MyEventSource.log.SomeEventHandled(21);
    Thread.SpinWait(1000000);
    }));
    }
    Task.WaitAll(list.ToArray());
    }

    [EventSource(Guid = "7BBB4E52-7DA7-45EB-B6C2-C01D460A087C")]
    class MyEventSource : EventSource
    {
    internal static MyEventSource log = new MyEventSource();

    [Event(1)]
    public void SomeEventHandled(int data)
    {
    WriteEvent(1, data);
    }
    }
    }
    }


  4. There is optional extension “Concurrency Visualizer” for visual studio, which visual all the data to debug the concurrency between the treads. After installing this, you can find the option under Analyze menu. See image below:

    clip_image004
  5. Now copy the GUID  of MyEventSource and go to the Concurrency Visualizer’s settings. Then add new provider and put this copied guid in the provider.
    Analyze->Concurrency Visualizer->Advance Settings->Markers

    image

    Provide name and paste copied guid in the Privider GUID field. Click ok to complete provider addition.
  6. Now start Concurrency Visualizer by pressing Shift+Alt+F5 and it will start Shift+Alt+F5. Now it will start generating reports. See the below images:

    image

    image

    image

    image



Tuesday, April 15, 2014

How does === means different than == in JavaScript?

These two operators do not mean the same and does different operation too.
== verifies if the compared values are equal
=== verifies if the variables that are compared have the same value and are the same type
JavaScript's standard equality operators (== and !=) check if two expressions are equal (or not equal). If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison. Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.
JavaScript's identity (strict equality) operators (=== and !==) behave identically to the equality operators except no type conversion is done, and the types must be the same to be considered equal. Here are a few examples:
"3" == 3 // true
"3" === 3 // false
1 == true // true
1 === true // false
"1" == true // true
"1" === true // false

Code snippet:

<script type="text/javascript">
var a = 5;
var b = '5';
var c = 5;
if(a==b)
{
document.write('a and b have the same value');
}

if(a===b)
{
document.write('a and b have the same value and the same type');
}
if(a===c)
{
document.write('a and c have the same value and the same type');
}
</script>


Saturday, April 12, 2014

Use your Windows Phone 7 device as a portable

A simple registry edit turns your WP7 device into a USB drive.

Open the Registry Editor (regedit), go to HKEY_LOCAL_MACHINE\SYSTEM and then expand the CurrentControlSet\Enum\USB folder.

Search for PortableDeviceNameSpaceExcludeFromShell and there you will get the other setting which are need to set are below:

> Change ShowInShell from 0 to 1.
> Change PortableDeviceNameSpaceExcludeFromShell from 1 to 0.
> Change EnableLegacySupport from 0 to 1. That's it. If there's more than one Windows Phone 7 device listed.

Cheers! Now all done.

Note: Remember to take backup before doing any registry changes..

Tuesday, January 21, 2014

How to troubleshot Windows - References



it’s often helpful to repair the MBR (Master Boot Record) to restore the Windows 7 boot loader—and you can do it easily from the Windows installation disc.


Repairing the Master Boot Record
If you want to restore the master boot record, you can simply type in the following command:
bootrec /fixmbr
You can also write a new boot sector onto the system partition with this command (which is often more useful):
bootrec /fixboot

And of course, if you just use bootrec /? you’ll be able to see all the options.

Monday, December 26, 2011

What is the use of AsyncCallback in Client server programs and GUI improvements? why should we use it?

When the asyn method finishes processing, the AsyncCallback method is automatically called, where post processing stmts can be executed. With this technique there is no need to poll or wait for the asyn thread to complete.

This is some explanation on Aynsc Call back usage:

Callback Model: The callback model requires that we specify a method to callback on and include any state that we need in the callback method to complete the call. The callback model can be seen in the following example:

static byte[] buffer = new byte[100];

static void TestCallbackAPM()
{
string filename = System.IO.Path.Combine (System.Environment.CurrentDirectory, "mfc71.pdb");

FileStream strm = new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.Read, 1024,
FileOptions.Asynchronous);

// Make the asynchronous call
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(CompleteRead), strm);
}


In this model, we are creating a new AsyncCallback delegate, specifying a method to call (on another thread) when the operation is complete. In addition, we are specifying some object that we might need as the state of the call. For this example, we are sending the stream object in because we will need to call EndRead and close the stream.



The method that we create to be called at the end of the call would look somewhat like this:



static void CompleteRead(IAsyncResult result)
{
Console.WriteLine("Read Completed");

FileStream strm = (FileStream) result.AsyncState;

// Finished, so we can call EndRead and it will return without blocking
int numBytes = strm.EndRead(result);

// Don't forget to close the stream
strm.Close();

Console.WriteLine("Read {0} Bytes", numBytes);
Console.WriteLine(BitConverter.ToString(buffer));
}


Other techniques are wait-until-done and pollback



Wait-Until-Done Model The wait-until-done model allows you to start the asynchronous call and perform other work. Once the other work is done, you can attempt to end the call and it will block until the asynchronous call is complete.



// Make the asynchronous call
strm.Read(buffer, 0, buffer.Length);
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null);

// Do some work here while you wait

// Calling EndRead will block until the Async work is complete
int numBytes = strm.EndRead(result);


Or you can use wait handles.



result.AsyncWaitHandle.WaitOne();


Polling Model The polling method is similar, with the exception that the code will poll the IAsyncResult to see whether it has completed.



// Make the asynchronous call
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null);

// Poll testing to see if complete
while (!result.IsCompleted)
{
// Do more work here if the call isn't complete
Thread.Sleep(100);
}

Thursday, December 22, 2011

How to open DateTime picker C# control programmatically on Button Click

You have to use interop to to send request to windows that show DataTimePicker 
on click of the button


//include namespace section 
using System.Runtime.InteropServices;

//declares
[DllImport("user32.dll")]
private static extern bool PostMessageForCalender(
IntPtr hWnd, // handle to destination window
Int32 msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);

const Int32 WM_LBUTTONDOWN = 0x0201;

//method to call calender dropdown
private void button1_Click(object sender, EventArgs e)
{
Int32 x = dateTimePicker1.Width - 10;
Int32 y = dateTimePicker1.Height / 2;
Int32 lParam = x + y * 0x00010000;

PostMessageForCalender(dateTimePicker1.Handle, WM_LBUTTONDOWN, 1,lParam);

}

Monday, December 12, 2011

Most Useful free .Net Libraries

Mathematics
  • Math.NET Numerics - special functions, linear algebra, probability models, random numbers, interpolation, integral transforms and more
Package managers for external libraries
  • NuGet (formerly known as NuPack) - Microsoft (developer-focused package management system for the .NET platform intent on simplifying the process of incorporating third party libraries into a .NET application during development)
  • OpenWrap - Sebastien Lambla - Open Source Dependency Manager for .net applications
Build Tools
  • Prebuild - Generate project files for all VS version, including major IDE's and tools like SharpDevelop, MonoDevelop, NAnt and Autotools
Dependency Injection/Inversion of Control
Logging
Validation
Design by Contract
Compression
Ajax
Data Mapper
ORM
Charting/Graphics
PDF Creators/Generators
Unit Testing/Mocking
Automated Web Testing
Misc Testing/Quality Support/Behavior Driven Development (BDD)
URL Rewriting
Web Debugging
  • Glimpse - Firebug for your webserver
Controls
MS Word/Excel Documents Manipulation
  • DocX to create, read, manipulate formatted word documents. Easy syntax, working nicely, actively developed. No Microsoft Office necessary.
  • Excel XML Writer allows creation of .XLS (Excel) files. No Microsoft Office necessary. Been a while since it has been updated. It also provides code generator to create code from already created XLS file (saved as xml). Haven't tested this but looks very promising. Too bad author is long time gone.
  • Excel Reader allows creation/reading of .XLS (Excel) files. No Microsoft Office necessary. Been a while since it has been updated.
  • Excel Package allows creation/reading of .XLSX (Excel 2007) files. No Microsoft Office necessary. Author is gone so it's out of date.
  • EPPlus is based on Excel Package and allows creation/reading of .XLSX (Excel 2007). It is actually the most advanced even comparing to NPOI.
  • NPOI is the .NET version of POI Java project at http://poi.apache.org/. POI is an open source project which can help you read/write xls, doc, ppt files.
Social Media
  • LinqToTwitter - Linq-based wrapper for all Twitter API functionality in C#
  • Facebook C# SDK - A toolkit for creating facebook applications / integrating websites with Facebook using the new Graph API or the old rest API.
Serialisation
  • sharpserializer - xml/binary serializer for wpf, asp.net and silverlight
  • protobuf-net - .NET implementation of google's cross-platform binary serializer (for all .NET platforms)
Machine learning
  • Encog C# - Neural networks
  • AForge.net - AI, computer vision, genetic algorithms, machine learning
Unclassified
Others:
Paid Libraries:


Check more at stackoverflow