Thread issue

I am making my first attempt to use threads in an application, but on the line where I am trying to create an instance of my thread, I get the expected method name "method name". Here is my code:

private static List<Field.Info> FromDatabase(this Int32 _campId)
    {
        List<Field.Info> lstFields = new List<Field.Info>();

        Field.List.Response response = new Field.List.Ticket
        {
            campId = _campId
        }.Commit();

        if (response.status == Field.List.Status.success)
        {
            lstFields = response.fields;
            lock (campIdLock)
            {
                loadedCampIds.Add(_campId);
            }
        }

        if (response.status == Field.List.Status.retry)
        {
            Thread th1 = new Thread(new ThreadStart(FromDatabase(_campId)));

            th1.Start();

        }

        return lstFields;
    }
+5
source share
4 answers

The ThreadStart constructor accepts only the method name. You execute the method there. Change it toThread th1 = new Thread(new ThreadStart(FromDatabase));

However, this would be wrong, because the FromDatabase method apparently takes a parameter, while ThreadStart expects a method without parameters, so you should use ParameterizedThreadStart instead

Read the following article for more details: http://www.dotnetperls.com/parameterizedthreadstart

+11

, FromDatabase.

, FromDatabase, :

private static List<Field.Info> FromDatabase(this Int32 _campId)
{
    List<Field.Info> lstFields = new List<Field.Info>();

    Field.List.Response response = new Field.List.Ticket
    {
        campId = _campId
    }.Commit();

    if (response.status == Field.List.Status.success)
    {
        lstFields = response.fields;
        lock (campIdLock)
        {
            loadedCampIds.Add(_campId);
        }
    }

    if (response.status == Field.List.Status.retry)
    {
        Thread th1 = new Thread(new ParameterizedThreadStart(FromDatabase));

        th1.Start(_campId);

    }

    return lstFields;
}
+1

, , int. . , -

private static List<Field.Info> FromDatabase(this object _campId)
{
    int _campIdInt = (int)_campId;
    List<Field.Info> lstFields = new List<Field.Info>();

    Field.List.Response response = new Field.List.Ticket
    {
        campId = _campIdInt
    }.Commit();

    if (response.status == Field.List.Status.success)
    {
        lstFields = response.fields;
        lock (campIdLock)
        {
            loadedCampIds.Add(_campIdInt);
        }
    }

    if (response.status == Field.List.Status.retry)
    {
        Thread th1 = new Thread(FromDatabase);

        th1.Start(_campIdInt);

    }

    return lstFields;
}
+1

- , , , ParameterizedThreadStart.

new Thread(() => FromDatabase(_campId)).Start();

:

  • .
  • .

ParameterizedThreadStart object.

0

All Articles