B
beginwithl
hi
1)
static void Main(string[] args)
{
Delegat delegat = new Delegat( new Program().Async );
IAsyncResult asyncR = delegat.BeginInvoke(null, null);
delegat = new Program().Async1;
IAsyncResult asyncR1 = delegat.BeginInvoke(null, null);
delegat.EndInvoke(asyncR); // exception
}
public void Async()
{
Thread.Sleep(16000);
}
public void Async1()
{
Thread.Sleep(16000);
}
In the above code we invoke delegate “delegat” two times, and thus two
new threads T1 and T2 are created to handle asynchronously invoked
methods. But then a call to EndInvoke() causes an exception to be
raised ( due to passing asyncR instead of asyncR1 as an argument ).
* What is the reason for the decision that EndInvoke() should only be
able to deal with one asynchronously invoked method at the time?
* Why couldn’t object ( of type AsyncResult ) passed as an argument to
EndInvoke() somehow inform it whether it should deal with method in T1
or in T2?
2) “EndInvoke supplies all the output from the asynchronous method
call, including 'ref' and 'out' parameters. If a delegate’s referenced
method has 'ref' or 'out' parameters, they must be
included in EndInvoke’s parameter list before the reference to the
IAsyncResult object,”
Why must we also include 'ref' and 'out' parameters in EndInvoke’s
parameter list?
3) Book claims EndInvoke() also does other things ( disposes thread’s
resources etc ) besides returning the value returned by asynchronously
invoked method, and thus we should always call it even if we are not
interested in a return value. But other sources claim that calling
EndInvoke() is not a must. So which is true?
thank you
1)
static void Main(string[] args)
{
Delegat delegat = new Delegat( new Program().Async );
IAsyncResult asyncR = delegat.BeginInvoke(null, null);
delegat = new Program().Async1;
IAsyncResult asyncR1 = delegat.BeginInvoke(null, null);
delegat.EndInvoke(asyncR); // exception
}
public void Async()
{
Thread.Sleep(16000);
}
public void Async1()
{
Thread.Sleep(16000);
}
In the above code we invoke delegate “delegat” two times, and thus two
new threads T1 and T2 are created to handle asynchronously invoked
methods. But then a call to EndInvoke() causes an exception to be
raised ( due to passing asyncR instead of asyncR1 as an argument ).
* What is the reason for the decision that EndInvoke() should only be
able to deal with one asynchronously invoked method at the time?
* Why couldn’t object ( of type AsyncResult ) passed as an argument to
EndInvoke() somehow inform it whether it should deal with method in T1
or in T2?
2) “EndInvoke supplies all the output from the asynchronous method
call, including 'ref' and 'out' parameters. If a delegate’s referenced
method has 'ref' or 'out' parameters, they must be
included in EndInvoke’s parameter list before the reference to the
IAsyncResult object,”
Why must we also include 'ref' and 'out' parameters in EndInvoke’s
parameter list?
3) Book claims EndInvoke() also does other things ( disposes thread’s
resources etc ) besides returning the value returned by asynchronously
invoked method, and thus we should always call it even if we are not
interested in a return value. But other sources claim that calling
EndInvoke() is not a must. So which is true?
thank you