Wednesday, September 9, 2015

C# Equivalent of DoEvents

I have been looking for a long time for an equivalent to Visual Basic's DoEvents statement.

DoEvents allows your code to yield execution to other threads in order to share processing time.  This could be do in a loop to allow you to process an iteration of the loop and when done yield.  This is often done to attempt to keep the UI responsive.  After each iteration you would call DoEvents thus allowing the UI some processing time.

I was studying today about multi threading and asynchronous processing when I came across some code that did a loop and called Thread.Sleep(0) at the end of each iteration.  Supposedly this yields execution without causing the loop to execute longer, since the sleep is for zero milliseconds.  Since the thread goes to sleep, other threads can then take over and release control when it is done.

I think you can use DoEvents in C# by using the the Visual Basic library, but from what I have seen people saying that is not recommended.  This is why I have been looking for a native C# method of doing this.

Visual Basic
        For Each file In files
            DoFileStuff()
            Application.DoEvents()
        Next

This is useful if DoFileStuff() updates the UI. DoEvents() would then let go of control, so the UI could take a turn and refresh itself.

C#
        foreach (var file in files)
        {
            DoFileStuff();
            Thread.Sleep(0);
}
The thread goes to sleep and immediately wakes up ready to be scheduled for execution when it is its turn again.

Saturday, January 24, 2015

HttpContext.Current Null in Asynchronous Methods

I created a page that had some logic to dynamically create the page.  It was doing a lot of fetches from the database and then calculating how to display each batch of data that was retrieved.  The page became slow.

To speed it up I converted the most time intensive loop to a parallel for each.  Inside the loop it was using HttpContext.Current.  Once I made the loop Parallel HttpContext.Current was null inside things called inside the loop..

It appears that if you leave the main thread of execution it no longer has a reference to the current context.

I found that HttpContext.Current can be set as well as read.  Outside the parallel loop I captured HttpContext.Current in a local variable.  I then accessed the variable in the loop.  The other things I was calling inside the parallel for each needed HttpContext.Current, so I set HttpContext.Current to the local variable.  The other methods were able to read HttpContext.Current as long as they were on the same thread as the calling method.

Tuesday, November 25, 2014

Making a DIV Dynamically Adjust to the Size of the Content and Not Fill the Entire Parent

I had created an HTML table with three cells (td). The first cell was 20% of the whole the second 60% and the third 20%. In the second cell I place a div. I needed the div to not fill the entire cell, but stretch or shrink based on how much content is in it. The following code is what I started with. The div would fill the cell, even if empty.

table>
 tr>
 td style="width: 20%">

/td>
td style="width: 60%;text-align:center;">
    div id="divMiddleCell">

            Dynamic Content

       /div>

/td>
/tr>
/table>

I altered it as follows to add display: inline-block tot he style property of the div and it made the div dynamically shrink and grow.


table>
tr>
 td style="width: 20%">

 /td>
 td style="width: 60%;text-align:center;display: inline-block">
    div id="divMiddleCell" ">

       Dynamic Content

       /div>

/td>
/tr>
/table>



Thursday, March 13, 2014

How to do Select Top in Oracle

In SQL Server I often do a SELECT TOP 10 FROM [TableName].

To get the bottom ten I found that I could ORDER BY DESC.

In Oracle things are a bit different. In Oracle they have a ROWNUM system field. To get the first ten records you just have to get the ROWNUMs that are 10 and below like the following:

SELECT * FROM [TableName] WHERE ROWNUM >= 11

Control 'gv' of type 'GridView' must be placed inside a form tag with runat=server.



I have gotten this error several times:

Control 'gv' of type 'GridView' must be placed inside a form tag with runat=server.

Each time I forget the solution and after I fix it I eventually forget why I was getting it.  This time is not different.  I can't remember why it started happening, but the solution is to create a method like this.

public override void VerifyRenderingInServerForm(Control control)

{
 
/* Verifies that the control is rendered */
}

Sometimes & nbsp; Doesn't Work and I Have to Use & #160; Instead

I found recently that sometimes when I use an HTML & nbsp; it literally prints & nbsp; instead of a nonbreaking space.

Blogger is having trouble taking what I want to say, so I have to put a space between the ampersand and the nbsp; characters to get it to print correctly.

One instance that gave me trouble with the nbsp was between a td tag, even though I can include other HTML markup in between the opening and closing tag.

After some head scratching and reading I tried using a & #160; and that worked.

I guess HTML has two different ways of expressing non breaking spaces.

Thursday, February 6, 2014

ASP.Net Web Forms Application Can't Find Namespace

I was creating a test Web Forms application and added some existing code files.

I first added them to the app_code folder, but in there they could not find the OracleDataClient namespace.  I found something that said to move the code file out of app_code and that fixed the Oracle problem.

Then when I would reference classes in those files the page code did not recognize the namespace.

After a while of searching I found someone who said to right click on the files and view properties.

The property they said to look at was Build Action.  My file was set to Content and I saw to change it to Compile.  That fixed it.