110,567
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Threading;
using System.Reflection;
namespace showjim.sys.threading
{
public class singleCall
{
private Action Call;
private int IsCalling = 0;
private int IsNewCall = 0;
private readonly object CompleteLock;
public singleCall(Action call, bool isComplete)
{
Call = call;
if (isComplete) CompleteLock = new object();
}
public void call()
{
if (CompleteLock == null)
{
if (Interlocked.Exchange(ref IsCalling, 1) == 0)
{
try
{
Call();
}
catch (Exception error)
{
showjim.sys.exception.debug.writeDebug(MethodBase.GetCurrentMethod(), error);
}
finally { IsCalling = 0; }
}
}
else
{
Monitor.Enter(CompleteLock);
int isCalling = IsNewCall = IsCalling;
IsCalling = 1;
Monitor.Exit(CompleteLock);
if (isCalling == 0)
{
do
{
try
{
Call();
}
catch (Exception error)
{
showjim.sys.exception.debug.writeDebug(MethodBase.GetCurrentMethod(), error);
}
finally
{
Monitor.Enter(CompleteLock);
isCalling = IsCalling = IsNewCall;
IsNewCall = 0;
Monitor.Exit(CompleteLock);
}
}
while (isCalling != 0);
}
}
}
}
}
class test
{
private static void DoSql()
{
string sql = "insert into tableB select ID from tableA where not exists(select ID from tableB where ID=tableA.ID)";
//..执行SQL
}
public static showjim.sys.threading.singleCall doSql
static test()
{
doSql = new showjim.sys.threading.singleCall(DoSql, true);
}
}
test.doSql.call();