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

ThreadStart parameters

How do you pass your parameters to the method you have specified for the
threadstart?

System.Threading.ThreadStart thdStart = new
System.Threading.ThreadStart(MoveMail(new object[] { progress, oItems,
destfolder, objConn });

Where MoveMail has the following parameters :

private void MoveMail(object status)
{
object[] parms = (object[])status;
IProgressCallback callback = (IProgressCallback)parms[0];
Outlook.Items col = (Outlook.Items)parms[1];
Outlook.MAPIFolder destfolder =
(Outlook.MAPIFolder)parms[2];
SqlConnection objConn = (SqlConnection)parms[3];

*** Sent via Developersdex http://www.developersdex.com ***
Mar 21 '07 #1
4 16166
"Mike P" <mi*******@gmail.comwrote in message
news:Oz**************@TK2MSFTNGP06.phx.gbl...
How do you pass your parameters to the method you have specified for the
threadstart?
If you have version 2.0 of the framework you can use
ParameterizedThreadStart instead of ThreadStart. Otherwise, the trick is to
create a class with member variables for the parameters and a start method
without parameters inside the class. Create an object from that class, set
the parameters into the member variables, and start the thread from the
parameter-less start method inside the class. This dumy method can then call
your real start method passing to it the parameters from the member
variables.
Mar 21 '07 #2
I can't find any online examples that actually show the syntax required
for passing parameters to ParameterizedThreadStart. Here is my code
that I have tried that doesn't work :

System.Threading.ThreadStart thdStart = new
System.Threading.ParameterizedThreadStart((MoveMai l), new object[] {
progress, oItems, destfolder, objConn });
System.Threading.Thread thd1 = new
System.Threading.Thread(thdStart);
thd1.Start();
*** Sent via Developersdex http://www.developersdex.com ***
Mar 21 '07 #3
On Mar 21, 9:18 am, Mike P <mike.p...@gmail.comwrote:
How do you pass your parameters to the method you have specified for the
threadstart?
See http://pobox.com/~skeet/csharp/threads/parameters.shtml

Jon

Mar 21 '07 #4
"Mike P" <mi*******@gmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I can't find any online examples that actually show the syntax required
for passing parameters to ParameterizedThreadStart. Here is my code
that I have tried that doesn't work :

System.Threading.ThreadStart thdStart = new
System.Threading.ParameterizedThreadStart((MoveMai l), new object[] {
progress, oItems, destfolder, objConn });
System.Threading.Thread thd1 = new
System.Threading.Thread(thdStart);
thd1.Start();
The ParameterizedThreadStart constructor only takes the delegate to the
start procedure. The parameter itself is passed on the Start method:

System.Threading.ParameterizedThreadStart thdStart = new
System.Threading.ParameterizedThreadStart(this.Mov eMail);
System.Threading.Thread thd1 = new System.Threading.Thread(thdStart);
thd1.Start(new object[] {progress, oItems, destfolder, objConn });

Notice that you can only pass a SINGLE parameter of type object. You can
put anyting inside an object, in this case an array of objects that contains
four elements. Be sure to adjust your MailMove method accordingly (it should
take only one object, not four, and then extract those four objects from
inside the one that you passed).

Mar 21 '07 #5

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

Similar topics

2
by: Victor Hadianto | last post by:
Hello, Can someone explain to me why are we not allowed to have a parameter on the ThreadStart delegate? Does anyone know the design decision behind this feature? This, IMO, rather annoying when...
3
by: Maxime | last post by:
Can put a method with parameter in the ThreadStart ? Thread bThread = new Thread(new ThreadStart(Function(Parameter))); // Not Ok why? another way maybe ? Max
4
by: Fernando Rodríguez | last post by:
Hi, ThreadStart doesn't take any parameters. How can I create threads that take parameters? O:-) Thanks
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
by: StefanS | last post by:
Hi all I'm stuck with a problem and hope someone can provide some hints on what I'm doing wrong. All I want to do ist start a thread which will sleep until a specified time and then wake up...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
2
by: Ryan Liu | last post by:
How a thread is reused in thread pool? Change the context and call theThread.Run() again, and it executes same threadStart? Thanks! Ryan
1
by: robparr | last post by:
Hello, hopefully someone can help me. I am reading and learning steadily about threading and asynchronous programming, which to me sound like the same thing. At the moment I am not sure what the...
1
by: ktrvnbq02 | last post by:
Hi, I recently came to debug some old code that was causing a StackOverflowException. The code in question makes significant use of recursion and with large data structures it exhausted the...
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
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.