如何在.net 4.0中同时调用同一方法的多个实例 [英] How to call a multiple instance of same method at same time in .net 4.0

查看:106
本文介绍了如何在.net 4.0中同时调用同一方法的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个方法可以进行sql数据库备份和恢复。因为这可能是耗时的活动



因此我在dataTable中收集相关数据,然后尝试将这些作为参数传递给方法





I have a method which does sql database backup and restore. since this can be time consuming activity

so i am collecting relevant data in a dataTable and then trying to pass those as parameter in the method

Parallel.ForEach(dtRestoreQueue.AsEnumerable(), drow =>
{
    PerformDBBackup(drow["Source backup Directory"].ToString(), drow["Source Server Database"].ToString(), drow["Destination backup Directory"].ToString(), drow["Source Server Instance"].ToString(), drow["Destination Server Instance"].ToString(), Convert.ToBoolean(drow["Source Integrated Security"].ToString()), drow["Source User Name"].ToString(), drow["Source Password"].ToString(), drow["Destination Data Directory"].ToString(), drow["Destination Log Directory"].ToString(), drow["Restore Database Name"].ToString());

});





但这不起作用,我正在变得像文件正在使用这样的错误



但是每当我使用for循环时,代码工作正常,如预期



你可以告诉我如何以任何其他方式并行调用此方法来执行此操作。



but this is not working and i am getting wierd error like file is in use like that

but whenever i am doing the same with for loop the code is working fine as expected

could you please let me know how to call the this method in parallel any other way to do this.

推荐答案

并行化可能无济于事:最佳解决方案是使用正确的备份方法,如果需要,可以在BackgroundWorker中调用它们。

看看这个:用C#备份SQL数据库 [ ^ ] - 它可能会有所帮助。
Parallelizing it may not help: the best solution is to use "proper" backup methods, and if necessary call them in a BackgroundWorker.
Have a look at this: Backing up an SQL Database in C#[^] - it may help.


这篇关于如何在.net 4.0中同时调用同一方法的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆