Ok easy question. At the below class do returnAttackDescription function thread safe.
What i mean is assume that 100 different calls is made to that function at the same time with all different parameters (as it takes 3 parameters)
Would this work thread safe ? if not how can i make it thread safe ? and will this dataview get initialized at the first function call ? or when ?
thank you
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;
}
}
Second possible solution is this thread safe ?
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
The solutions is not actual thread safe, the issue that I see now is that the dvAttacks.RowFilter is change the results, so you need to make it thread safe by using a thread lock as:
private static readonly object _lock = new object();
public static string returnAttackDescription(string srAttackName, string srLang, string srCssClassName)
{
lock (_lock)
{
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;
}
}
The function Descriptions() is called when your application starts, make the static data, DataView dvAttacks and then this data stay on memory with out change until the application restarts. Each pool of your application have a different set of this data.
On the second solution you only reads them with out affect the DataView, so you do not change them to have any conflict. So as it is, work just fine, and all the new memory did not conflict on threads.
This is the parameter that is common private static DataView dvAttacks; among threads, and this is only created when your application starts - after that you have only reads... but you must not change it ether inside with out lock.
The different between one and two is that in the first the data changes inside with the filter, on the second you just read them and copy the one you need on new memory, so you only reads them. The second works as it is.
and will this dataview get initialized at the first function call
Its initialized the moment the application starts and before any page call. You can use the Debug.Write to check this out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With