The variable “this” is normally used in the methods of the DataService class. It’s just not available within the thread’s method. Here’s a more complete code:
private class _ExecuteBuilderInfos
{
public int aBuildId, aElemId, aUserId;
public object is_remind, buildtype, build_objid, storeprocname;
public _ExecuteBuilderInfos(int aaBuildId, int aaElemId, int aaUserId,
object ais_remind, object abuildtype, object abuild_objid, object astoreprocname)
{
aBuildId = aaBuildId;
aElemId = aaElemId;
aUserId = aaUserId;
is_remind = ais_remind;
buildtype = abuildtype;
build_objid = abuild_objid;
storeprocname = astoreprocname;
}
}
private System.Threading.Thread FBackThreadExecuteBuilder = null; //后台执行自动生成配置的线程
private static readonly List<_ExecuteBuilderInfos> _ToExecuteBuilderInfosList = new List<_ExecuteBuilderInfos>();
private static System.Threading.ManualResetEvent FMRE = new System.Threading.ManualResetEvent(false); //用于线程等待
private void AddToQueueExecuteBuilder(int aaBuildId, int aaElemId, int aaUserId,
object ais_remind, object abuildtype, object abuild_objid, object astoreprocname)
{
lock (_ToExecuteBuilderInfosList)
{
_ToExecuteBuilderInfosList.Add(new _ExecuteBuilderInfos(aaBuildId, aaElemId, aaUserId,
ais_remind, abuildtype, abuild_objid, astoreprocname));
if (_ToExecuteBuilderInfosList.Count == 1) FMRE.Set();
}
}
private void _BackThreadExecuteBuilder()
{
//后台循环检测
_ExecuteBuilderInfos lItem;
while (true)
{
lItem = null; bool lbWait = false;
lock (_ToExecuteBuilderInfosList)
{
if (_ToExecuteBuilderInfosList.Count > 0) lItem = _ToExecuteBuilderInfosList[0];
else
{
lbWait = true;
FMRE.Reset();
}
}
if (lbWait) FMRE.WaitOne();
if (lItem != null)
{
try
{
int loObjInstId; string loPromptInfo;
whdErrCode ret = whdAsyncElementBuildExecute(true, lItem.aBuildId, lItem.aElemId, lItem.aUserId, lItem.is_remind, lItem.buildtype,
lItem.build_objid, lItem.storeprocname, out loObjInstId, out loPromptInfo);
//Using "this.Connection" will throw a null exception in whdAsyncElementBuildExecute
//Using "this.Session" will throw an exception: "This service instance has not been activated yet" in whdAsyncElementBuildExecute
}
catch (Exception ex)
{
ErrorLogAndNotify(0, "_BackThreadExecuteBuilder2", "后台异步执行自动生成配置出错:" +
ex.Message + "\nStackTrace:" + ex.StackTrace);
}
finally
{
lock (_ToExecuteBuilderInfosList)
{
_ToExecuteBuilderInfosList.Remove(lItem);
}
}
}
System.Threading.Thread.Sleep(5);
}
}
public whdErrCode whdElementBuildExecute(int aBuildId, int aElemId, int aUserId, out int aoObjInstId)
{
whdErrCode ret = new whdErrCode();
ret.errCode = 0;
ret.errMsg = "成功";
string lPromptInfo = "";
aoObjInstId = 0;
//获取必要的参数
object is_remind, buildtype, build_objid, storeprocname, synctype;
GetSQLValueEx(string.Format(
"select is_remind, buildtype, build_objid, storeprocname, synctype " +
"from t_builder where buildid = {0}", aBuildId),
out is_remind, out buildtype, out build_objid, out storeprocname, out synctype);
if (GD.IsNullInteger(synctype, 1) == 1)
{
//==1 synchronization,同步执行方法
ret = whdAsyncElementBuildExecute(false, aBuildId, aElemId, aUserId, is_remind, buildtype,
build_objid, storeprocname, out aoObjInstId, out lPromptInfo);
//It can use "this" normally in whdAsyncElementBuildExecute
}
else
{
//==2 asynchronous,异步执行方法:把请求添加到队列中,排队执行
if (FBackThreadExecuteBuilder == null)
{
FBackThreadExecuteBuilder = new System.Threading.Thread(
new System.Threading.ThreadStart(_BackThreadExecuteBuilder));
FBackThreadExecuteBuilder.Start();
}
AddToQueueExecuteBuilder(aBuildId, aElemId, aUserId, is_remind, buildtype, build_objid, storeprocname);
}
if (!string.IsNullOrWhiteSpace(lPromptInfo))
{
ret.errMsg = (ret.errMsg == "成功" ? "" : ret.errMsg + " - ") + lPromptInfo;
}
return ret;
}