New Thread does not see the supplied method from the same namespace

I am new to c# and I am trying to figure out what am I doing wrong in the following script. I am getting an error "Method Name Expected" when starting a new Thread for the "del" delegate. The method getUrlThread is clearly defined and the delegate del is pointing to it so why is it not being seen? Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;

namespace updateNewFrontEnd
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
        }

        // define function pointer
        // =======================
        public delegate string getUrlThreadDelegate(string targetUrl);

        // define function for the function pointer
        // ========================================
        public string getUrlThread(string targetUrl)
        {

            httpRequestClass urlResponseText = new httpRequestClass();

            urlResponseText.TargetUrl = targetUrl;

            string text = urlResponseText.getUrlResponse();

            return text;
        }


        private void btnUrl_Click(object sender, EventArgs e)
        {
            // top decalrations
            // ================

            .... some code here....

            string targetUrl = "some string here...";


            // instantiate the HTTP call object to the "targetUrl" url
            // =======================================================

            getUrlThreadDelegate del = new getUrlThreadDelegate(getUrlThread);


            Thread t1 = new Thread(new ThreadStart(del(targetUrl))); // ERROR HERE !!!
            t1.Start();


            .... some more code here....

        }

    }
}
Jon Skeet
people
quotationmark

It's not really clear what you're trying to achieve, but currently you're calling del here:

new Thread(new ThreadStart(del(targetUrl)))

You've got a fundamental problem though - the method you're trying to call doesn't have the right signature for ThreadStart (which is a void parameterless delegate). It's not clear what you want to do with the response returned by getUrlThread, but you could use a lambda expression to call the method more simply without declaring your own delegate type:

Thread t1 = new Thread(() => getUrlThread(targetUrl));
t1.Start();

That will fetch the value - but then throw it away.

As an aside - but an important one - it would really help the readability of your code if you'd follow .NET naming conventions. Namespace, type and method names should be PascalCased, not camelCased.

Additionally, you might want to consider using async/await... or at least use Task<T> if you're using .NET 4.0 or higher.

people

See more on this question at Stackoverflow