473,666 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doing some background work occasionally

I have an application that needs to perform some background work, i.e.
Logging, wich must not block the main thread. How would I basically
design such a scenario? It is obvious that I should do that on an extra
thread but I think it is a bad idea to spawn a new thread everytime a
log-message is written and let it die once the message was written. But
how do I keep a thread alive and trigger the log-messages and pass the
string to that thread? Also, should I give that thread a lower
priority? Maybe there is a best practice for this kind scenario?

Oct 26 '06 #1
33 1462
"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
[...] But
how do I keep a thread alive and trigger the log-messages and pass the
string to that thread? Also, should I give that thread a lower
priority? Maybe there is a best practice for this kind scenario?
I believe that the .NET worker thread pool will address what you want.

(non-existent newsgroup removed from Newsgroups: field)
Oct 26 '06 #2
Thank you for that tip. One questions though:

1. If an exception occurs inside a thread of a queued workitem how can
I rethrow that exception on the main thread?

Peter Duniho wrote:
"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
[...] But
how do I keep a thread alive and trigger the log-messages and pass the
string to that thread? Also, should I give that thread a lower
priority? Maybe there is a best practice for this kind scenario?

I believe that the .NET worker thread pool will address what you want.

(non-existent newsgroup removed from Newsgroups: field)
Oct 26 '06 #3
I would look at a queue, accessed (for instance) via a static/singleton
pattern. Access to the queue would be sync'd. callers would
* lock
* enqueue string
* check count; if ==1 then Monitor.Pulse as have just restarted the Q
* unlock
The (single) logging thread (started in the static/singleton ctor) would
* lock
* check count
* if == 0
* Monitor.Wait
* unlock
* back to the top
* else
* dequeue
* unlock
* log
* back to the top

This way, you have a single thread that stays alive, but doesn't hammer the
CPU while the Q is empty; callers only block to enqueue, not to log.
You might also add something to allow graceful teardown of the thread
(without Thread.Abort), and I'd probably make the logging thread a
background thread anyway...

Marc
Oct 26 '06 #4
That sounds more like an async callback... in which case, create a delegate
to a method, and call BeginInvoke on the delagate, also providing the
callback delegate (to a method e.g. on your form). In the callback, you can
then use the Form's BeginInvoke etc to get back to the UI thread.

Marc
Oct 26 '06 #5
"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Thank you for that tip. One questions though:

1. If an exception occurs inside a thread of a queued workitem how can
I rethrow that exception on the main thread?
I'm not really sure what that would mean, exactly. Since the exception
doesn't occur on the main thread in that case, and the main thread would be
happily going along executing its own code, at what point would you have the
main thread get interrupted with the exception? How should the main thread
be expected to recover from the exception, given that the exception would
have nothing to do with what the main thread actually was doing?

IMHO, a much better approach would be to come up with some signaling
mechanism that your threads can use to notify the main thread of an error,
and have the main thread check that now and then. Or maybe even better,
have the worker thread, upon catching a thrown exception, use Invoke or
BeginInvoke to run a delegate on the main thread, causing the main thread to
process the error in whatever way you feel is appropriate.

Pete
Oct 26 '06 #6
The scenario is this: as soon as any excpetion occurs in one of the
threads the thread will be finished (there is one big try{}catch{}
inside the thread's callback. In case an exception occured the main
thread must be resonsible of handling the excpetion. This is a
requirement by design of the main application.
Or maybe even better,
have the worker thread, upon catching a thrown exception, use Invoke or
BeginInvoke to run a delegate on the main thread, causing the main thread to
process the error in whatever way you feel is appropriate.
This sounds like a good idea for my scenario. I now need to find out
how to run the delegate on the main thread an pass the exception
correctly.

Peter Duniho wrote:
"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Thank you for that tip. One questions though:

1. If an exception occurs inside a thread of a queued workitem how can
I rethrow that exception on the main thread?

I'm not really sure what that would mean, exactly. Since the exception
doesn't occur on the main thread in that case, and the main thread would be
happily going along executing its own code, at what point would you have the
main thread get interrupted with the exception? How should the main thread
be expected to recover from the exception, given that the exception would
have nothing to do with what the main thread actually was doing?

IMHO, a much better approach would be to come up with some signaling
mechanism that your threads can use to notify the main thread of an error,
and have the main thread check that now and then. Or maybe even better,
have the worker thread, upon catching a thrown exception, use Invoke or
BeginInvoke to run a delegate on the main thread, causing the main thread to
process the error in whatever way you feel is appropriate.

Pete
Oct 26 '06 #7
bonk wrote:
Or maybe even better,
have the worker thread, upon catching a thrown exception, use Invoke or
BeginInvoke to run a delegate on the main thread, causing the main thread to
process the error in whatever way you feel is appropriate.

This sounds like a good idea for my scenario. I now need to find out
how to run the delegate on the main thread an pass the exception
correctly.
Use Control.Invoke or Control.BeginIn voke.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
details.

Jon

Oct 26 '06 #8
Sorry, I forgot to mention that it a console application. How would I
do it in this case? Invoke a delegate on a main thread from another
thread?
Jon Skeet [C# MVP] wrote:
bonk wrote:
Or maybe even better,
have the worker thread, upon catching a thrown exception, use Invoke or
BeginInvoke to run a delegate on the main thread, causing the main thread to
process the error in whatever way you feel is appropriate.
This sounds like a good idea for my scenario. I now need to find out
how to run the delegate on the main thread an pass the exception
correctly.

Use Control.Invoke or Control.BeginIn voke.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
details.

Jon
Oct 26 '06 #9
Check out the ThreadPool class:
http://msdn2.microsoft.com/en-us/lib...hreadpool.aspx

bonk wrote:
I have an application that needs to perform some background work, i.e.
Logging, wich must not block the main thread. How would I basically
design such a scenario? It is obvious that I should do that on an extra
thread but I think it is a bad idea to spawn a new thread everytime a
log-message is written and let it die once the message was written. But
how do I keep a thread alive and trigger the log-messages and pass the
string to that thread? Also, should I give that thread a lower
priority? Maybe there is a best practice for this kind scenario?
Oct 26 '06 #10

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

Similar topics

5
11084
by: proximus | last post by:
Hi, I am trying to change the background of table TD's. The problem is that I have no access to the HTML code. SO I am trying to alter this using Javascript/DOM in an external .js file. I have tried lot's of things and spent hours on this, I thought I might give it a try here. One of the TD's uses HTML to set the background, the other one uses CSS styling:
33
1993
by: bonk | last post by:
I have an application that needs to perform some background work, i.e. Logging, wich must not block the main thread. How would I basically design such a scenario? It is obvious that I should do that on an extra thread but I think it is a bad idea to spawn a new thread everytime a log-message is written and let it die once the message was written. But how do I keep a thread alive and trigger the log-messages and pass the string to that...
0
8454
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
8362
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
8878
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...
0
8785
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5671
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
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
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.