473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading parameter

Hello,

This might sound as a stupid question, but how can I pass parameters to a
thread? Let me rephrase that.

When I have a method, i declare it like this:

public void DoSomething(str ing aParameter, int anotherParamete r) {
....
}

When I want to use this method with a thread I do it like this:
Thread doSomethingThre ad= new Thread(new ThreadStart(thi s.DoSomething)) ;

doSomethingThre ad.Start();

Where do I add the parameters?

Nov 15 '05 #1
2 3554
grmAbay <gr*****@hotmai l.com> wrote:
This might sound as a stupid question, but how can I pass parameters to a
thread?


Not a stupid question at all - see
http://www.pobox.com/~skeet/csharp/threadstart.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
100
Hi,
In addition to what you can find in Jon's site I want to give you one more
idea.
You can decalre a delegate for the method you want to start in a thread.

//Delegate and the method itslef might have a return value as well
delegate void ThreadMethod(st ring aParameter, int anotherParamete r) ;

public void DoSomething(str ing aParameter, int anotherParamete r) {
....
}

ThreadMethod starter = new ThreadMethod(Do Something);

Now you can start DoSomething in a separate thread:

starter.BeginIn voke(param1, param2, null, null);

You can use IAsyncResult returned by BeginInvoke and use EndInvoke to wait
for the thread to finish and get result. You can optain a wait handle,
checking periodically IAsyncResult.Is Completed or using callback method. You
have options.

Begin invoke uses a thread form the thread pool

More info you can find in MSDN
ms-help://MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpovrasynchrono usprogrammingo
verview.htm

HTH
B\rgds
100

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
grmAbay <gr*****@hotmai l.com> wrote:
This might sound as a stupid question, but how can I pass parameters to a thread?


Not a stupid question at all - see
http://www.pobox.com/~skeet/csharp/threadstart.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #3

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

Similar topics

3
6684
by: Savagesmc | last post by:
Problem Background: I am working on a thin wrapper for the pthreads interface on linux, and have encountered frustrating behavior. The idea is to have a "PosixThread" class that any class can inherit from. A child then only needs to define a virtual "executive" method that has the thread code for the child object, and the threading setup and management are automatic then. The thread creation takes place in the PosixThread base class...
15
2440
by: DanielEKFA | last post by:
Hey there :) Just joined the group, looking for a resolution to a problem I and my three groups members are having (we're students, making an FTP-like server/client as an exam project). We're getting segmentation faults when threading with the following code (this code is not the actual code as it's going to look, but a shortened version for debugging purposes, yet it segfaults the same way as the "real" code):
3
5964
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
6
3258
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the work may not be completed before the event is triggered again I am trying to add critical sections round the producer & consumer parts of
1
977
by: Pug Fugly | last post by:
I have a function that takes in a parameter, and in the function it calls the .BeginInvoke on the worker delegate. I know that the System.AsyncCallback callback function takes in IAsyncResult as the only parameter. But I have a case where I need to pass a parameter from the function that calls the .BeginInvoke to my callback that calls the .EndInvoke. Is this possible? Can I add another parameter to my callback function, or is...
0
1982
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks about in a new application I'm writing. However, when I try to use ThreadStart to do some work in a separate thread from my GUI, the methods Jason described don't seem to catch the exception. Take the following source code: Public Class...
7
339
by: Phill W. | last post by:
Can anyone recommend a good reference on Threading (Framework 1.1)? I'm particularly confused about how to call a method in my "main" thread from another one. - or more, one day ;-) I've tried creating delegates to main thread methods and Invoking them and BeginInvoking them, but I've had some quite /horrendous/ results ... TIA,
7
1457
by: ChrisM | last post by:
Hi, I'm trying to get my head round Async. procedure calls. I have the following code: delegate double GetLabourWIPDelegate(DateTime reqDate); .... GetLabourWIPDelegate getLabourWIPDelegate = new GetLabourWIPDelegate(GetLabourWIPOnDate);
5
10895
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked a bunch of code will get executed. I would like to show a Progress Bar / form in modal/ShowDialog...
2
1684
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've two questions regarding the System.Threading.Thread namespace. First, what is the point of having a ThreadStart parameter? For example... using System.Threading; Thread myThread; myThread = new Thread(new ThreadStart(myFunc)); // This works
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.