473,406 Members | 2,217 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,406 software developers and data experts.

Threading 2!

I am using the following code, what I need to know is, what do I do if I
want to pass parameters to DoSomeWork? I don't understand how the
callback variable is passed to DoSomeWork.

private void SpawnSomeWork()
{
IProgressCallback callback; // = ???
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(
DoSomeWork ),
callback );
}

private void DoSomeWork( object status )
{
IProgressCallback callback = status as IProgressCallback;

try
{
callback.Begin( 0, 100 );
for( int i = 0 ; i < 100 ; ++i )
{
callback.SetText( String.Format( "Performing op: {0}", i )
);
callback.StepTo( i );
if( callback.IsAborting )
{
return;
}
// Do a block of work
if( callback.IsAborting )
{
return;
}
}
}
catch( System.Threading.ThreadAbortException )
{
// We want to exit gracefully here (if we're lucky)
}
catch( System.Threading.ThreadInterruptedException )
{
// And here, if we can
}
finally
{
if( callback != null )
{
callback.End();
}
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '07 #1
2 1648
I am using the following code, what I need to know is, what do I do if I
want to pass parameters to DoSomeWork? I don't understand how the
callback variable is passed to DoSomeWork.

private void SpawnSomeWork()
{
IProgressCallback callback; // = ???
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(
DoSomeWork ),
callback );
}

private void DoSomeWork( object status )
{
You can change the 'callback' portion of QueueUserWorkItem to an object
array or a custom class, and pass in several parameters, like such:

QueueUserWorkItem(
new WaitCallback(DoSomeWork),
new object[] { callback, param1, param2 }
);

and then parse out the properties in the DoSomeWork function:

private void DoSomeWork( object status )
{
object[] parms = (object[])status;
IProgressCallback callback = (IProgressCallback)parms[0];
int param1 = (int)parms[1];
string param2 = (int)parms[2];
...
-mdb
Mar 16 '07 #2
Mike,

If you're using 2.0 this is super easy. Just use an anonymous method
instead.

private void SpawnSomeWork()
{
object parameter1 = ?;
object parameter2 = ?;
ThreadPool.QueueUserWorkItem(delegate(object state)
{
// Capture local variables.
Console.WriteLine(parameter1);
Console.WriteLine(parameter2);
});
}

Brian

On Mar 16, 10:25 am, Mike P <mike.p...@gmail.comwrote:
I am using the following code, what I need to know is, what do I do if I
want to pass parameters to DoSomeWork? I don't understand how the
callback variable is passed to DoSomeWork.

private void SpawnSomeWork()
{
IProgressCallback callback; // = ???
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(
DoSomeWork ),
callback );

}

private void DoSomeWork( object status )
{
IProgressCallback callback = status as IProgressCallback;

try
{
callback.Begin( 0, 100 );
for( int i = 0 ; i < 100 ; ++i )
{
callback.SetText( String.Format( "Performing op: {0}", i )
);
callback.StepTo( i );
if( callback.IsAborting )
{
return;
}
// Do a block of work
if( callback.IsAborting )
{
return;
}
}
}
catch( System.Threading.ThreadAbortException )
{
// We want to exit gracefully here (if we're lucky)
}
catch( System.Threading.ThreadInterruptedException )
{
// And here, if we can
}
finally
{
if( callback != null )
{
callback.End();
}
}

}

*** Sent via Developersdexhttp://www.developersdex.com***

Mar 16 '07 #3

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
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
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:
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.