async delegate and exception

J

Jeff

Hey

..NET 2.0

In my app I'm about to program some code which retrieve a XML file from
somwhere on the Internet. and then parses the XML - just looping through the
records in the XML document and collecting a value at each record....

This task will run each day to collect this data...

Because parsing this XML document can take a while, so I'll set a delegate
to do the processing in the thread pool....

PROBLEM
If no XML file exist then the delegate should throw an exception and stop
executing... my code start a delegate with BeginInvoke but how should I
catch this exception in the program..?? Should I just implement a try/catch
block in the ExecuteExample method and if no file XML file exists when
TestMethod executes then throw an exception, will then this exception be
caught in the process started the delegate thread? This confuses me a bit

Here is some example code describing my problem:
delegate decimal TestDelegate();
private decimal TestMethod()
{
//Do XML processing here
}

public decimal ExecuteExample()
{
IAsyncResult aResult = null
TestDelegate d = new TestDelegate(TestMethod)

aResult = d.BeginInvoke(null, null);

if (!aResult.IsCompleted)
aResult.AsyncWaitHandle.WaitOne();

decimal value = d.EndInvoke(aResult);

}

any suggestions?
 
J

Jon Skeet [C# MVP]

Jeff said:
.NET 2.0

In my app I'm about to program some code which retrieve a XML file from
somwhere on the Internet. and then parses the XML - just looping through the
records in the XML document and collecting a value at each record....

This task will run each day to collect this data...

Because parsing this XML document can take a while, so I'll set a delegate
to do the processing in the thread pool....

Unfortunately, your code isn't clear - ExecuteExample basically starts
the operation on the thread pool and immediately waits for it to
complete, which defeats the point of using a separate thread.

Basically the call to EndInvoke will throw the exception - how you
handle it is up to you.
 
D

DeveloperX

Hey

.NET 2.0

In my app I'm about to program some code which retrieve a XML file from
somwhere on the Internet. and then parses the XML - just looping through the
records in the XML document and collecting a value at each record....

This task will run each day to collect this data...

Because parsing this XML document can take a while, so I'll set a delegate
to do the processing in the thread pool....

PROBLEM
If no XML file exist then the delegate should throw an exception and stop
executing... my code start a delegate with BeginInvoke but how should I
catch this exception in the program..?? Should I just implement a try/catch
block in the ExecuteExample method and if no file XML file exists when
TestMethod executes then throw an exception, will then this exception be
caught in the process started the delegate thread? This confuses me a bit

Here is some example code describing my problem:
delegate decimal TestDelegate();
private decimal TestMethod()
{
    //Do XML processing here

}

public decimal ExecuteExample()
{
    IAsyncResult aResult = null
    TestDelegate d = new TestDelegate(TestMethod)

    aResult = d.BeginInvoke(null, null);

    if (!aResult.IsCompleted)
        aResult.AsyncWaitHandle.WaitOne();

    decimal value = d.EndInvoke(aResult);

}

any suggestions?

Have the EndInvoke return a struct or class perhaps. You can then
handle the exception on the retrieving thread and hand back a nice
neat result.

Something like

public struct Result
{
public double Value;
public bool Completed;
}

perhaps?
 
W

William Stacey

It depends on what your doing and if you really need to block. In many
cases, you can just handle the result logic inside the delegate and ~never
block. Such as:

private void DoWork()
{
// Other stuff

// Do work on another thread.
ThreadPool.QueueUserWorkItem(
delegate
{
Exception ex = null;
string result = null;
try
{
Console.WriteLine("Do xml.");
result = "<xml>";
}
catch (Exception ex2)
{
ex = ex2;
}
finally
{
// Handle results to UI or other.
if (ex!=null)
Console.WriteLine(ex.Message);
else
Console.WriteLine(result);
}
});
}

--W
 
J

Jeff

thanks for replying to my post

Basically what I want to do is to execute this delegate in the thread pool
so it won't block executing on the main thread. If for example the server
having the XML file is down when my code trying to access it then I thought
executing it in the tread pool was the way doing it.

I modified my code to use a WHILE-statment instead of the IF-statement
while (!aResult.IsCompleted)
aResult.AsyncWaitHandle.WaitOne();
But I think my code above is missing something. Instead of setting the main
executing thread to wait it should (AFIAK) be allowed to go and do some
other processing while this delegate is being processed., how do I specify
the main executing thread to go and do something else while waiting for this
delegate thread to finish? (if that is possible)

Was it that you had in mind when mentioning that calling EndInvoke
immediatley after the thread is started defeats the point in using the
thread pool ??

any suggestions?
 
J

Jon Skeet [C# MVP]

Jeff said:
thanks for replying to my post

Basically what I want to do is to execute this delegate in the thread pool
so it won't block executing on the main thread. If for example the server
having the XML file is down when my code trying to access it then I thought
executing it in the tread pool was the way doing it.

Yes - but *not* executing in the thread pool and immediately waiting.
I modified my code to use a WHILE-statment instead of the IF-statement
while (!aResult.IsCompleted)
aResult.AsyncWaitHandle.WaitOne();

That's going to be just as bad.
But I think my code above is missing something. Instead of setting the main
executing thread to wait it should (AFIAK) be allowed to go and do some
other processing while this delegate is being processed., how do I specify
the main executing thread to go and do something else while waiting for this
delegate thread to finish? (if that is possible)

Well, what do you want it to do? How much more work is there that the
main thread can do before the other thread completes? Think about what
you actually want to do in parallel.
 
T

taiwoa

Basically what I want to do is to execute this delegate in the thread
pool
so it won't block executing on the main thread.
<<<

Use the BackgroundWorker component to do this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top