-2

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);
                }
            }
        }
    }
Ivy
  • 2,285
  • 4
  • 24
  • 29
  • How do you know "it doesn't like it"? – Madbreaks Jan 09 '13 at 18:42
  • 3
    "it does not like it" is a rather poor description of the problem. What happens? What is the error message? – Oded Jan 09 '13 at 18:42
  • 1
    and what are you trying to do? your code hurt my eyes. perhaps, if you write what you want to do , someone can give you a better solution. – DarthVader Jan 09 '13 at 18:43
  • 2
    Why are you passing `ds` at all if you're just immediately reassigning it? – Jon B Jan 09 '13 at 18:44
  • 1
    What is `TryCount` and why isn't this an infinite loop? – asawyer Jan 09 '13 at 18:45
  • @Madbreaks look at Edit 1. – Ivy Jan 09 '13 at 18:53
  • Add a break point to each Lock method. Which one is being called when you have no parameter in the delegate? The Action one right? So when you add a parameter you are trying to get the Func to run. However, you defined T as object, not Dataset. Beyond that, you don't have any mechanism to acutally pass the needed argument to the lock method. – asawyer Jan 09 '13 at 18:54
  • @JonB Dataset is not reassigned, it is filled. – Ivy Jan 09 '13 at 18:54
  • @asawyer, I dont want to try more then 2 times and then giveup and see Edit 3. – Ivy Jan 09 '13 at 18:55
  • @asawyer it is not possible to run the application att all, the syntax is not correct – Ivy Jan 09 '13 at 18:56
  • 1
    You still didnt write what you are trying to do, whatever your are trying with lock and monitors are very dangerous. – DarthVader Jan 09 '13 at 18:57
  • @DarthVader, The problem is that when running diffrent operations lile ExecuteScaler and so on to the MySQL database it will sometime freez and I need to cancel the command from another thread else it will never let go. Thats why I need to use the loop. The ODBCConnection have to be withou lock(If I got it right?) when using multiple threads at the same time. I am opening and closing only once and not for every call or thread. All threads will use the same ODBCConnection to avoid opening alot of connections. – Ivy Jan 09 '13 at 19:00
  • 1
    @Ivy `ds = new DataSet();` -- this assigns a new instance of a `DataSet` to `ds`. – Jon B Jan 09 '13 at 19:01
  • ok but why are trying to do this yourself, there are frameworks you can use easily, take nhibernate or EF. you will probably leave a bug with this code. just saying. – DarthVader Jan 09 '13 at 19:07

1 Answers1

3

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.

Jon B
  • 51,025
  • 31
  • 133
  • 161