Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threading question

I am making my first attempt at using threads in an application, but on the line where I try to instantiate my thread I get the error 'method name expected'. 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;
    }
like image 384
user517406 Avatar asked Sep 12 '11 07:09

user517406


People also ask

What is the benefits of using threading?

Advantages of Thread Threads minimize the context switching time. Use of threads provides concurrency within a process. Efficient communication. It is more economical to create and context switch threads.

What is thread starvation?

Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads.

What is multiple threading?

Multithreading is the ability of a program or an operating system to enable more than one user at a time without requiring multiple copies of the program running on the computer. Multithreading can also handle multiple requests from the same user.

What is thread group why its advised not to use it?

Argh I hate it when people say "don't use ThreadGroup". It's like saying "don't use Threads". A thread has a ThreadGroup, there's no getting around that, so unless you want to have a program with no threads, you are using ThreadGroups. I agree we need a question to address when, how and why not to use them.


1 Answers

ThreadStart constructor only accepts method name. You're executing the method there. Change it to Thread th1 = new Thread(new ThreadStart(FromDatabase));

However that would be incorrect since FromDatabase method appears to be taking parameter while ThreadStart expects method with no parameters so you should be using instead ParameterizedThreadStart

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

like image 173
Muhammad Hasan Khan Avatar answered Oct 02 '22 09:10

Muhammad Hasan Khan