Sunday, June 20, 2010

Open Data Protocol

Sometimes you might wondering how to develop module by module and then integrate all modules into a single system. Basically, you need a protocol that is design for the integration purpose. This might be something that you are looking for.

Check this out:

http://www.odata.org/developers/protocols/overview

Tuesday, June 8, 2010

Macros for Visual Studio

Run Visual Studio.
  1. Select Tools \ Macros \ Macros IDE.
  2. In the Project Explorer (within the Macros IDE), double click MyMacros \ Module1.
  3. Paste the following codes into the editor.

After that, close the Macros IDE. Now, you may customize the shortcut key to execute the Macro.
  1. Select Tools \ Customize from VS menu.
  2. Click Keyboard button (next to the Close button).
  3. In the "show command containing" field, type "PrintDateTimeSingleLine". This will shortlist the commands that is accessible through VS.
  4. In the "Press shortcut keys" field, press CTRL+0 and click Assign button.
  5. Click OK button to save the customized keyboard settings.
  6. In the coding editor, press CTRL+0 and VS will execute the Macro. So, you don't have to repeatly typing the current date/time for the comments anymore.


Sub PrintDateTime()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert(DateTime.Now.ToString("d.MMM.yyyy, ddd @ hh:mm"))
End If
End Sub


Sub PrintDateTimeSingleLine()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//" & DateTime.Now.ToString("d.MMM.yyyy") & "-lhw-")
End If
End Sub

Sub PrintDateTimeProcHeader()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert(DateTime.Now.ToString("d.MMM.yyyy") & "-lhw")
selection.NewLine()
selection.Insert("-")
End If
End Sub


Sub PrintSeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//-----------------------------------------------------------------------")
End If
End Sub

Sub PrintHeavySeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//=======================================================================")
End If
End Sub


Sub PrintShortSeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//-------------------------------------")
End If
End Sub

Sub PrintNotice()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//<======")
End If
End Sub

Saturday, May 1, 2010

Fullscreen Internet Explorer

To run the Internet Explorer in fullscreen,
   iexplore.exe -k
Below is the C++ code in calling Internet Explorer to show 2 tab pages in fullscreen mode:

CComQIPtr sp = pActiveSite;
(pActiveSite is IOleClientSite *)
CComPtr m_pBrowser;
HRESULT hr = sp->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2,
(void**)&m_pBrowser);
VARIANT_BOOL v = VARIANT_FALSE;
VARIANT_BOOL v1 = VARIANT_TRUE;

m_pBrowser->put_TheaterMode(v1);
m_pBrowser->put_TheaterMode(v);
Reference:
http://www.vistaheads.com/forums/microsoft-public-internetexplorer-general/289892-ie7-fullscreen-toggle-effect-programmatically-similar-like-f11.html

Saturday, April 17, 2010

AsyncOperationManager for WinForm

To run a process in a thread that is other than UI, you may use the following:

1. BackgroundWorker class - allows you to post the progress in integer type only.
2. AsyncOperationManager class - allows you to post any object into the method that is running in the UI context.

If you need to pass the information to be shown in the UI, AsyncOperationManager is much more flexible.

Below is the sample code on using the AsyncOperationManager class.

private void button3_Click(object sender, EventArgs e)
{
AsyncOperation ao
= AsyncOperationManager.CreateOperation(null);

Thread t = new Thread(new
ParameterizedThreadStart(DoRun2));
t.Start(ao);
}

public void DoRun2(object state)
{
AsyncOperation ao = (AsyncOperation)state;

int j;

for (int i = 0; i < 100; i++)
{
j = tl.id;
ao.Post(this.DoProgress2, i);
System.Threading.Thread.Sleep(90);
}

ao.OperationCompleted();
}
void DoProgress2(object state)
{
// you don't have to call label2.Invoke() method because
// ao.Post() will put the context back to the UI
// context automatically.
this.label2.Text = state.ToString();
}

Tuesday, March 2, 2010

Reflection performance

The following is the reference on reflection performance:

http://msdn.microsoft.com/en-us/magazine/cc163759.aspx
http://www.drdobbs.com/windows/184416570

Basically, to speed up the reflection, you should do this:
1. Cache the object instance that you have created and reuse it later.
2. Using Interface or delegate to invoke the method instead of MethodInfo.Invoke.
3. Create the assembly dynamically using Reflection.Emit.

Monday, January 18, 2010

global.asax - rewriting the URL

This is something interesting that you might want to explore in ASP.NET. The question is how to handle the following URL:

http://localhost/myWebsite/beers

Instead, there are a few ways to achieve it. The easiest way is to use the "urlMappings" in the web.config file. But, this is good for the website that does not update frequently.


  <system.web>

    <urlMappings>
      <add url="~/beers" mappedUrl="~/article_list.aspx?id=1111"/>
    </urlMappings>
  </system.web>



If you have a website that requires to publish the contents that stores in the database, then, you might wonder how to achieve the same result. So, the URL mapping feature can't meet the requirement.

Two ways to achieve this:
(1) you may create a class that inherits from IHttpModule OR
(2) add global.asax to the website and then override BeginRequest method.


Sample code


protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
// get the requested URL.
string s = this.Request.RawUrl.ToString();
// get the current website url including the virtual directory.
string current_website = (String)Application["current_website"];

if (String.IsNullOrEmpty(current_website))
{
current_website = VirtualPathUtility.ToAbsolute("~/");
Application["current_website"] = current_website;
}

// remove the virtual directory. So, only the requested file name will be in the string.
string s2 = s.Replace(current_website, "");

// if the string contains "." (ie, file extension dot)
// or "/" (ie, the folder separator), exit the checking.
if ((s2.Contains("/"))
|| (s2.Contains("."))
)
{
return;
}
else
{
// Now, we need to search for the string in the database that matches the requested url.
// If found the record, we will rewrite the url. The new URL will not reflect in the client browser.
if (s2.CompareTo("beers") == 0)
{
string url = "~/article.aspx?id=1111";
((HttpApplication)sender).Context.RewritePath(url);
}
}
}
catch (Exception ex)
{
// exception handler goes here.

}
}


Wednesday, November 4, 2009

How to find out the current path/virtual directory of a website?

Most of the time, when we are setting the image url, we will use "~" symbol before the image file name. I was wondering what is the meaing of "~" symbol and how it works? How does ASP.NET knows whether the website is running from the root of IIS or from a Virtual Directory? Then, translate this symbol into a correct URL?

For example, setting the image button image url to "~/images/ok.jpg". We will have 2 situations:

1. The website is running from the IIS root. This means, you will have

http://www.testing.com/images/ok.jpg

2. The website is running from a virtual directory in IIS. You will have

http://www.testing.com/news/images/ok.jpg

Programmatically, we have to use System.Web.VirtualPathUtility class to determine the URL of the image.

"~/images/ok.jpg"
  • VirtualPathUtility.IsAbsolute() will returns false. Meaning, once the URL contains the "~" symbol, it becomes relative. So, VirtualPathUtility.IsAppRelative() will return true.
  • For ASP.NET to translate this URL into a runtime URL, we have to call VirtualPathUtility.ToAbsolute(). The result will be "/images/ok.jpg" and "/news/images/ok.jpg" for the above mentioned situation respectively.
  • To compute the physical path, you have to call AppDomain.CurrentDomain.BaseDirectory + relative path (you have to remove the "~" symbol and replace "/" with "\").