簡単な質問です。以下のクラスで、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;
}
}
2番目に考えられる解決策はこのスレッドセーフですか?
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;
}
}
c#asp.net 4.0