Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why a string comes back as null
Message
From
03/12/2008 10:44:49
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Miscellaneous
Thread ID:
01365266
Message ID:
01365380
Views:
10
Hi Paul,

>>Hi All,
>>
>>I have a method that returns a string if there is no file in a directory. I have stepped through this and watched this line get set to an empty string but when it comes back to the calling line of code, it is null. I am missing something and wonder if it is the way I did the while statement. Any ideas appreciated.
>>Thanks for the help
>>Tim
>>
>>When the return from PopQueue is set to "" then in my while statement the value of fileName is null. Why would this be?
>>
>>while ((fileName = this.PopQueue(path, filter)) != "" && llSuccess)
>>{
>>    // Do all my stuff here, process each file returned until no more files are returned.
>>}
>
>>
>
>What's fileName set to initially?
>
>Actually, my initial thought when I saw this code was, "that actually works?!" I tried out a simpler version of it, and it seems to work OK (I removed the code which accesses the filesystem - it just appends a character to a test string. Still, it might be because of the evaluation order of things. You could try moving the fileName assignment above the while (then duplicating it inside of the while after your // Do all my stuff code has run) to see if that's the case.

I think I let myself get trapped to quickly jumping to a conclusion. I had a breakpoint on the while clause and it kept breaking there after I saw no files were in the directoy. So I stepped into the PopQueue() method and saw it was indeed being set to an empty string but when it came back it was showing as null. So I assumed my problem was this. Now to check based on the comments of others, I set more breakpoints before the loop, after the loop and in the middle to verify the what the value was inside the loop and before I find the loop is actually exiting properly but the method was being called constant. It is called from a threading timer. Here is what I found with that.
Before the while loop I am suspending the timer and then after the loop I restart it.
// Before the loop suspend the timer with:
if (tmrQChecker != null)
     this.SetTimer(tmrQChecker, Timeout.Infinite, this.QCheckInterval, null);

Then I re-start the timer with this:
this.SetTimer(tmrQChecker, this.QRetryInterval, this.QCheckInterval, null);

private void SetTimer(System.Threading.Timer timer, int delaySeconds, int intervalSeconds, TimerCallback callback)
{
     if (timer == null)
     {
          using (AutoResetEvent autoEvent = new AutoResetEvent(true))
          {
               TimeSpan delayTime = new TimeSpan(0, 0, delaySeconds);
               TimeSpan timerInterval = new TimeSpan(0, 0, intervalSeconds);

               if (callback == null)
                    callback = new TimerCallback(this.OnBlankTimerEvent);

               timer = new System.Threading.Timer(callback, autoEvent,
               delayTime, timerInterval);
          }
     }
     else // Reset Timer
     {
          // I did not have the *1000 on these values and they were 90 and 120 respectively.  These values are fine above 
          // since it was being used in a TimeSpan.  Effectively my timer was firing every fraction of a second.

          timer.Change(delaySeconds * 1000, intervalSeconds * 1000);
     }
}
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform