0

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....

        }

    }
}
Milan
  • 3,209
  • 1
  • 35
  • 46

1 Answers1

2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi, thanks for the reply. What I was trying to achieve the following: On the "btnUrl_Click" event a new delegate (named "del") for the "getUrlThread" method is created. That method reference is called by a new Thread "t" and should run its course (whatever happens on that thread does not matter for now). You say..."parameter-less delegate..." but it is clearly defined as: "public delegate string getUrlThreadDelegate(string targetUrl);" with one string parameter. What am I missing ? – Milan Sep 09 '14 at 23:24
  • ...ok, scrap that previous comment, I got it ! Once I removed the parameter and set method to void it works. Thx. – Milan Sep 09 '14 at 23:39
  • ...also find an explanation here: http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread – Milan Sep 09 '14 at 23:45