Solved

ParameterizedThreadStart with parameter

Posted on 2009-05-07
14
11,092 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:u2envy1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 16

Accepted Solution

by:
CuteBug earned 500 total points
ID: 24324822
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
 

Author Comment

by:u2envy1
ID: 24324919
Perfecto !!
How do I call the CheckPunches in a ThreadPool.... ?
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24324955
WaitCallback workItem = new WaitCallback(CheckPunches);
ThreadPool.QueueUserWorkItem(CheckPunches,  pmList);
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:u2envy1
ID: 24325093
Thanks....
Can I add this in a loop or should I keep the WaitCallback outside the loop ?
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24325208
Which loop are you referring to?
0
 

Author Comment

by:u2envy1
ID: 24325261
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
 
LVL 9

Expert Comment

by:tculler
ID: 24325338
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
 

Author Comment

by:u2envy1
ID: 24325429
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
 
LVL 16

Expert Comment

by:CuteBug
ID: 24325556
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
 

Author Closing Comment

by:u2envy1
ID: 31578959
Thx..... all your help is much appreciated
0
 
LVL 9

Expert Comment

by:tculler
ID: 24326077
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
 
LVL 16

Expert Comment

by:CuteBug
ID: 24326183
tculler, you are right.
my mistake.
0
 
LVL 9

Expert Comment

by:tculler
ID: 24326251
No problem, honest mistake ;)
0
 

Author Comment

by:u2envy1
ID: 24326348
Thx guys......
tculler: Sorry I neva award you with some points.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question