ParameterizedThreadStart with parameter

How do I let the ParameterizedThreadStart take a List<byte> as a parameter ?
List<byte> pmList = new List<byte>();
 
ParameterizedThreadStart pts = new ParameterizedThreadStart(CheckPunches);
                        Thread t = new Thread(pts);
                        t.Start(pmList);
 
private void CheckPunches(List<byte> bBuffer)
{
 
}

Open in new window

u2envy1Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CuteBugConnect With a Mentor Commented:
You have to declare CheckPunches in the following way
            List<byte> pmList = new List<byte>();
 
            ParameterizedThreadStart pts = new ParameterizedThreadStart(CheckPunches);
            Thread t = new Thread(pts);
            t.Start(pmList);
 
 
        }
 
        private void CheckPunches(object data)
        {
            List<byte> pmList = (List<byte>)data;
        }

Open in new window

0
 
u2envy1Author Commented:
Perfecto !!
How do I call the CheckPunches in a ThreadPool.... ?
0
 
CuteBugCommented:
WaitCallback workItem = new WaitCallback(CheckPunches);
ThreadPool.QueueUserWorkItem(CheckPunches,  pmList);
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
u2envy1Author Commented:
Thanks....
Can I add this in a loop or should I keep the WaitCallback outside the loop ?
0
 
CuteBugCommented:
Which loop are you referring to?
0
 
u2envy1Author Commented:
Sorry...... My loop looks like this.

Process Buffer()
{
do
{
 
WaitCallback workItem = new WaitCallback(CheckPunches);
ThreadPool.QueueUserWorkItem(CheckPunches,  pmList);
pmList = Connection.ReadData();
 
}
While(pmList.Count() != 0);
}

Open in new window

0
 
tcullerCommented:
You can actually just omit the WaitCallback delegate decleration entirely; there's no reason for it, really.

Also, you're going to get a compiler error on line 11 for 2 reasons: first, "while" is capitalized. Second, the List object's Count method is actually a property, so you'll need to remove those parentheses.

Using the ThreadPool and individual threads are completely different things, so if you'd like me to explain what's going on here, I can :)
Nate
0
 
u2envy1Author Commented:
Sorry about the two mistakes. Was just typing over my head.
So you saying that every time I call the CheckPunches it will be executed in a new thread with out the WaitCallback delegate ?
0
 
CuteBugCommented:
WaitCallback delegate is required.

@u2envy1:
By the way, It should be ThreadPool.QueueUserWorkItem(workItem, pmList);

And yes it should be within the loop. Therefore each time the pmList gets new data, a new instance of CheckPunches is added to the ThreadPool for processing.
0
 
u2envy1Author Commented:
Thx..... all your help is much appreciated
0
 
tcullerCommented:
Hate to go back and forth here, but... The WaitCallback delegate is NOT required. The compiler will automatically do this for you--there's no reason to create it yourself. The following will work just fine.
ThreadPool.QueueUserWorkItem(YourMethod, pmList);
// "YourMethod" should match the name of the handling method. Be sure
// to omit the parentheses "()"
 
void YourMethod(Object myData) 
{
     // Processing, etc etc
}

Open in new window

0
 
CuteBugCommented:
tculler, you are right.
my mistake.
0
 
tcullerCommented:
No problem, honest mistake ;)
0
 
u2envy1Author Commented:
Thx guys......
tculler: Sorry I neva award you with some points.
0
All Courses

From novice to tech pro — start learning today.