473,791 Members | 3,179 Online
Bytes | Software Development & Data Engineering Community
+ 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.Threadin g.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM= System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.loadAuditFi le)); //error
Thread_LoadAZM. Start();
}

private void loadAuditFile(s tring sFileName)
{
.....
}

Thanks again for your help.
Nov 16 '05 #1
6 2920
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.co m

"VM" <vo******@yahoo .com> wrote in message
news:es******** ******@TK2MSFTN GP09.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.Threadin g.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM= System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.loadAuditFi le)); //error
Thread_LoadAZM. Start();
}

private void loadAuditFile(s tring 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 DisplayAuditFil e(string sFileName)
{
_sFileName = sFileName;
ThreadStart thr_loadAZM = new ThreadStart(loa dAuditFile);
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.c om> wrote in
message news:Ox******** ******@TK2MSFTN GP10.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.co m

"VM" <vo******@yahoo .com> wrote in message
news:es******** ******@TK2MSFTN GP09.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.Threadin g.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM= System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.loadAuditFi le)); //error
Thread_LoadAZM. Start();
}

private void loadAuditFile(s tring 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.co m

"VM" <vo******@yahoo .com> wrote in message
news:ur******** *****@TK2MSFTNG P11.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 DisplayAuditFil e(string sFileName)
{
_sFileName = sFileName;
ThreadStart thr_loadAZM = new ThreadStart(loa dAuditFile);
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.c om> wrote in message news:Ox******** ******@TK2MSFTN GP10.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.co m

"VM" <vo******@yahoo .com> wrote in message
news:es******** ******@TK2MSFTN GP09.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.Threadin g.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM= System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.loadAuditFi le)); //error
Thread_LoadAZM. Start();
}

private void loadAuditFile(s tring sFileName)
{
....
}

Thanks again for your help.



Nov 16 '05 #4
Hi VM,

"VM" <vo******@yahoo .com> wrote in message
news:es******** ******@TK2MSFTN GP09.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.Threadin g.Thread Thread_LoadAZM;
public void LoadFile(string sFileName)
{
Thread_LoadAZM= System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.loadAuditFi le)); //error
Thread_LoadAZM. Start();
}

private void loadAuditFile(s tring 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.BeginIn voke(someFileNa me, 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.co m>
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
5923
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 of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should...
3
2999
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(...) inside the ThreadStart it says that the method is not found, and parameters do not match. How can I get around this to multithread my application? Having either this, or sender makes no difference in the maethod. Thanks for all help. (Sorry for the...
11
2296
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 .... py> C().spam(3) spam spam spam
2
3734
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 the Controls.Add() method several times or even calling the Controls.AddRange() method once can take a huge amount of time. Therefore, I would like to be able to run the loop that creates the controls on a different thread and meanwhile give the user...
3
1565
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 separate from the Threading code but allow a return parameter to the UI. I'd like to have something like the following pseudocode....Is this even possible? main.cs {
3
5768
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 DLLs. After building it up, I've deployed it on the web server. From my Windows application, I then add a web reference to that web service.
5
2586
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 or more threads are created, the ImportOneFile method attempts to add a previously added file. If I allow 4 maximum threads and process 4 files, the last file is attempted 4 times and none of the other files are added to the destination. If I...
3
4297
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 it in the dispose method. So database_insert method is quick to do its business and finish. As I was doing these database inserts in response to a timer tick event I had a situation where the time for my database_insert method was greater then...
5
1817
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 that lambda functions are available in 3.0, I read up on the advantages of functional programming, and one of the advantages is that tasks performed in a functional language do not have side effects, and thus tasks can be divided between...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9023
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6770
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.