0
我有2個webclient方法一個接一個執行。函數沒有按正確的步驟順序執行
問題是,第一個只執行完第二個之後執行。
我的主要問題是,爲了使第二個方法優先,第一個方法首先需要完成,因爲第二個方法需要第一個方法中的數據成功執行。
{
string name;
string surname;
string url1 =" //sample/1";
string url2 ="//sample/2";
//execution point!
public void ListBox_SelectionChagned(Object sender, SelectionChangedEventArgs e)
{
var sel = ((ListBox)ContextMenuPopup.Child).SelectedItem;
SubName = Convert.ToString(sel);
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri(url1);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted1);
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri(url2 + name);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted2);
webClient.OpenReadAsync(uri);
System.Diagnostics.Debug.WriteLine(url2 + name);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Method1
void webClient_OpenReadCompleted1(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(ObservableCollection<Subscriptions>));
ObservableCollection<Subscriptions> alldata = ser.ReadObject(e.Result) as ObservableCollection<Subscriptions>;
foreach (data _dt in alldata)
{
name = First_Name;
surname = Surname;
System.Diagnostics.Debug.WriteLine("My first name " + name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Method2
void webClient_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(ObservableCollection<Subscriptions>));
ObservableCollection<Subscriptions> alldata = ser.ReadObject(e.Result) as ObservableCollection<Subscriptions>;
foreach (data _dt in alldata)
{
name = First_Name;
surname = Surname;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class data
{
public string First_Name { get; set; }
public string Surname { get; set; }
}
}
摘要:我需要在第一個方法中獲取值「name」並使用它在第二個方法中追加我的url2?
但我的第二種方法完成之前,我的第一個方法?