471,337 Members | 859 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Passing a variable to a new thread?

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!

Feb 23 '06 #1
9 1658
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...

--
Ravi Wallau
no****@nospam.org
"Raith" <Ra******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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!

Feb 23 '06 #2
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]

"Raith" <Ra******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
| 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!
|
Feb 23 '06 #3
Raith <Ra******@gmail.com> wrote:
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...


<snip>

See http://www.pobox.com/~skeet/csharp/t...rameters.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #4
Ravi Ambros Wallau <no****@nospam.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #5
Ah ok, I've kind of get it working using ParameterizedThreadStart...
but how do I convert the "object" type, back to a string[]?

Feb 24 '06 #6
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]

"Raith" <Ra******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
| Ah ok, I've kind of get it working using ParameterizedThreadStart...
| but how do I convert the "object" type, back to a string[]?
|
Feb 24 '06 #7
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 :)

Feb 24 '06 #8
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();
}

*** Sent via Developersdex http://www.developersdex.com ***
Mar 20 '06 #9

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();
}
*** Sent via Developersdex http://www.developersdex.com ***
Mar 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
2 posts views Thread by Jerry Spence1 | last post: by
9 posts views Thread by zholthran | last post: by
11 posts views Thread by =?Utf-8?B?U3VqZWV0?= | last post: by
8 posts views Thread by asit | last post: by

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.