小编Pet*_*Luu的帖子

在使用Entity Framework等待数据更改时,是否有更好的替代Thread.Sleep?

我有一个使用Entity Framework访问的数据库和一组我需要在远程机器上按顺序执行的操作.机器在完成后通过更新数据库进行通信,否则不报告.鉴于其余代码的体系结构,提供一些我可以加入的事件将是困难的(虽然这将是理想的).我试图做的一个例子是:

private enum Machines
{
    SetA,
    SetB
};

private void Action()
{
    ExecuteWork(Machines.SetA);
    while (!IsWorkDone(Machines.SetA))
    {
        Thread.Sleep(TimeSpan.FromMinutes(1));
    }
    ExecuteWork(Machines.SetB);
}

private void ExecuteWork(Machines machineGroup)
{
    // Do long running work on every remote machine in this set, between 10-40 minutes.
    // When this work is completed, each machine reports its completion to a database.
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法阻止执行下一个操作,直到第一个操作完成,如果限制是我们必须依赖数据库进行状态更新?

private bool IsWorkDone(Machines machineGroup)
{
    using (_context = new DbContext())
    {
        var machines = _context.Machines.Where(machine => machine.Group.Equals(machineGroup.ToString(), StringComparison.OrdinalIgnoreCase));
        foreach (var machine in machines) …
Run Code Online (Sandbox Code Playgroud)

c# multithreading entity-framework

4
推荐指数
1
解决办法
651
查看次数

标签 统计

c# ×1

entity-framework ×1

multithreading ×1