Adding multithreading to method with parameters?


V

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.
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

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.
 
V

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 said:
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]
- (e-mail address removed)

VM said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

VM said:
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.


message news:[email protected]
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]
- (e-mail address removed)

VM said:
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.
 
D

Daniel Pratt

Hi VM,

VM said:
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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top