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

Multithread

I'm trying to write a multithread application:

it will get different kind of data from DB, my intention was to create
different thread for each query.
I tryed to use the AsyncHelper.FireAndForget method u can easily find
on this goup but i wasn't lucky: I was able to crate threads but not to
get back results....

Any idea/modification of AsyncHelper.FireAndForget will be gratly
appreciated.

Basicly I need to call a method with parameters (array of parameters)
and get back array of sorted list or similar...

Thanks in advance.

Mar 23 '06 #1
2 1225


da*************@gmail.com wrote:
Any idea/modification of AsyncHelper.FireAndForget will be gratly
appreciated.

Basicly I need to call a method with parameters (array of parameters)
and get back array of sorted list or similar...


You will need a method of synchronizing so the place where the result is
needed can wait for the results, I would suggest adding a WaitHandle to
the data-structure passed between the working thread and users.

public abstract class Operation<T> {
protected readonly ManualResetEvent done =
new ManualResetEvent(false);
public WaitHandle Done { get { return done; } }
public ResultType Result {
get {
Done.WaitOne();
return result;
}
}
protected T result;
protected abstract run();
public void Run() {
if ( Done.WaitOne(TimeSpan.Zero, false) )
throw new InvalidOperation("Already ran once");
try {
run();
} finally {
done.Set();
}
}
}

Use this to define operations, like:

public class PlusOperation: Operation {
int x, y;
public PlusOperation(int x, int y) {
this.x = x;
this.y = y;
}
protected override run() { result = x+y; }
}

which you can use as:

int count = 3;
Operation[] operations = new Operation[count];
WaitHandle[] waitfor = new WaitHandle[count];
for ( int i = 0; i < count; ++i ) {
Operation op_i = new PlusOperation(i,-i);
operations[i] = op_i;
waitfor[i] = op_i.Done;
new Thead(new ThreadStart(op_i.Run)).Start();
// or you could use fireandforget
}
WaitHandle.WaitAll(waitfor); // stop untill all are done
int sum;
for ( int i = 0; i < operations.Length; ++i )
sum += operations[i].Result;
--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 23 '06 #2
Another option is to supply the thread with a delegate as an additional
parameter. Once the thread has finished its work then it will call the
delegate, almost similar to raising an event. The query results could then be
passed as a parameter of the delegate.

Yet another option would be to define an interface IQueryListener, and
implement a class that implements that interface. The interface would contain
one method, e.g. OnQueryComplete() and would pass the results as a parameter.
Then you would instantiate that class and pass the reference to that instance
to the thread when it is started.

Kind rgards,
Manfred.
---
Manfred Lange.
csUnit Lead Developer
http://www.csunit.org
"da*************@gmail.com" wrote:
I'm trying to write a multithread application:

it will get different kind of data from DB, my intention was to create
different thread for each query.
I tryed to use the AsyncHelper.FireAndForget method u can easily find
on this goup but i wasn't lucky: I was able to crate threads but not to
get back results....

Any idea/modification of AsyncHelper.FireAndForget will be gratly
appreciated.

Basicly I need to call a method with parameters (array of parameters)
and get back array of sorted list or similar...

Thanks in advance.

Mar 24 '06 #3

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

Similar topics

12
by: haptiK | last post by:
Hello, can i use php to multithread mail() or something similar? in my company i need to send multiple copies of email to a few hundred ppl affilated and on my list. instead of calling mail...
4
by: dbmethods | last post by:
Could someone give a hint on how to do multithread programming with PHP scripting here? Thanks
0
by: Alice | last post by:
Hello I have four multithread windows applications(vb.net) interacting and running on the same machine(windows 2000 with .net framework 1.0). All of them start a new thread each time Filewatcher's...
0
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in...
3
by: QQ | last post by:
I am new here and got lost on a multithread C++ system source codes Thanks a lot!
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
2
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting...
0
by: fred | last post by:
I need some help in trying to understand how to make myCollection (inherited from CollectionBase) multithread safe. Taking my implementation of the Add Sub and a readonly property Item. Public...
6
by: jmartin | last post by:
Hi, I have made a multithread version of a program (load a file into database), and with two processors I get the double of time in the multithread than in the process (unithread) version. I...
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
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
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
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
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.