473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1742
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sayoyo | last post by:
Hello:) I wish to know if there is some pattern design that allow passing the result of one executing thread(at the end of the execution) to another thread. And it is possible to pass data...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: LeTubs | last post by:
Hi I'm think I'm having some problem with pointers..... namely passing them around...Or I think ? I've used gdb to find the following (note conn is of type MYSQL *conn). Program received...
3
by: rollasoc | last post by:
Hi, Having a slight problem with how to pass parameters to functions between threads. I have a form with a progress bar and a description label on it. It has a public function...
2
by: Jerry Spence1 | last post by:
I have seen the following example of passing data to a thread: Class ThreadData Public Id As Integer Public Msg As String Public Done As Boolean ' The entry point for the ...
1
by: Hemanth | last post by:
Hi, I am using .NET 2.0. Suppose I mark a static variable with the ThreadStatic attribute in a class. From some method in the class, if I pass that variable to an object of another class as...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
11
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
8
by: asit | last post by:
How do I pass parameters to created thread..here is the code(but it can't pass data..it uses global variable) #include <windows.h> #include <stdio.h> DWORD Sum; DWORD WINAPI...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.