A Threading gotcha when converting from VB to C#

I was translating a threading example that I originally wrote in VB and C# was giving me a hard time about something that made no sense.

It turned out that in C#, a delegate for a ParameterizedThreadStart must literally use the object class in its signature.

 

For example in VB, I had this method

Public Sub UpcomingTripEmail(ByVal cust as Customer)..do some stuffEnd Sub

And I had no problem creating a Thread with it

Dim workerThread = New Thread(UpcomingTripEmail)

But when I tried it in C#

public void UpcomingTripEmails(Customer cust){..do some stuff}

The code for creating the thread

Thread workerThread = new Thread(emailThread.UpcomingTripEmails);

complained that it had invalid arguments.

What it wanted was

 

public void UpcomingTripEmails(object cust){ var cust = (Customer)cust;
 ...do some stuff
}

Picky picky picky!

No comments (yet...)

Leave a Comment