- Home /
suspend a function time to recover a data C#
I need to recover a number of furniture (by querying a MySQL database) and then use that number to run other querys on a second database. My problem now is that the first query retrieves the number of good furniture but the function continues to run while this number has not yet been returned by the php script. Suddenly, the "for" loop does not run. (n_meuble is still at '0' when 'for' try to loop) So I need help to suspend the "gal_of_library" function until the IEnumerator "get_num_meuble" returns the value of "n_meuble".
private int n_meuble;
private ArrayList gal_tab;
public ArrayList gal_of_library(){
mssg_debug = mssg_debug+" GAL_OF_LIB => ";
gal_tab = new ArrayList();
n_meuble = 0;
StartCoroutine(get_num_meuble());//récupére le nombre de meuble
for(int n_mbl = 0; n_mbl < n_meuble; n_mbl++){
mssg_debug = mssg_debug+" launch for, to getsingledatameuble ";
StartCoroutine(get_singledata_meuble(n_mbl));//pour chaque meuble, récupére une image et un id_meuble lié à
} //un ensemble de planche dans une autre table
return gal_tab;
}
private IEnumerator get_num_meuble(){
string get_url = get_n_meuble;
WWW hs_get = new WWW(get_url);
yield return hs_get;
if (hs_get.error != null){
mssg_debug = mssg_debug+"There was an error getting the dataMeuble: "+hs_get.error;
}else{
string s_nmeuble = hs_get.text;
n_meuble = int.Parse(s_nmeuble);//renseigne le nombre de meuble
mssg_debug = mssg_debug+" n_meuble = "+n_meuble;
}
}
private IEnumerator get_singledata_meuble(int n_mbl){
string get_url = get_data_meuble+"?n_meuble="+n_mbl;
WWW hs_get = new WWW(get_url);
yield return hs_get;
if (hs_get.error != null){
mssg_debug = mssg_debug+"There was an error getting the dataMeuble: "+hs_get.error;
}else{
ArrayList couple_tab;
string get_content = hs_get.text;
string[] complete_sentence = get_content.Split('|');
foreach (string part_sentence in complete_sentence){
couple_tab = new ArrayList();//couple de données (image/id_meuble)
string[] words = part_sentence.Split(',');
foreach (string word in words){
mssg_debug = mssg_debug+word;
couple_tab.Add(word);
}
gal_tab.Add(couple_tab);//tab (contenant des tableaux de couple) pour générer la galerie cotés 'Main'(GUI)
}
Comment
Best Answer
Answer by Kalu · Jan 22, 2012 at 02:44 AM
Complete with this, the function order is important, it's the second IEnumerator which call the first one. Could help someone else
private IEnumerator get_Tabdata_meuble(){
string get_url = get_data_meuble;
WWW hs_get = new WWW(get_url);
yield return hs_get;
if (hs_get.error != null){
mssg_debug = mssg_debug+"There was an error getting the dataMeuble: "+hs_get.error;
}else{
ArrayList couple_tab;
string get_content = hs_get.text;
string[] complete_sentence = get_content.Split('|');
foreach (string part_sentence in complete_sentence){
if(part_sentence == ""){ break; }//si le morceau de phrase analysé est vide, break
couple_tab = new ArrayList();//couple de données (image/id_meuble/name_meuble)
string[] words = part_sentence.Split(',');
mssg_debug = mssg_debug+"|| données ||";
foreach (string word in words){
couple_tab.Add(word);
mssg_debug = mssg_debug+" |-| Words => "+word;
}
gal_tab.Add(couple_tab);//tab (contenant des tableaux de couple) pour générer la galerie cotés 'Main'(GUI)
}
}
}
public IEnumerator gal_of_library(){
gal_tab = new ArrayList();
n_meuble = 0;
yield return StartCoroutine(get_Tabdata_meuble());//suspend l'execution en attendant gal_tab
mssg_debug = mssg_debug+" || gal_tab.Complete ==> "+gal_tab.Count;
Main otherScript = GetComponent<Main>();
otherScript.send_galtab(gal_tab);
}