Medic3000
Medic3000

Reputation: 786

new thread causes "Method Name Expected" error

I'm trying to call buildRangedJobCache with SelectionRange S as the passed in parameter, But the Compiler(visual studio 2010) gives the error: Method Name Expected below is the call that gives issue:

    private void retrieveSeveralDaysJobs(SelectionRange S)
    {
        ignoreUpdates = false;
        this.SetStatus(DataLogUIStrings.strRetrievingJobInformation);
        Thread buildIndexThread = new Thread(new ThreadStart(buildRangedJobCache(S)));
        buildIndexThread.Priority = ThreadPriority.Lowest;
        buildIndexThread.Start();
    }

and here is the function buildRangedJobCache(SelectionRange S):

    private void buildRangedJobCache(SelectionRange S)
    {
        this.Cursor = Cursors.AppStarting;
        try
        {                
            if (DataStore == null)
            { throw new Exception("Error: DataStore is null, Unable to retrieve jobs."); }
            lock (((ICollection)jobs).SyncRoot)
            {
                for (DateTime Day = S.Start; Day <= S.End; Day.AddDays(1))
                {
                    this.RangeJobs.AddRange(DataStore.GetJobsListForDay(JobDateToDisplay.GetValueOrDefault(DateTime.Today)));
                }
            }
            this.SetStatus(string.Format(DataLogUIStrings.strRetrievedSummaryInformation, this.jobs.Count));
        }
        catch (Exception e)
        {
            Log.Write(e);
        }
        this.Cursor = Cursors.Default;
    }

also I've linked to here: Delegate: Method name expected error as this solution didn't work for me.

**update: apparently it isn't clear, the solution of putting:

Thread buildIndexThread = new Thread(new ThreadStart(buildRangedJobCache));

does the same issue.

Upvotes: 3

Views: 7314

Answers (2)

svick
svick

Reputation: 244777

The Thread constructor requires a delegate (either ThreadStart or ParametrizedThreadStart). You're trying to create the ThreadStart delegate, but you can create a delegate out of a method, not method invocation expression.

So, if your method didn't have a parameter, the following would work:

new Thread(new ThreadStart(buildRangedJobCache));

and so would (because the compiler can infer which delegate are you creating):

new Thread(buildRangedJobCache);

If you need to pass a parameter to your method, you can use ParametrizedThreadStart and the overload of Start() that takes a parameter. But this means you have to change your method to have object parameter and cast it to your type inside the method.

I think a better option would be to use a lambda:

new Thread(() => buildRangedJobCache(S));

This creates an anonymous method that matches ThreadStart and remembers the variable S. This way, you don't need the signature of your method and you also don't need any casting.

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

Pass only method name when creating delegate:

Thread buildIndexThread = new Thread(new ThreadStart(buildRangedJobCache));

Also ThreadStart delegate should not receive any arguments. It is defined as

 public delegate void ThreadStart();

So, your buildRangedJobCache method signature do not match ThreadStart delegate signature. If you want to pass some parameters to thread, you should use ParameterizedThreadStart delegate, which accepts parameter of type object:

private void retrieveSeveralDaysJobs(SelectionRange range)
{
    ignoreUpdates = false;
    this.SetStatus(DataLogUIStrings.strRetrievingJobInformation);
    // pass ParameterizedThreadStart delegate
    Thread buildIndexThread = new Thread(BuildRangedJobCache);
    buildIndexThread.Priority = ThreadPriority.Lowest;
    buildIndexThread.Start(range); // provide parameter for thread
}

private void BuildRangedJobCache(Object obj)
{
    SelectionRange range = (SelectionRange)obj; // cast to your type
    // code
}

Upvotes: 1

Related Questions