Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

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.

Thursday, June 21, 2012

The name 'Request' does not exist in the current context

I receive this message today in Visual Studio when trying to get at the Request object of a web page and it stumped me for a bit, since it did not represent what the actually problem was.  "The name 'Request' does not exist in the current context"


I had this code in the Page_Load event:
     if (Request("myFlag")=="1") DoSomething();


Since I had seen similar code elsewhere and I was in a Page_Load I was pretty sure I had the Request object available to me.


I tried going at the base page.  I tried using "this".  None of them worked.


As I flipped things this way and that I had something pop up that made me realize I had wrapped the parm of the Request object in parenthesis instead of brackets.  Once I changed that it fixed the problem.
    if (Request["myFlag"]=="1") DoSomething();


The error message was misleading.  While a method named Request was not in the current context, the problem actually was that I was using an object like a method.  I will admit that I probably made this mistake because I bounce back and forth between VB and C#.  Perhaps developers that exclusively do C# and Java would not be tripped up by this.

Friday, February 6, 2009

Bool/Bit - C#-TSQL(SQL Server)

When passing a parameter to a SQL Server stored procedure using C# you cannot pass 0 or 1 like you would manually calling an EXEC on he procedure.

I passed a parameter as a zero and it acted like I had passed a 1 or true. I switched it to false and it ran as it was supposed to.

Friday, April 18, 2008

Statements can only be used fo calls, assignments...

This C# error probably means that you have tried to call a function without parethesis after it. For example, typing:
GetTaxes;
Instead of:
GetTaxes();

Wednesday, October 31, 2007

Switch Case C#

For some reason I find myself having to look this up every time I go to use it.

Switch is like Select in VB. The structure of it is as follows.

switch (variable)
{
case value1:
statements;
break;
case value2:
statements;
break;
case value3:
statements;
break;
default:
statements;
break;
}
  • Switch is followed by a variable in parenthesis. This the variable that you are checking the value of.
  • Each value that you want to check the variable against is in a case vale block.
    • Must have a colon after the value.
    • Each statement int he block must be terminated with a semicolon.
    • Each block must have a break at the end of it. C# does not allow falling through to one case to the other, but you can do several empty cases in a row that all fall through to one code block. The break tells it not to do this. For example:
      case 1:
      case 2:
      case 3:
      DoSomething();
      break;
      In this example if the variable is 1, 2, or 3 it runs do something.
  • There can be a default case that executes if the variable does not match any of the cases.
  • It appears as though one can include a goto statement in their case block also. For example "goto case 17" jumps you to case one. You could execute code in case 45 and then jump to case 17 and run its code as well, even though the variable is not equal to 17.
  • All of the case blocks are surrounded by {}. The opening { is after switch (variable). The closing } is after the last case block of code.

Wednesday, July 11, 2007

Cannot implicitly convert type 'string' to 'method group'

I run into this now and again in C#: Cannot implicitly convert type 'string' to 'method group'

This usually means that you have not included the parenthesis at the end of a method call. For example, ToString(). Many times I will do:

if (str == var.ToString)

or

str = var.ToString;

It throws the about error because I needed to do:
if (str == var.ToString())

or

str = var.ToString();