473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding multithreading to method with parameters?

VM
I'm trying to add multithreading to my win application but I'm having
trouble with the code since the method to be threaded has parameters. How
can I add multithreading to a method with parameters? MSDN says ThreadStart
cannot take parameters, so how can I do it?

public System.Threading.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM=System.Threading.Thread(new
System.Threading.ThreadStart(this.loadAuditFile)); //error
Thread_LoadAZM.Start();
}

private void loadAuditFile(string sFileName)
{
.....
}

Thanks again for your help.
Nov 16 '05 #1
6 2902
VM,

You can't, technically, but you can get around it. Since the delegate
that you pass to the Thread constructor (through a delegate) is attached to
a class, you can access members of that class when your method is called.
So, what you have to do is set the appropriate members of the class, then
start the thread, and have the thread access them when the entry point for
the thread first starts.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VM" <vo******@yahoo.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
I'm trying to add multithreading to my win application but I'm having
trouble with the code since the method to be threaded has parameters. How
can I add multithreading to a method with parameters? MSDN says ThreadStart cannot take parameters, so how can I do it?

public System.Threading.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM=System.Threading.Thread(new
System.Threading.ThreadStart(this.loadAuditFile)); //error
Thread_LoadAZM.Start();
}

private void loadAuditFile(string sFileName)
{
....
}

Thanks again for your help.

Nov 16 '05 #2
VM
I'm not sure I understand what you're saying...
Do you mean that I should declare the variables globally from within the
class so the method can access those members? I was working on somethins
like this:

private string _sFileName;
public void DisplayAuditFile(string sFileName)
{
_sFileName = sFileName;
ThreadStart thr_loadAZM = new ThreadStart(loadAuditFile);
Thread AZMThread = new Thread (thr_loadAZM);
AZMThread.Name = "myThread";
AZMThread.Start ();
}

private void loadAuditFile()
{
// I use _sFileName here
}

Thanks.again.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ox**************@TK2MSFTNGP10.phx.gbl...
VM,

You can't, technically, but you can get around it. Since the delegate
that you pass to the Thread constructor (through a delegate) is attached to a class, you can access members of that class when your method is called.
So, what you have to do is set the appropriate members of the class, then
start the thread, and have the thread access them when the entry point for
the thread first starts.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VM" <vo******@yahoo.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
I'm trying to add multithreading to my win application but I'm having
trouble with the code since the method to be threaded has parameters. How can I add multithreading to a method with parameters? MSDN says

ThreadStart
cannot take parameters, so how can I do it?

public System.Threading.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM=System.Threading.Thread(new
System.Threading.ThreadStart(this.loadAuditFile)); //error
Thread_LoadAZM.Start();
}

private void loadAuditFile(string sFileName)
{
....
}

Thanks again for your help.


Nov 16 '05 #3
VM,

You pretty much got it. If you have to have multiple threads hitting
the same method and worry about maintaining the state, you can always
refactor out that code into another class which will do nothing but have the
method which you want to execute, as well as the state needed to run that
code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VM" <vo******@yahoo.com> wrote in message
news:ur*************@TK2MSFTNGP11.phx.gbl...
I'm not sure I understand what you're saying...
Do you mean that I should declare the variables globally from within the
class so the method can access those members? I was working on somethins
like this:

private string _sFileName;
public void DisplayAuditFile(string sFileName)
{
_sFileName = sFileName;
ThreadStart thr_loadAZM = new ThreadStart(loadAuditFile);
Thread AZMThread = new Thread (thr_loadAZM);
AZMThread.Name = "myThread";
AZMThread.Start ();
}

private void loadAuditFile()
{
// I use _sFileName here
}

Thanks.again.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ox**************@TK2MSFTNGP10.phx.gbl...
VM,

You can't, technically, but you can get around it. Since the delegate
that you pass to the Thread constructor (through a delegate) is attached

to
a class, you can access members of that class when your method is called. So, what you have to do is set the appropriate members of the class, then start the thread, and have the thread access them when the entry point for the thread first starts.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VM" <vo******@yahoo.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
I'm trying to add multithreading to my win application but I'm having
trouble with the code since the method to be threaded has parameters.

How can I add multithreading to a method with parameters? MSDN says

ThreadStart
cannot take parameters, so how can I do it?

public System.Threading.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM=System.Threading.Thread(new
System.Threading.ThreadStart(this.loadAuditFile)); //error
Thread_LoadAZM.Start();
}

private void loadAuditFile(string sFileName)
{
....
}

Thanks again for your help.



Nov 16 '05 #4
Hi VM,

"VM" <vo******@yahoo.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...
I'm trying to add multithreading to my win application but I'm having
trouble with the code since the method to be threaded has parameters. How
can I add multithreading to a method with parameters? MSDN says ThreadStart cannot take parameters, so how can I do it?

public System.Threading.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM=System.Threading.Thread(new
System.Threading.ThreadStart(this.loadAuditFile)); //error
Thread_LoadAZM.Start();
}

private void loadAuditFile(string sFileName)
{
....
}


Another option (which, frankly, I'm surprised doesn't get more "press")
is to use a custom delegate. So within your class you might have a delegate
declared like this:

private delegate void LoadFileHandler(string sFileName);

Then, to call the LoadFile method on another thread, you could use code
like this:

...
LoadFileHandler handler = new LoadFileHandler(this.LoadFile);
handler.BeginInvoke(someFileName, null, null);
...

The BeginInvoke method is generated automatically by the compiler from
the delegate declaration. BeginInvoke has all the parameters defined by the
delegate plus one for passing a callback delegate and one for passing a
"state" parameter. Besides making the parameter passing much easier, the
above also has the advantage of using the thread pool, which is usually a
better idea than creating your own threads.

Regards,
Daniel
Nov 16 '05 #5
VM <vo******@yahoo.com> wrote:
I'm trying to add multithreading to my win application but I'm having
trouble with the code since the method to be threaded has parameters.


See http://www.pobox.com/~skeet/csharp/threadstart.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi

A method I use for this is to encapsulate the thread (method) in a class. Isntead of passing parameters, you can set the properties on the class, then call the method. I got this technique from an article on MSDN.
http://msdn.microsoft.com/library/de...net09272002.as

Download the code sample and have a good look..

Cheers
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
3
by: TazCoder | last post by:
I have a problem with multithreading. The error I keep getting is that findFileButton_Click(...) is not a member function. But when I take the parameters out of the findFileButton_Click(...)...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
2
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
3
by: Zachary Burns | last post by:
I've been banging my head over this for a few hours and I'm not finding any good code samples to demonstrate MultiThreading in C# Framework 1.1 that shows me how to basically have the UI code stay...
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
3
by: bob | last post by:
In an effort to keep my UI responsive I have begun to run my time intensive methods on their own thread and in particular the database inserts. I open my DB connection in the constructor and close...
5
by: shumaker | last post by:
I just read an overview of C# 3.0, and it occured to me that some of these features could make it much easier to write programs that automatically make use of multi core processors. After reading...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.