473,387 Members | 1,876 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,387 software developers and data experts.

passing data to thread

How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}

Nov 16 '05 #1
4 7855
class ThreadController
{
private int i;
private string s;
public ThreadController(int i, string s)
{
this.s = s;
this.i = i;
}
public void ThreadProc()
{
// use i and s
}
}

ThreadController tc = new ThreadController(42, "Hello World");
Thread t = new Thread(new ThreadStart(tc.ThreadProc);
t.Start();

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<41********@news.starhub.net.sg>

How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2
Hi Bern,
How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}


I'm using a wrapper that supports a delegate with a generic
state object argument.
ContextThread newthread = new ContextThread(
new ContextThreadStart (Method), someObject);

void Method(object state) {
SomeObject c = state as SomeObject;
...
}

bye
Rob
using System;
using System.Threading;

namespace ACME {

public delegate void ContextThreadStart(object state);

public class ContextThread {

public Thread Thread {
get {
return thread;
}
}
readonly Thread thread;
readonly ContextThreadStart start;
readonly object state;

public ContextThread(ContextThreadStart start, object state) {
this.start = start;
this.state = state;
this.thread = new Thread(new ThreadStart(ThreadProc));
}

public void Start() {
thread.Start();
}

void ThreadProc() {
start(state);
}
}

}
Nov 16 '05 #3
Hi Bern,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #4
Bern <x@x.com> wrote:
How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
10
by: Mark Denardo | last post by:
Hi, I have multi-threaded application that requires one particular thread (Non-Form based) to receive data from other threads. I know how to pass control to Form Threads, but I believe there's no...
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 ...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
0
by: Iain McIntosh | last post by:
Hello if anyone can help me with this I will be very grateful. I have a working version of this program as a windows application, when I try to port it over to c# ASP.NET I can't make it work. ...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
2
by: Carl Heller | last post by:
Working in VS2003, .Net 1.1 I'm working on a project where I compare data between two databases. This is a lengthy process, and very data intensive, so I decided to create a class, and thread...
5
by: nass | last post by:
this is a thought experiment. i do not have the time to implement it and test it to see if it works so i am relying on your good will:) thank you in advance im on a linux machine (slackware...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...

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.