July 5, 2011

WaitHandles’ Switch

Filed under: .net,multi-threading Himanshu @ 3:57 pm

I will say it again: “I like extension methods”. They provide great way of increase readability of the code in languages like C#.

Anyway this post is not about extension method. Have you ever needed to write code that will wait on different wait handles (WaitHandle) and execute different piece of code depending up on which handle is signaled. Here is one way to implement that:

public static void WaitSwitch(this WaitHandle[] waitHandles, params Action[] actions)
  
{
  
    waitHandles.DoEmptyOrNullArgCheck("waitHandles");
  
    actions.DoEmptyOrNullArgCheck("waitHandles");
  
    if (waitHandles.Length != actions.Length)
  
        throw new ArgumentException("length of wait handles and actions has to be same");
  
    var triggerIndex = WaitHandle.WaitAny(waitHandles);
  
    actions[triggerIndex]();
  
}
  

And then client code will become as:

(new[] { shutdownTriggeredEvent, updateAvailableEvent }).WaitSwitch(
  
    () => isShutdownRequested = true,
  
    DownloadNextUpdate
  
);
  

July 1, 2011

HTTP 404 while asp.net web forms and spring.net project

Filed under: asp.net,spring.net Himanshu @ 3:32 pm

While doing some refactoring with the project, I suddenly start getting HTTP 404, and that to for all pages that exists and was working few minutes back. I was amused to get the error, with the fact that pages existed, I could see them and feel them! After a while, I noticed that I have deleted a existing .aspx file from project which I forgot to remove from spring.net configuration file.

If you add any .aspx in Spring.Net config that do not exists, Spring.NET brings you to yellow screen with appropriate message. But not when you delete one from project and forget to remove from config file! At least not all the time.

June 22, 2011

Scoping the search result in Firefox

Filed under: internet Himanshu @ 3:31 pm

Do you know, in Firefox 5 it’s possible to scope the search results. For example if you suffix or prefix search text with *, results will be only from bookmarks! That’s helpful, isn’t it. And there are more such special characters that can be used to scope the search result.  More over that, combination of multiple such character is possible as well, that’s more helpful! See details here.

June 2, 2011

JavaScript, and var

Filed under: javascript — Tags: , Himanshu @ 2:16 pm

While working in my current project, I re-learned this hard way.  And I don’t want to make same mistake again, hence noting it here. Many a times you remember something better when noted somewhere.

I had created a javascript that was similar as below:

 1: function Type1(){
 2:     _type = "type1";
 3:     this.show = function() { alert(_type); }
 4: }
 5: function Type2(){
 6:     _type = "type2";
 7:     this.show = function() { alert(_type); }
 8: }
 9: o1 = new Type1();
 10: o2 = new Type2();
 11: o1.show();
 12: o2.show();

And me being ignorant about what I have written, was expecting to see two alerts once with “type1” and another with “type2”.

Case that I had was more complex , hence I took more time to understand the problem, and then note that I haven’t have “var”! The code should be as:

 1: function Type1(){
 2:     var _type = "type1";
 3:     this.show = function() { alert(_type); }
 4: }
 5: function Type2(){
 6:     var _type = "type2";
 7:     this.show = function() { alert(_type); }
 8: }
 9: o1 = new Type1();
 10: o2 = new Type2();
 11: o1.show();
 12: o2.show();

Note and Remember, “var” defines the scope of variable as local!

Separate domain and Runas

Filed under: networking,windows plateform Himanshu @ 1:45 pm

Have you ever wonder how can you run a specific application having awareness of different domain’s credentials? We have VPN setup between our different offices, and all offices are having complete separate windows domain tree. I’m in Pune office, that is having domain, which has not direct relation to our US office (e.g. it’s not welcomed in networking terms) – and believe me there is a good reason to be it that way.

While being in Pune, I needed to run an application on my machine (that is in Pune domain), with is aware that I also have valid credentials in our US office. And Runas worked well in that case as well. Runas.exe can be found in %WINDIR%\System32, checkout its short help by: %windir%\system32\runas.exe /? on command prompt.

I had used it as %WINDIR%\System32\runas.exe /netonly /user:<my US domain name>\<my username in US domain name> “<path to the application that should run within that credentials>”

January 21, 2011

Upgrading XP by SP3 on Laptop

Filed under: windows plateform Himanshu @ 7:34 pm

I was trying to apply Windows XP SP3 through windows auto updates.  Auto update repeatedly reported that it couldn’t apply SP3 update. After digging further, I found that update was failing because laptop was not on AC supply.

Two points to note:

  1. On laptop, shouldn’t apply windows update while battery powered
  2. Interesting to understand that either xp auto update do not have way to report correct problem and instead just say “Could not apply following updates”, or maybe SP3 installer is not rightly coded for auto update.

January 20, 2011

Thought, Suggestion, Quote,

Filed under: random thoughts Himanshu @ 2:20 pm

Bad news is better than no communication

January 8, 2011

Aggregation by rolling week

Filed under: tsql Himanshu @ 5:37 pm

In my current project, we had interesting problem, customer wants to see few data aggregation by rolling week and not year week. Rolling week mean say today is ‘Saturday’, data should aggregate by all week ending ‘Saturday’, but if it was ‘Tuesday’, data should aggregate by by all week ending ‘Tuesday’.  When I heard this for first time, I thought this going to end up real big and complex procedure. But to my surprise it’s not:

   1:  select customerId, bSaleDate as SaleDate, SUM(Amount) as Amount
   2:  from
   3:  (
   4:      select distinct
   5:          SaleByCustomerAndDate.customerId,
   6:          SaleSevenDayRange.SaleDate as bSaleDate,
   7:          SaleByCustomerAndDate.SaleDate as aSaleDate,
   8:          SaleByCustomerAndDate.Amount as Amount
   9:      from
  10:      (
  11:          select
  12:              customerId,
  13:              SaleDate,
  14:              SUM(Amount) as Amount
  15:          from
  16:              Sale
  17:          group by
  18:              customerId,
  19:              SaleDate
  20:      )
  21:      as SaleByCustomerAndDate inner join
  22:      (
  23:          select
  24:              customerId,
  25:              SaleDate,
  26:              DATEADD(DAY,-6,SaleDate) as LastSevenDay
  27:          from Sale
  28:      ) as SaleSevenDayRange on SaleByCustomerAndDate.SaleDate
  29:          between
  30:              SaleSevenDayRange.LastSevenDay
  31:              and
  32:              SaleSevenDayRange.SaleDate
  33:      and SaleByCustomerAndDate.customerId = SaleSevenDayRange.customerId
  34:  ) as c
  35:  group by
  36:      customerId,
  37:      bSaleDate
  38:  order by customerId, SaleDate

January 5, 2011

ASP.NET Postback on table row click

Filed under: .net,asp.net Himanshu @ 8:34 am

When you need to go back to the server for further processing on clicking of a html element, for example on html table row click, below code may help you provide such functioning in ASP.NET.

Well, actually to allow control to support bubbling of the events, and allow user of the control to specify CommandName and CommandArgument while being used in template controls,  there is some extract effort, essentially will need to implement IButtonControl. Let’s see the code below that I have implemented to provide function of  selecting row when clicked on it.

   1:  [ToolboxData("<{0}:ListViewRowClickCommand  CommandName=\"Select\" runat=\"server\"></{0}:ListViewRowClickCommand>")]
   2:  public class ListViewRowClickCommand : HtmlTableRow, IButtonControl, IPostBackEventHandler
   3:  {
   4:      public bool DisableCommand { get; set; }
   5:      protected override void Render(HtmlTextWriter writer)
   6:      {
   7:          writer.WriteBeginTag("tr");
   8:          if (ID != null)
   9:              writer.WriteAttribute("id", ClientID);
  10:          if (!DesignMode)
  11:          {
  12:              if (!DisableCommand)
  13:                  writer.WriteAttribute("onclick", "javascript:" +
  14:                      Page.ClientScript.GetPostBackEventReference(this, CommandArgument));
  15:          }
  16:          Attributes.Render(writer);
  17:          writer.Write(HtmlTextWriter.TagRightChar);
  18:  
  19:          RenderChildren(writer);
  20:          writer.WriteEndTag("tr");
  21:      }
  22:  
  23:      public bool CausesValidation
  24:      {
  25:          get { return (bool)(ViewState["CausesValidation"] ?? true); }
  26:          set { ViewState["CausesValidation"] = value; }
  27:      }
  28:  
  29:      public string CommandArgument
  30:      {
  31:          get { return (string)ViewState["CommandArgument"]; }
  32:          set { ViewState["CommandArgument"] = value; }
  33:      }
  34:  
  35:      public string CommandName
  36:      {
  37:          get { return (string)ViewState["CommandName"]; }
  38:          set { ViewState["CommandName"] = value; }
  39:      }
  40:  
  41:      public string PostBackUrl
  42:      {
  43:          get { return (string)ViewState["PostBackUrl"]; }
  44:          set { ViewState["PostBackUrl"] = value; }
  45:      }
  46:  
  47:      public string ValidationGroup
  48:      {
  49:          get { return (string)ViewState["ValidationGroup"]; }
  50:          set { ViewState["ValidationGroup"] = value; }
  51:      }
  52:  
  53:      public event EventHandler Click;
  54:      public event CommandEventHandler Command;
  55:  
  56:      public void RaisePostBackEvent(string eventArgument)
  57:      {
  58:          if (CausesValidation)
  59:          {
  60:              if (Page.IsPostBack)
  61:                  Page.Validate();
  62:              if (!Page.IsValid)
  63:                  return;
  64:          }
  65:  
  66:          OnClick(EventArgs.Empty);
  67:          OnCommand(new CommandEventArgs(CommandName, CommandArgument));
  68:      }
  69:  
  70:      protected virtual void OnCommand(CommandEventArgs eventArgs)
  71:      {
  72:          if (Command != null)
  73:              Command(this, eventArgs);
  74:  
  75:          RaiseBubbleEvent(this, eventArgs);
  76:      }
  77:  
  78:      protected virtual void OnClick(EventArgs eventArgs)
  79:      {
  80:          if (Click != null)
  81:              Click(this, eventArgs);
  82:      }
  83:  }

Above control can be used as below:

   1:  ...
   2:  
   3:  <asp:ListView ID="listViewOfSomething" runat="server" OnSelectedIndexChanging="OnSelectedIndexChanging">
   4:      <LayoutTemplate>
   5:          <table id="htmlTableOfSomething" >
   6:              <tbody>
   7:                  <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
   8:              </tbody>
   9:          </table>
  10:      </LayoutTemplate>
  11:      <ItemTemplate>
  12:          <vdd:ListViewRowClickCommand ID="ListViewRowClickCommand" runat="server"
  13:              CommandName="Select" Class="SomeCssClassForRow" >
  14:              <td class="CssClassForColumn">
  15:  
  16:  ...

Feel free to put comments in case if things are not clear.

April 18, 2010

Question your guesses and assumptions

Filed under: random thoughts Himanshu @ 12:19 am

It’s fare to guess or assume, in fact it helps moving forward in at least one direction, and is inevitable many a times. But started believing that a guess or assumption is truth, can be really harmful. Eventually questioning guesses and assumptions is important, and that also helps being open for multiple perspectives.

« Newer PostsOlder Posts »

Powered by WordPress