我必须调用多个消耗时间的存储过程.理想情况下,这些程序必须在同一时间执行,但它会引发很多问题.
这是简化的代码:
private async void refresh_Controle(object sender, RoutedEventArgs e)
{
SqlParameter param1 = new SqlParameter("@devicename", DeviceName);
Task<int> mcResult = GenkaiBase.Database.ExecuteSqlCommandAsync("exec Refresh_McAfee @devicename", param1);
int Mc = await mcResult;
SqlParameter param2 = new SqlParameter("@devicename", DeviceName);
Task<int> dcaiResult = GenkaiBase.Database.ExecuteSqlCommandAsync("exec Refresh_DCAI @devicename", param2);
int Dc = await dcaiResult;
}
Run Code Online (Sandbox Code Playgroud)
这有两个问题:
我尝试在异步方法中使用此代码同时调用这两个过程:
public async Task<bool> Refresh_Control(string devicename)
{
List<Task> Tlist = new List<Task>();
Console.WriteLine("Launch Refresh");
SqlParameter param1 = new SqlParameter("@devicename", devicename);
Task<int> mcResult = Genkai_db.Database.ExecuteSqlCommandAsync("exec Refresh_McAfee @devicename", param1);
SqlParameter param2 …Run Code Online (Sandbox Code Playgroud) c# asynchronous stored-procedures entity-framework async-await
我对async/await如何作为并行工作感到困惑所以我在这里制作了一个测试代码:我尝试发送6个用模拟模拟的任务.每个任务都将执行3个其他子任务.
你可以复制/粘贴测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//job simulation
Func<int, string, Tuple<int, string>> tc = Tuple.Create;
var input = new List<Tuple<int, string>>{
tc( 6000, "task 1" ),
tc( 5000, "task 2" ),
tc( 1000, "task 3" ),
tc( 1000, "task 4" ),
tc( 1000, "task 5" ),
tc( 1000, "task 6" )
};
List<Tuple<int, string>> JobsList = new List<Tuple<int, string>>(input);
//paralelism atempt …Run Code Online (Sandbox Code Playgroud) 我已经从 ObservableCollection 构建了一个动态 UserControl 如下...
public static ObservableCollection<Model.Model.ControleData> ListControleMachine = new ObservableCollection<Model.Model.ControleData>();
public Genkai(string Autorisation) {
InitializeComponent();
DataContext = this;
icTodoList.ItemsSource = ListControleMachine;
Model.Model.ControleData v = new Model.Model.ControleData();
v.ComputerName = "M57095";
v.ImportSource = "LOAD";
ListControleMachine.Add(v);
}
Run Code Online (Sandbox Code Playgroud)
XAML
<ItemsControl x:Name="icTodoList" ItemsSource="{Binding ListControleMachine}" >
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:ControlMachineII}">
<local:ControlMachineII />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
但是如何从 C# 代码访问 DataContext?
例如,假设我想删除带有关闭按钮的 UserControl,我至少需要访问 ControleData.ComputerName 值,然后从 Mainform.ListControleMachine 中删除它。
我找不到实现这一点的最佳实践,并在 UserControl 代码中使用我的数据。
我认为删除按钮代码是这样的(带有硬编码值)
Genkai.ListControleMachine.Remove(Genkai.ListControleMachine.Where(X => X.ComputerName == "M57095").Single());
Run Code Online (Sandbox Code Playgroud)