Question regarding asynchronous programming?

I have a function named "Search" which invokes another method "SearchIndividuals" as shown below. In the "SearchIndividuals" it invokes another method "Send" which is defined as asynchronous and as soon as it is done with processing it returns the result using a "callback". In the "SendCallback" method the "Result" variable will be set.

In the "Search" method it needs to process the "Result". How can I make sure it reads the "Result" variable only after it is set in the "SendCallback" method.

Thanks for any help or suggestions.
//class level variable
private string Result = null;

private void Search(object sender, SearchEventArgs args)
{
    SearchIndividuals(Name);

    if(Result! = null)
    //process result

}

private void SearchIndividuals(string name)
{
    AsyncProcess.Send(name);

}

private void SendCallback(object sender, ResponseXml e)
{
    Result = e.Response

}

Open in new window

ipjyoAsked:
Who is Participating?
 
Meir RivkinFull stack Software EngineerCommented:
u can use while(Result ==null);
ot
put a variable in your class say _CallbackResponseSet which u change to true once SendCallback is done.
something like:

//class level variable
private string Result = null;
bool isDone = false;

private void Search(object sender, SearchEventArgs args)
{
    SearchIndividuals(Name);

    while(isDone == false){
    //process result
}

}

private void SearchIndividuals(string name)
{
    AsyncProcess.Send(name);

}

private void SendCallback(object sender, ResponseXml e)
{
    Result = e.Response
isDone =true;
}
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Is this WinForms or WebForms?

Is it ok to BLOCK execution in Search()?
(this could potentially cause an unresponsive WinForm app depending on where this is executing)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.