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

firing Multi-Threads sometimes misses

I am new to multi-threading. Here is my scenario:

foreach (<file in a certain folder>)
new Thread((ThreadStart)(delegate { processFile(<file>); })).Start();

sometimes misses firing some threads to process files. It misses
firing different threads every time I run it. I suppose it all
depends on the time-slice it is getting at that moment (or I may be
way off base here). What I mean by "misses" is that the "foreach"
loop appears to have executed the thread-start line but the thread
never actually starts.

Can someone please tell me what I can add to the code to make sure
that a thread is started and not missed?

Checking a flag that a thread sets when it first starts is an obvious
solution, but I am looking for a more language native way (if one
exists).

Your help is greatly appreciated.
jake
Jul 23 '08 #1
4 1251
I've never seen a thread not start. I have seen a thread encounter an
exception and die silently. Are your threads starting and dying
without you noticing? Maybe put a big try/catch block in the thread
start routine and log unhandled exceptions? To answer your question,
this may work:

if ( thread.ThreadState == ThreadState.Unstarted ) { freak out... }
I'm not here to critique your implementation, but starting an
unbounded number of threads can kill an application. I've done it by
accident. Can you feed a list of tasks to a single worker thread or
the thread pool instead?

On Jul 23, 10:05 am, jake <jakedim...@gmail.comwrote:
I am new to multi-threading. Here is my scenario:

foreach (<file in a certain folder>)
new Thread((ThreadStart)(delegate { processFile(<file>); })).Start();

sometimes misses firing some threads to process files. It misses
firing different threads every time I run it. I suppose it all
depends on the time-slice it is getting at that moment (or I may be
way off base here). What I mean by "misses" is that the "foreach"
loop appears to have executed the thread-start line but the thread
never actually starts.

Can someone please tell me what I can add to the code to make sure
that a thread is started and not missed?

Checking a flag that a thread sets when it first starts is an obvious
solution, but I am looking for a more language native way (if one
exists).

Your help is greatly appreciated.
jake
Jul 23 '08 #2
On Jul 23, 5:05*pm, jake <jakedim...@gmail.comwrote:
I am new to multi-threading. *Here is my scenario:

foreach (<file in a certain folder>)
* * * * new Thread((ThreadStart)(delegate { processFile(<file>); })).Start();

sometimes misses firing some threads to process files. *It misses
firing different threads every time I run it. *I suppose it all
depends on the time-slice it is getting at that moment (or I may be
way off base here). *What I mean by "misses" is that the "foreach"
loop appears to have executed the thread-start line but the thread
never actually starts.
I strongly suspect that's not true. I suspect what *actually* happens
is that some files are processed by two different threads.

Change your code to take a local copy (within the foreach loop) of the
iteration variable, and use *that* in your anonymous method. Otherwise
the iteration variable itself is captured, and that may well have
moved onto the next value before the thread starts.

Jon
Jul 23 '08 #3
Thank you Jon. That makes sense. There are some sparse but strange
log file entries (during file processing) that I can now attribute to
the cause you just mentioned that I, admittedly, did not think about.
I will implement the local variable copy solution you mentioned.
Regards,
jake
On Jul 23, 12:46 pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 23, 5:05 pm, jake <jakedim...@gmail.comwrote:
I am new to multi-threading. Here is my scenario:
foreach (<file in a certain folder>)
new Thread((ThreadStart)(delegate { processFile(<file>); })).Start();
sometimes misses firing some threads to process files. It misses
firing different threads every time I run it. I suppose it all
depends on the time-slice it is getting at that moment (or I may be
way off base here). What I mean by "misses" is that the "foreach"
loop appears to have executed the thread-start line but the thread
never actually starts.

I strongly suspect that's not true. I suspect what *actually* happens
is that some files are processed by two different threads.

Change your code to take a local copy (within the foreach loop) of the
iteration variable, and use *that* in your anonymous method. Otherwise
the iteration variable itself is captured, and that may well have
moved onto the next value before the thread starts.

Jon
Jul 23 '08 #4
Jon,
That was the cause. I created a local copy of the iteration variable
and I passed that to the anonymous method, just like you said, and it
worked.
Thanks again.
jake
On Jul 23, 12:46 pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 23, 5:05 pm, jake <jakedim...@gmail.comwrote:
I am new to multi-threading. Here is my scenario:
foreach (<file in a certain folder>)
new Thread((ThreadStart)(delegate { processFile(<file>); })).Start();
sometimes misses firing some threads to process files. It misses
firing different threads every time I run it. I suppose it all
depends on the time-slice it is getting at that moment (or I may be
way off base here). What I mean by "misses" is that the "foreach"
loop appears to have executed the thread-start line but the thread
never actually starts.

I strongly suspect that's not true. I suspect what *actually* happens
is that some files are processed by two different threads.

Change your code to take a local copy (within the foreach loop) of the
iteration variable, and use *that* in your anonymous method. Otherwise
the iteration variable itself is captured, and that may well have
moved onto the next value before the thread starts.

Jon
Jul 24 '08 #5

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

Similar topics

3
by: arc | last post by:
I have a server object that performs zipcode validation based on zipcode entered in the web form. Ideally this should work as follows: Once the zip code is entered in the edit box it should...
12
by: qaz | last post by:
For some reason my global.asa file is not firing. I have it located in the root of my website (e.g., wwwroot\mywebsite\global.asa) and I have the web site configured as an "application" in IIS. ...
2
by: swathi | last post by:
----------------------------------------------------------------------- A poll associated with this post was created, to vote and see th results, please visit...
6
by: Rod Snyder | last post by:
I'm trying to set up a page with an asp.net link button that would send a user to a certain page and on page load execute a specific stored procedure tied to the button on the previous page. The...
3
by: Mike | last post by:
Hi, I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-( namespace...
0
by: John Grandy | last post by:
On a .aspx page I have seen commercial controls that serve as replacements for the <asp:Button> control that immediately disable the button on form submission so that accidental double-clicks (or...
3
by: Dean Earley | last post by:
Has anyone had the NodeClick event not firing in VB6 with the VB5 Treeview control? It fires correctly when using the cursor keys to select nodes but there is nothing when i click a node. It...
19
by: furiousmojo | last post by:
This is a strange problem. I have a project where the contents of global.asax application_error are not firing. It is an asp.net 2.0 application using web application projects. I have another...
6
by: Robert | last post by:
Quick question about the visible property on a form control. I have a label that displays a message if a certain criteria is met. By default the label is visible. I want access to compare a...
11
by: PokerMan | last post by:
Hi I have a situation where a method is fired from receiving a message from my server. On receiving this message it triggers a visual animation. So what happens when the server sends say 3...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.