C# 中的静态函数,并发问题

普拉桑特·库马尔·维纳科塔

我的应用程序中有许多静态函数,它们在整个应用程序中被多次调用。随着用户数量的增加,这些函数可能会被多次调用。我只是想知道这些函数是否会产生任何并发问题。我的应用程序中的一些静态函数甚至与数据库交互。我确保大部分静态函数只使用局部变量。我的一些静态函数如下。请查看并建议是否需要进行任何更改。

public static bool ISValidString(string CheckString)
{
    if (CheckString != null)
        CheckString = CheckString.Trim();
    if (String.IsNullOrEmpty(CheckString) || String.IsNullOrWhiteSpace(CheckString) || CheckString == "-" ||
        CheckString == "NA" || CheckString == "." || CheckString == "--Select--" || CheckString == "0" || CheckString == "0.00" || CheckString == "00")
        return false;
    else
        return true;
}

    public static DataTable ProcessMaster(string Condition, string Master)
    {
        DataTable dt = new DataTable();
        dt = (((DataSet)HttpContext.Current.Application[Master]).Tables[0].Select(Condition).Length>0)
            ? ((DataSet)HttpContext.Current.Application[Master]).Tables[0].Select(Condition).CopyToDataTable()
            : ((DataSet)HttpContext.Current.Application[Master]).Tables[0].Clone();
        return dt;
    }


    private static void GetPayTypes()
    {
        HttpContext.Current.Application["PayTypeMaster"] = DBFactory.GetMasters(null, "GetPayTypes");
    }
    public static DataTable GetPayTypes(string Filter = null, bool ForceRefresh = false)
    {
        if (HttpContext.Current.Application["PayTypeMaster"] == null || ForceRefresh == true)
            GetPayTypes();

        string Condition;
        if (Factory.ISValidString(Filter))
            Condition = "PayTypeID like '%" + Filter + "%' or PayTypeDesc like '%" + Filter + "%'";
        else
            Condition = Filter;
        return Factory.ProcessMaster(Condition, "PayTypeMaster");
    }

    public static DataSet GetMasters(string Filter, string MasterName)
    {
        CompanyParams myparam = new CompanyParams();
        try
        {
            myparam.AddParameter("@TranType", MasterName);
            myparam.AddParameter("@Filter", Filter);
            return CompanyDB.GetData(SPNameMasters, myparam.Params);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {

        }
    }
兰吉特鲍瓦尔

为什么不在使用应用程序特定变量的代码周围使用 Monitor 或简单的 lock 语句,并且不希望多个线程同时执行代码。最简单的互斥方式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章