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

Help with thread implementation

Hi!

I have a directory and file search class that I would like to call in an
asynch manner. The class is implemented such that whenever a matching item
is found (directory, file, file content) an appropriate event is triggered.
The application/class using it subscribes to the events (to update UI or log
to file or whatever) and the event args include a Cancel parameter that they
can set to true to abort the search.

I have a couple of questions:
1. Where do I implement the threading? In the app/class calling the
filesearcher class or in the filesearcher class directly?
2. How do I make the events synchronuos? I need to wait for their completion
since I need the Cancel parameter.
3. Is there an alternative to using a Cancel parameter in the event args?
4. If implementing threading in the filesearcher class is the best way to
proceed, can I still maintain the synchronuos behavior already implemented?

Coming from the Delphi world, I have had little problems getting to grips
with .Net and C# (there are a lot of similarities), but .Net threading is
one of those things that I can't quite get my head around so I am looking
forward to your suggestions and ideas.

--
Regards, Peter
Nov 5 '06 #1
2 1333
Oh, BTW I have read Jon Skeet's excellent article on threading
(http://www.yoda.arachsys.com/csharp/threads/) but didn't really find a
solution for my problem (or maybe I didn't understand it :))

--
Regards, Peter
Nov 5 '06 #2
"Peter Thornqvist" <pe*************@gmail.comwrote in message
news:ur**************@TK2MSFTNGP04.phx.gbl...
1. Where do I implement the threading? In the app/class calling the
filesearcher class or in the filesearcher class directly?
If you want the class to present an async interface to any client, then put
the threading in the class itself. If not, then wrap the class with some
threading functionality in the client.

You can do it either way. Personally, I'd go ahead and build the threading
into the class. That makes things a little cleaner and reusable. But
there's no mandate that it be done one way or the other.
2. How do I make the events synchronuos? I need to wait for their
completion since I need the Cancel parameter.
When your background thread calls an event handler, that happens on the
background thread, synchronously with anything else that happens in the
thread.

There are other ways to run an event handler, but this is the way it happens
in the simplest case. So your background thread would simply call the event
handler normally, check the Cancel parameter on return and behave
accordingly.

Of course, this means that the handlers registered with your class should
not take too much time to do anything. They should pretty much just note
the information as desired, set the Cancel flag if necessary, and return as
soon as possible. Otherwise, the entire background thread gets held up.
3. Is there an alternative to using a Cancel parameter in the event args?
Depends on what behavior you want. But sure, there's lots of other ways you
could inform the background thread to stop searching, if that's what you
want.
4. If implementing threading in the filesearcher class is the best way to
proceed, can I still maintain the synchronuos behavior already
implemented?
What synchronous behavior are you trying to maintain? You need not require
the class client to use the async behavior if you don't want do. Making it
optional should not be difficult, if that's what you mean.

Pete
Nov 5 '06 #3

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

Similar topics

2
by: Donny | last post by:
We have an object that is to be shared across different multi-threaded nodes in a distributed system. It should be possible to lock that object using any thread, and the other node which shares...
8
by: Maurice LING | last post by:
Hi, I just have a simple question about threads. My classes inherits from threading.Thread class. I am calling threading.Thread.run() method to spawn a few threads to parallel some parts of my...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
4
by: Leonardo Hyppolito | last post by:
Hello, I am trying to write a multithread program that simulates producers and consumers. My program can have many producers and many consumers (each in a separate thread). It has a storage...
19
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
4
by: Lighter | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; using namespace boost; void hello() { cout << "Hello world, I'm a thread!" << endl; }
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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?

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.