1

I am not a used C# programmer, so if this is stupid, sorry :-)

I am developing a C# app for Windows 8. I need to do a web request, and therefore, I have a callback to handle the request data.

I am using the standard approach:

private static ManualResetEvent allDone = new ManualResetEvent(false);

private static void daCallback(IAsyncResult data) {
   ...
   SampleDataSource.allDone.Set();
}

// This is the Class CONSTRUCTOR
public SampleDataSource() {
   # before anything, reset allDone:

  string request = "http://some.url.com";
  HttpWebRequest webRequest = WebRequest.Create(request) as HttpWebRequest;
  webRequest.Method = "POST";

  webRequest.BeginGetResponse(new AsyncCallback(daCallback), webRequest);

  Debug.WriteLine("Asked to begin get response");

  SampleDataSource.allDone.WaitOne(12000);

  Debug.WriteLine("Done Waiting");
   #...
}

When running, allDone (inside do_something) is not initialized. I even added a condition around to compare allDone with null, and yep, it is null.

The "Done Waiting" message never gets print at all...

What am I doing wrong?

Thank you

Alberto
  • 499
  • 4
  • 23
  • 1
    Why do you need a reset event at all? (And are you sure some other code somewhere isn't setting it to null? From the code you've posted here it shouldn't be...) – Cameron Nov 20 '12 at 19:43
  • what are you supposed to be doing in the first place – Sam I am says Reinstate Monica Nov 20 '12 at 19:43
  • @Cameron, it doesn't matter if I am calling Reset, or set, or WaitOne. It is not defined. Why? – Alberto Nov 20 '12 at 19:44
  • @ambs perhaps you have some other variable/property etc. named allDone ? – Yahia Nov 20 '12 at 19:45
  • Only these references to allDone is this namespace/class. – Alberto Nov 20 '12 at 19:45
  • @ambs then just reference it via `YourClassName.allDone` and see what happens... – Yahia Nov 20 '12 at 19:46
  • @Yahia, tried it. I still get complains about NullReferenceException. Something weird here :-) – Alberto Nov 20 '12 at 19:53
  • @ambs that contradicts what you showed as code... it is either a weird compiler/runtime issue OR some other (beside what you show) does something to produce this behaviour... I bet on the latter... – Yahia Nov 20 '12 at 19:55
  • @Yahia, added some more code in the post, hopefully that can show some stupid error I am doing :-) – Alberto Nov 20 '12 at 20:00
  • Can it be because I am doing this in a constructor? :-| – Alberto Nov 20 '12 at 20:06
  • Fond out that WaitOne should receive a number of milliseconds for Windows 8 Store Apps... but still no fun. – Alberto Nov 20 '12 at 20:50
  • Can't understand why, but initializing allDone in the method works (now that WaitOne has a timeout). But still, can't understand why it doesn't work as above. – Alberto Nov 20 '12 at 20:54

2 Answers2

2

You're calling a static field from an instance method. Use YourClassName.allDone.Reset() instead.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
2

You have to add the classname in front of your call to allDone, because it's static. So instead of

allDone.Reset();

try

//replace CLASS with whatever you named your class
CLASS.allDone.Reset();

Equivalent approach to all other calls.

KeyNone
  • 8,745
  • 4
  • 34
  • 51
  • That would explain it all. But it seems it continues to be with a null value. – Alberto Nov 20 '12 at 19:48
  • @ambs Did you try setting a breakpoint at `private static ManualResetEvent allDone = new ManualResetEvent(false);`? This way you can be sure it really gets called or not (and that allDone isn't null afterwards). – KeyNone Nov 21 '12 at 07:40