我有 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?
但我的第二种方法在我的第一种方法之前完成?