首页 > 解决方案 > 在 C# 中由多个线程调用的内部方法创建的数据表是否需要锁定?

问题描述

我有这个下面的方法,在当前方法中创建的数据表,我会将这 4 个数据表发送到 sql 过程,它根据一些验证返回真/假。

怀疑:对于单线程应用程序,它的工作正常。将来我们计划实现任务,以便多个任务可以调用此方法,这种编写方法的方式是否适合多个线程,或者我是否需要实现“锁定”

我的理解:

这些表不被多个线程共享,每个线程都有自己的数据表,因为这些数据表是在当前方法中实例化的,所以在向数据表添加列时不需要实现锁。我理解正确的方法吗?

private static bool ValidateContent()
        {
            DataTable bomdata = DataTables.GetChangeDataTable();
            DataTable rateupdata = DataTables.GetChangeDataTable();
            DataTable domaintabledata = DataTables.GetChangeDataTable();
            DataTable algo = DataTables.GetChangeDataTable();

            _pvCircularInfo.CircularInfo.ForEach(x =>
            {

                x.CircularDetails.Algorithms.ForEach(a => {

                    bomdata.Columns.add(/*adds stmg here to data table*/);
                });

                x.CircularDetails.FormsInfo.risks.ForEach(a => {

                    bomdata.Columns.add(/*adds stmg here to data table*/);

                });

                x.CircularDetails.FormRules.ForEach(a => {

                    bomdata.Columns.add(/*adds stmg here to data table*/);
                });

                x.CircularDetails.Lookuptables.ForEach(a => {

                    bomdata.Columns.add(/*adds stmg here to data table*/);
                });

                x.CircularDetails.DomainTables.ForEach(a => {
                    bomdata.Columns.add(/*adds stmg here to data table*/);

                });

            });
           calls the db proc by sending all 4 data tables thast returns true or false
            return false/false;

        }

标签: c#

解决方案


推荐阅读