c# – 性能问题执行存储过程列表
发布时间:2020-12-15 22:16:08 所属栏目:百科 来源:网络整理
导读:我在启动 Windows服务时遇到了一些性能问题,第一轮我的lstSps很长(大约130个存储过程).反正是为了加快速度(除了加快存储过程的速度)? 当foreach结束并转到第二轮时,它会更快,因为在TimeToRun()上没有那么多返回true.但是,我关心的是第一次,当有更多的存储过
我在启动
Windows服务时遇到了一些性能问题,第一轮我的lstSps很长(大约130个存储过程).反正是为了加快速度(除了加快存储过程的速度)?
当foreach结束并转到第二轮时,它会更快,因为在TimeToRun()上没有那么多返回true.但是,我关心的是第一次,当有更多的存储过程要运行时. 我有关于制作一个数组和一个for循环,因为我读得更快,但我相信问题是因为程序需要很长时间.我可以用更好的方式构建它吗?也许使用多个线程(每个执行一个)或类似的东西? 真的很感激一些提示:) 编辑:只是为了澄清,它的方法HasResult()正在执行SP:s并使看起来花时间.. lock (lstSpsToSend) { lock (lstSps) { foreach (var sp in lstSps.Where(sp => sp .TimeToRun()).Where(sp => sp.HasResult())) { lstSpsToSend.Add(sp); } } } while (lstSpsToSend.Count > 0) { //Take the first watchdog in list and then remove it Sp sp; lock (lstSpsToSend) { sp = lstSpsToSend[0]; lstSpsToSend.RemoveAt(0); } try { //Send the results } catch (Exception e) { Thread.Sleep(30000); } } 解决方法
我会做的是这样的事情:
int openThread = 0; ConcurrentQueue<Type> queue = new ConcurrentQueue<Type>(); foreach (var sp in lstSps) { Thread worker = new Thread(() => { Interlocked.Increment(ref openThread); if(sp.TimeToRun() && sp.HasResult) { queue.add(sp); } Interlocked.Decrement(ref openThread); }) {Priority = ThreadPriority.AboveNormal,IsBackground = false}; worker.Start(); } // Wait for all thread to be finnished while(openThread > 0) { Thread.Sleep(500); } // And here move sp from queue to lstSpsToSend while (lstSpsToSend.Count > 0) { //Take the first watchdog in list and then remove it Sp sp; lock (lstSpsToSend) { sp = lstSpsToSend[0]; lstSpsToSend.RemoveAt(0); } try { //Send the results } catch (Exception e) { Thread.Sleep(30000); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容