mscommunity.net

Interactive mscommunity.net online activities
Signed in as anonymous | Edit Profile | Sign out | Help
in Search

Weblog :: Boris Ševo

Sporadic posts about my interests, e.g. software development (mostly .NET), technology in general and some occasional rant.

veljača 2008 - Posts

  • Concurrent programming in C# - Parallel.Do method

    This is my first one but certainly not the last one post about the concurrent programming in C#. Free lunch is over
    is an excellent text which shows why concurrent programming matters. If you ever read anything about it, you
    have probably noticed that people talk about it as something hard and complicated. Is it really so hard and
    complicated? Well, in my opinion it depends. It depends about how much concurrent your program need to be,
    what kind of knowledge do you have about this programming model, which libraries are you using for threading etc.
    I have some knowledge about this topic, but it's very far from being profound. Because of that I'm happy that
    there are some smart people in Parallel Computing Developer Center who made an excellent extension
    for .NET - ParallelFX.

    There is a lot in it and there's a lot interesting topics to write about, but because this is my first post, I will start
    with something simple and still very powerful. I will write only about Do method. According to documentation,
    it's a method with following declaration:

    public static void Do(
    params Action[] actions
    )

    This method "executes each of the provided actions inside a discrete, asynchronous task". It doesn't
    finishes until each of provided actions has completed. That means, it will finish when the slowest action finishes. Imagine
    that you have two long running methods called foo and bar. If you call them in sequential manner you will need to
    wait until foo finishes its execution, then you will run bar method and wait until it finishes its execution. If you run
    them in parallel you will only need to wait as much as the longest of this two is executing. Let's see an example.

        class Program
    {
    private static double x = 1000000000;

    private static void foo()
    { for (double i = 0; i < x; i++) ;}

    private static void bar()
    { for (double i = 0; i < x; i++) ;}


    static void Main(string[] args)
    {
    Stopwatch watch = new Stopwatch();

    watch.Start();
    // *** sequential
    foo();
    bar();
    watch.Stop();

    Console.WriteLine("Not parallel: " +
    (watch.ElapsedMilliseconds / 1000.0).ToString() +
    "s");

    watch.Reset();
    watch.Start();
    // *** parallel
    Parallel.Do(() => foo(), () => bar());
    watch.Stop();

    Console.WriteLine("Parallel: " +
    (watch.ElapsedMilliseconds / 1000.0).ToString() +
    "s");
    }
    }

    The time which you get running the above program will depend about your computer CPU. On my computer
    (Core Duo, 2.33GHz) I get:
    Not parallel: 8,769s
    Parallel: 4,595s
    So, with a help of just one line of code we have a multithreaded program!

    That's all for the first time. It was just a little introduction. When I learn more about ParallelFX I will write again.

  • Inline text editing control in ASP.NET AJAX

    Inline text editingInline text editing is a technique which I really like. It can be used on various places, displaying updatable widget title or displaying updateable user status are just two of them. Still, it can be very confusing to the users if they aren't enough familiar with the application they are using. To make it more user friendly, I added few features (dynamic CSS changing, mouse cursor changing and tooltip) which purpose is to suggest the users that there is something more behind the plain text which is initially shown to them.
    Control is implemented as ScriptControl and all the logic behind it is made with JavaScript using ASP.NET AJAX framework (full source code and a test web page are provided at the end - you will need VS2008 to build it). It doesn't extend any ASP.NET controls ("TextBox" and "Label" are generated on the fly) and it allows you to get and store updated text on the persistent medium (database for example) using .asmx web services.
    In source code you can see how to:

    • build an ASP:NET AJAX control with embedded .js file
    • use ASP.NET AJAX DOM extensions
    • dynamically invoke web services to asynchronously communicate with the server
    • process various DOM events like keypress, mouseover, mouseout, click and blur

    To use this control you only need to add one line of code in your .aspx page.

    <inlEdit:InlineEditing ID="InlineEditing1" runat="server" TextId="100" 
    ServicePath="WebService.asmx" GetMethod="Load"
    SaveMethod="Save" CssClass="lblClass"
    LabelHoverCssClass="lblHoverClass"
    TextBoxCssClass="txtbxCssClass"/>

    This is just a first version. Potential improvements are:

    • add a button next to "TextBox" do the same what pressing enter key does
    • show progress indication when retrieving and updating text
    • add MaxLength property (handle pasting and various keys)
    • any other improvement

    Source code (tested in IE7, FF2.0.0.12 and Opera 9.26.)

  • Using enter in TextBox control to submit the form with multiple Button controls

    Hitting enter after you have typed something in some TextBox control is something natural to most users, but it
    makes life of the ASP.NET developer more difficult. If you don't know about what I'm talking here is a simple
    example. Imagine that you have a simple web form with two text fields and two button controls. The first text field
    and the first button are used for searching the web site and they are placed somewhere near the top of the page.
    The second text field and the second button are used for some totally different purpose and that's filtering the
    entries displayed on the form.
    The important thing to remember is that both buttons, when clicked, are causing postback and that first button is
    rendered before the second button. So, when enter is pressed inside the first text field, web site will be searched.
    So far so good. But let's suppose that user has typed something in the second text field. If enter is pressed while
    mouse cursor is inside the second text field, user will expect that the form entries will be filtered. But that isn't going
    to happen! The first button will do the submit and web site will be searched once again.

    Described behavior I found in IE7 and Opera. Firefox is immune to this problem. To understand why is this happening
    we need to look at the code. Here is the code of the simple web form which will demonstrate the problem:

        <head runat="server">
    ...
    <script language="javascript" type="text/javascript">
    function Submit(id, e) {
    var isEnter = window.event == null ?
    e.keyCode == 13 :
    window.event.keyCode == 13;
    if(isEnter)
    document.getElementById(id).click();
    }
    </script>
    <script runat="server">
    protected void btnSearch_Click(object sender, EventArgs e)
    { lit1.Text = "I'm searching"; }

    protected void filterBtn_Click(object sender, EventArgs e)
    { lit1.Text = "I'm filtering"; }
    </script>
    </head>


    <form id="form1" runat="server">
    <asp:Literal ID="lit1" runat="server"></asp:Literal>
    <br />
    <asp:TextBox ID="TextBox1" runat="server" onkeypress="Submit('btnSearch', event)" />
    <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" Text="Search" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" onkeypress="Submit('filterBtn', event)" />
    <asp:Button ID="filterBtn" runat="server" onclick="filterBtn_Click" Text="Filter" />
    </form>

    The interesting part isn't the web form source code then the generated html. Here it is only the important part:

        <input name="TextBox1" type="text" id="TextBox1" 
                  onkeypress="Submit('btnSearch', event)" />
    <input type="submit" name="btnSearch" value="Search" id="btnSearch" />
    <br />
    <input name="TextBox2" type="text" id="TextBox2" onkeypress="Submit('filterBtn', event)" />
    <input type="submit" name="filterBtn" value="Filter" id="filterBtn" />

    Both buttons are generated as submit buttons and that's what is causing the problems. When you press enter while you
    are typing inside the second text field (TextBox2) default button will do the submit. Default button, if explicitly not
    specified, will be the first rendered button (in this case this is the btnSearch). You can change the default button by
    setting the form's defaultbutton attribute to id of some other button, but this won't fix the problem. The solution is to say to
    ASP.NET that is shouldn't render buttons which type is submit. Instead it should render regular buttons. We can do that
    with the help of UseSubmitBehavior property. All we need to do is to set it to false. The markup will now look like this:

        ...
    <form id="form1" runat="server">
    ...
    <asp:TextBox ID="TextBox1" runat="server" onkeypress="Submit('btnSearch', event)" />
    <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click"
    Text="Search" UseSubmitBehavior="false" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" onkeypress="Submit('filterBtn', event)" />
    <asp:Button ID="filterBtn" runat="server" onclick="filterBtn_Click"
    Text="Filter" UseSubmitBehavior="false" />
    </form>

    Generated HTML now looks like this:

        ...
    <input type="button" name="btnSearch" value="Search" id="btnSearch"
    onclick="java_script:__doPostBack('btnSearch','')"/>
    ...
    <input type="button" name="filterBtn" value="Filter" id="filterBtn"
    onclick="java_script:__doPostBack('filterBtn','')"/>

    That's it. When you press enter inside TextBox1 btnSearch will do the submit, and if you press enter inside the TextBox2
    btnSearch will do the submit.

    Posted vlj 13 2008, 08:52 by boris.sevo with 6 comment(s)
    Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems