I have the following method :
while (TryCount < 2)
{
Lock.Try<object>(inCommandObj.Connection, _timeout, delegate(DataSet ds)
{
dataAdapter = new OdbcDataAdapter(inCommandObj);
returningObj = dataAdapter.Fill(ds);
done = true;
return returningObj;
});
if (done)
break;
inCommandObj.Cancel();
}
You can see that Im trying to pass a DataSet in to this anonymous method but it does not like it? How can I make this possible?
Bestregards
Edit 1 : Exception message now is : Delegate 'System.Func' does not take 1 arguments
Edit 2 : In this case I need to fill a existing dataset with the dataAdapter.Fill method. This have to be done using a lock(with timout) becouse there will be alot of threads running this method. If the lock times out I need to cancel the current MySQL command that have frezzed and then try to send a new command(1 time).
Edit 3 : This is how the lock Class looks like :
public static class Lock
{
public static T Try<T>(object objLock, int timeout, Func<T> f)
{
if (System.Threading.Monitor.TryEnter(objLock, timeout))
{
try
{
return f.Invoke();
}
finally
{
System.Threading.Monitor.Exit(objLock);
}
}
return default(T);
}
public static void Try(object objLock, int timeout, Action f)
{
if (System.Threading.Monitor.TryEnter(objLock, timeout))
{
try
{
f.Invoke();
}
finally
{
System.Threading.Monitor.Exit(objLock);
}
}
}
}
In this line:
public static T Try<T>(object objLock, int timeout, Func<T> f)
Func<T>
is looking for a function that takes no parameters and returns a T
. You're sending it a delegate that takes a DataSet
and returns an object.
Since you're creating a new DataSet
in your delegate anyway, just drop the DataSet ds
from the delegate and you should be all set.
In fact, you can simplify this to:
Lock.Try<object>(inCommandObj.Connection, _timeout, () =>
{
var ds = new DataSet();
dataAdapter = new OdbcDataAdapter(inCommandObj);
returningObj = dataAdapter.Fill(ds);
done = true;
return returningObj;
});
If that doesn't do what you intended, then you need to refactor Try
to take a different Func
argument, or consider a totally different solution.