Passing a variable to a new thread?

R

Raith

Ok I'm still very new to C# and I've been searching around and getting
myself all confused so gave up and am posting here.

Vastly simplified but I have a form with some tick boxes on it,
clicking on ok populates an array with the values selected and runs a
process passing the array, a bit like below...

public void process(string[] valuesArray)
{
// Do stuff that takes a while here.
}

private void button1_Click(object sender, EventArgs e)
{
string[] valuesArray = new string[3];
valuesArray = {"test1", "test2", "test3"};

process(valuesArray);
}

This is fine but locks up the form until it's finished, so I thought
I'd do the processing in a new thread... Only thing is I don't know how
to pass the array to the process when starting a new thread.

Any help appreciated, thanks in advance!
 
R

Ravi Ambros Wallau

As explained in MSDN, a thread in C# does not accept parameters.
If using VS.NET 2005, you can use the BackgroundTask component.
In VS.NET 2003, you have two choices:
1. Create a class that encapsulates the thread - this class should have all
thread arguments, and the thread will access the arguments of this thread to
perform any operation needed;
2. Use a static variable somewhere that should be accessed by the thread.

Probally there are other options, but these are the two basic ones...
 
W

William Stacey [MVP]

Here is a way to use type safe parm to start new thread or start one on the
thread pool.
http://spaces.msn.com/staceyw/blog/cns!F4A38E96E598161E!651.entry

--
William Stacey [MVP]

| Ok I'm still very new to C# and I've been searching around and getting
| myself all confused so gave up and am posting here.
|
| Vastly simplified but I have a form with some tick boxes on it,
| clicking on ok populates an array with the values selected and runs a
| process passing the array, a bit like below...
|
| public void process(string[] valuesArray)
| {
| // Do stuff that takes a while here.
| }
|
| private void button1_Click(object sender, EventArgs e)
| {
| string[] valuesArray = new string[3];
| valuesArray = {"test1", "test2", "test3"};
|
| process(valuesArray);
| }
|
| This is fine but locks up the form until it's finished, so I thought
| I'd do the processing in a new thread... Only thing is I don't know how
| to pass the array to the process when starting a new thread.
|
| Any help appreciated, thanks in advance!
|
 
J

Jon Skeet [C# MVP]

Ravi Ambros Wallau said:
As explained in MSDN, a thread in C# does not accept parameters.

Well, not in .NET 1.1. In .NET 2.0 there's ParameterizedThreadStart
which makes life a lot easier.
 
R

Raith

Ah ok, I've kind of get it working using ParameterizedThreadStart...
but how do I convert the "object" type, back to a string[]?
 
W

William Stacey [MVP]

with a cast:
string[] sa = (string[])value;

If you want the type safe version, you could use the code I showed in other
post.
--
William Stacey [MVP]

| Ah ok, I've kind of get it working using ParameterizedThreadStart...
| but how do I convert the "object" type, back to a string[]?
|
 
R

Raith

Ahh I see! I'd used the ToString function before to convert to a string
but didn't know how to do the cast that way.

Got it working using a different thread now, unfortunately I'm now
going to have to rethink my code because there's some code at the end
of the main thread that stuffs things up while the worker thread is
running. I'll get there in the end, thanks for the help :)
 
G

Gayan Gunarathne

Use this way...

String[] value;
public void process(string[] valuesArray)
{
Console.WriteLine("Thread started");
// Do stuff that takes a while here.
}
public void process()
{
process(value);
}

private void button2_Click(object sender, EventArgs e)
{
Thread newThread=new Thread(new ThreadStart(process));
newThread.Start();
}
 
G

Gayan Gunarathne

try this..

String[] value;
public void process(string[] valuesArray)
{
Console.WriteLine("Thread started");
// Do stuff that takes a while here.
}
public void process()
{
process(value);
}

private void button2_Click(object sender, EventArgs e)
{
Thread newThread=new Thread(new ThreadStart(process));
newThread.Start();
}
 

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