推荐的方法是在WPF事件中同时运行两个编码器



我有一个WPF应用程序,在该应用程序中,用户输入搜索名称/字符串,并根据复选框选择server1或server2,然后命中搜索按钮。基于复选框选择,它将查询并搜索第一个服务器或第二台服务器,但也可以选择一次搜索/查询这两个服务器。我在EventHandler中使用IF语句和其他语句的当前设置,但是我需要在选择两者的情况下实现第三个条件。

现在,在第三个控制语句中,我希望两个查询同时执行,但我不确定实现此问题的最佳方法是什么。

这是代码:

private void searchGzBTN_Click(object sender, RoutedEventArgs e)
        {
            NetworkCredential credentials = new NetworkCredential(usrBoxValue, pwdBoxValue, domainFuerNetCred);
            if ((!string.IsNullOrEmpty(GzBoxValue)) && Handle(checkboxGz2010)) 
            {
                ListBox2010.Items.Clear();
                using (ClientContext clientctx = new ClientContext(url2010BoxValue))
                {
                    clientctx.Credentials = credentials; 
                    List selektierteListe = clientctx.Web.Lists.GetByTitle(comboBoxValue);
                    CamlQuery query = new CamlQuery();
                    query.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Contains><FieldRef Name='GZ'/><Value Type='Text'>" + GzBoxValue + "</Value></Contains></Where></Query><ViewFields><FieldRef Name='BaseName' /><FieldRef Name='Title' /><FieldRef Name='Name' /><FieldRef Name='ID' /></ViewFields></View>"; //Viewfields noch einbauen
                    ListItemCollection GFGZListe = selektierteListe.GetItems(query);
                    clientctx.Load(GFGZListe);
                    clientctx.ExecuteQuery();
                    foreach (ListItem item in GFGZListe)
                    {
                        ListBox2010.Items.Add("" +item["FileleafRef"]);
                    }
                    GFGZListePass = GFGZListe;
                }
            }

如前所述,直接紧随其后的是Else语句,基本上只是检查其他服务器复选框是否已启用,并且基本上根据Clientctx.executequery()基本执行相同的操作;唯一的区别是查询略有不同,并且变量名称略有不同(用于存储结果)。

现在,如果我需要这两个Camlquery同时运行该怎么办?我应该看一下平行。还是任务和线程?这里推荐的行动课程是什么?

本质上我想要:

1)检查是否检查了两个复选框2)如果他们同时执行两个查询3)返回结果

因此,如果有人对接近此问题ID的最佳方法有建议。

善意

如果您使用的是SharePoint,则自然异步 api, ExecuteQueryAsync

public override void ExecuteQueryAsync(
    ClientRequestSucceededEventHandler succeededCallback,
    ClientRequestFailedEventHandler failedCallback
)

首先,您需要将其用作TaskCompletionsource作为单独的方法(我们称为ExecuteQueryTapAsync),使用succeededCallbackfailedCallback返回Task。有关此信息的更多信息:如何:将EAP模式包装在任务,任务和基于事件的异步模式中。

然后,您可以在下面的下并行运行两个查询。您需要进行按钮单击处理程序async

private async void searchGzBTN_Click(object sender, RoutedEventArgs e)
{
    try
    {
       // ... 
       // server1
       var query1 = ExecuteQueryTapAsync(clientctx);
       // ...
       // server2
       var query2 = ExecuteQueryTapAsync(clientctx);
       // ...
       // asynchronously await both results
       await Task.WhenAll(query1, query2);
       // ...
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

您可能需要使用Task.Factory.StartNew来产生两个背景线程。这是一个总体想法:

Task.Factory.StartNew(() => {
    // ... run this block in a background thread
    ListItemCollection GFGZListe = selektierteListe.GetItems(query);
    clientctx.Load(GFGZListe);
    // return the result to the continuation block (below)
    return GFGZListe;
}).ContinueWith(task => {
    // handle results
    foreach (ListItem item in task.Result)
    {
        ListBox2010.Items.Add("" +item["FileleafRef"]);
    }
}, 
TaskScheduler.FromCurrentSynchronizationContext());    // use the UI thread for the continuation

只需确保不要将任何与UI相关的内容放在背景线程中,否则您将获得跨线程访问例外。

最新更新