c# - Return value from async Method -
I have asmx webservice and I am using it in PCL, so we all know that now PCL is now using asmx webservice Does not support, it supports WCF webservices; I somehow manage to call the asmx webservice via PCL using the Silverlight plugin. But it creates async method and converts WSDL to wcf.
The problem now is that I have to return some values from webmoots which are now async method if someone finds its solution then please help me.
I am using the following code,
public class PerformLogIn {string temp; Public string checktip (string code) {ServiceReference1.WeatherSoapClient obj = new ServiceReference1.WeatherSoapClient (New BasicHttpBinding (), New Endpoint Address ("http://wsf.cdyne.com/WeatherWS/Weather.asmx")); Obj.GetCityForecastByZIPCompleted + = getResult; Obj.GetCityForecastByZIPAsync (code); Return temporary; } Zero getResult (object sender, GetCityForecastByZIPCompletedEventArgs e) {string error = zero; If (E. error! = Null) error = E. Error. message; Else If (E. Canceled) Error = "Canceled"; Var results = E. result; Temporary = result.temprature; }
}
}
So when I run it, the value of temp
is zero I when I debug it, I found out that When gets getResult
, it enters the zero getResult
, by that time it has reached the return
statement.
The problem is, when the GetCityForecastByZIPAsync
method calls async
, does not wait for the operation of others to complete it, it only It eliminates its execution and returns are now applied to the event event when a webservice
is getResult
(in this case), event getResult
(in this case) is based on the network, the server response which will be a bit delayed which we can not controlThen the GetCityForecastByZIPAsync
method completes its execution. And after some time when getResult
occurs, it executes, but the calling method does not return ie i. checktump
method
then I < How to get data from the code> getResult event and put it back in the checkTemp
method so that I can use it for my purpose.
If someone knows, please help me.
FYI: Your entire whole article question is too much for your audience, and do not seem to be related to the question (Or at least, if you explain the actual details more widely, then not relevant).
As far as the question goes and hellip;
The two most obvious problems with your code is that a) you start your asynchronous operation after your getResult
event handler , which means That you can complete the operation before being notified about completion, and b) your checkTemp
method does not wait until the completion of the result.
The first issue is easy to fix. Reverse the order of just two statements, so that they can look like this:
obj.GetCityForecastByZIP contains ++ getResult; Obj.GetCityForecastByZIPAsync (code);
Fixing a second part is hard to comment on, mainly because you have not provided enough context in your question. If the GetCityForecastByZIPAsync
method is actually a C # async
method, then the best way is to get rid of the getResult
method altogether To subscribe) it is done by ... completed
event), and waiting
calls in GetCityForecastByZIPAsync
. Otherwise, the best approach is to not actually return the value back to checkTemp
, instead of zero
for your method and your getResult
method Requires completion of the operation at the completion of the operation (for example by placing the code displaying results in the UI).
Without further reference, it is impossible to say Get the information about how to post better questions.
Comments
Post a Comment