好简单的问题。在下面的类中做 returnAttackDescription 函数线程安全。
我的意思是假设使用所有不同的参数同时对该函数进行 100 次不同的调用(因为它需要 3 个参数)
这个工作线程安全吗?如果不是,我怎样才能使它线程安全?这个数据视图会在第一次函数调用时被初始化吗?还是什么时候?
谢谢你
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public static class Descriptions
{
private static DataView dvAttacks;
static Descriptions()
{
try
{
DataSet dsTempEnemyAttack = DbConnection.db_Select_Query("select AttackType,AttackCategory,BasePower,Accuracy,MoreFacts_tr,MoreFacts_en,Priority from tblAttacks");
dvAttacks = new DataView(dsTempEnemyAttack.Tables[0]);
}
catch
{
}
}
public static string returnAttackDescription(string srAttackName, string srLang, string srCssClassName)
{
dvAttacks.RowFilter = "AttackName='" + srAttackName + "'";
string srReturn = string.Format("<div class=\"{0}\" title=\"" +
"{0}<hr/>" +
"Type: {1}<br/>" +
"Category: {2}<br/>" +
"Base Power: {3}<br/>" +
"Accuracy: {4}<br/>" +
"Priority: {5}<br/>" +
"Effect: {6}\"></div>", srCssClassName, srAttackName,
dvAttacks[0]["AttackType"].ToString(),
dvAttacks[0]["AttackCategory"].ToString(),
dvAttacks[0]["BasePower"].ToString(),
dvAttacks[0]["Accuracy"].ToString(),
dvAttacks[0]["Priority"].ToString(),
dvAttacks[0]["MoreFacts_" + srLang].ToString());
return srReturn;
}
}
第二种可能的解决方案是这个线程安全吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public static class Descriptions
{
private static DataView dvAttacks;
static Descriptions()
{
try
{
dsAttacks = DbConnection.db_Select_Query("select AttackName,AttackType,AttackCategory,BasePower,Accuracy,MoreFacts_tr,MoreFacts_en,Priority from tblAttacks");
}
catch
{
}
}
public static string returnAttackDescription2(string srAttackName, string srLang, string srCssClassName)
{
var results = (from r in dsAttacks.Tables[0].AsEnumerable()
where r.Field<string>("AttackName") == srAttackName
select new
{
srAttackType = r.Field<string>("AttackType"),
srAttackCategory = r.Field<string>("AttackCategory"),
irBasePower = r.Field<Int16>("BasePower"),
irAccuracy = r.Field<Int16>("Accuracy"),
irPriority = r.Field<Int16>("Priority"),
srMoreFacts = r.Field<string>("MoreFacts_" + srLang)
}
).FirstOrDefault();
string srReturn = string.Format("<div class=\"{0}\" title=\"" +
"{0}<hr/>" +
"Type: {1}<br/>" +
"Category: {2}<br/>" +
"Base Power: {3}<br/>" +
"Accuracy: {4}<br/>" +
"Priority: {5}<br/>" +
"Effect: {6}\"></div>", srCssClassName, srAttackName,
results.srAttackType,
results.srAttackCategory,
results.irBasePower,
results.irAccuracy,
results.irPriority, results.srMoreFacts);
return srReturn;
}
}
时间:2019-04-01 标签:c#asp.net 4.0