Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF上下文和多线程_C#_.net_Multithreading_Entity Framework - Fatal编程技术网

C# EF上下文和多线程

C# EF上下文和多线程,c#,.net,multithreading,entity-framework,C#,.net,Multithreading,Entity Framework,我想知道是否有人能解释一下我的代码出了什么问题。 我在额外的线程中运行计时器。在定时器功能中,我使用EF上下文。我看到定时器功能已经运行了6次,我特别设置了3秒钟的间隔,只运行了100行,但在我的数据库中,我只看到了一个工作。 那么我的错误在哪里呢 namespace WindowsFormsApplication2 { public partial class Form1 : Form { private static int cnt = 0;

我想知道是否有人能解释一下我的代码出了什么问题。 我在额外的线程中运行计时器。在定时器功能中,我使用EF上下文。我看到定时器功能已经运行了6次,我特别设置了3秒钟的间隔,只运行了100行,但在我的数据库中,我只看到了一个工作。 那么我的错误在哪里呢

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private static int cnt = 0;
        private Thread thread;

        public Form1()
        {
            InitializeComponent();
        }

        private  void StartThread()
        {
            var timer = new System.Timers.Timer();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(ProcessDb);
            timer.Interval = 3000;
            timer.Start();
        }

        public void ProcessDb(object sender, System.Timers.ElapsedEventArgs e)
        {
            cnt++;
            ConnService serv = new ConnService();
            serv.UpdateConnections(cnt);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            thread = new Thread(StartThread);
            thread.Start();

            Thread.Sleep(20000);
        }
    }

    public class MyqQueue
    {
        public static Stack<int> myStack = new Stack<int>();
        public static Stack<int> myStack2 = new Stack<int>();
    }
}

namespace WindowsFormsApplication2
{
    class ConnService
    {
        public ConnService()
        {
            cnt++;
        }

        private static int cnt;
        public void UpdateConnections(int second)
        {
            MyqQueue.myStack.Push(second);

            DjEntities ctx = new DjEntities();
            var entities = ctx.Connections.Take(100).Where(c => c.State == null);
            foreach (var connection in entities)
            {
                connection.State = second;
            }
            if (second == 1)
                Thread.Sleep(7000);

            MyqQueue.myStack2.Push(second);
            ctx.SaveChanges();
        }
    }
}
命名空间窗口窗体应用程序2
{
公共部分类Form1:Form
{
私有静态int cnt=0;
私有线程;
公共表格1()
{
初始化组件();
}
私有void StartThread()
{
var timer=new System.Timers.timer();
timer.appead+=新系统.Timers.ElapsedEventHandler(ProcessDb);
定时器间隔=3000;
timer.Start();
}
public void ProcessDb(对象发送方,System.Timers.ElapsedEventArgs e)
{
cnt++;
ConnService serv=新的ConnService();
服务更新连接(cnt);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
线程=新线程(StartThread);
thread.Start();
睡眠(20000);
}
}
公共类MyqQueue
{
公共静态堆栈myStack=新堆栈();
公共静态堆栈myStack2=新堆栈();
}
}
命名空间Windows窗体应用程序2
{
类连接服务
{
公共服务()
{
cnt++;
}
私有静态int-cnt;
公共无效更新连接(整数秒)
{
MyqQueue.myStack.Push(第二个);
DjEntities ctx=新的DjEntities();
var entities=ctx.Connections.Take(100)。其中(c=>c.State==null);
foreach(实体中的var连接)
{
连接状态=秒;
}
如果(秒==1)
睡眠(7000);
MyqQueue.myStack2.Push(秒);
ctx.SaveChanges();
}
}
}
应改为

 ctx.Connections.Where(c => c.State == null).Take(100)
您的第一个查询转换为先不过滤地获取100,然后应用过滤器

我编写的第二个查询将筛选项目,然后再筛选前100名

 ctx.Connections.Where(c => c.State == null).Take(100)