.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Threading loop

Posted By:      Posted Date: September 17, 2010    Points: 0   Category :.NET Framework
Hi,   I am creating multiple threads in a foreach loop as follows:  foreach (KeyValuePair<int, dummyDetails> dummies in dummyDetailsList)   {                     ThreadStart starter = delegate { connectLoop(dummies.Key, dummies.Value); };                     Thread thread = new Thread(starter);                     thread.Start();                       }  public void connectLoop(int deviceID, dummyDetails data)  {                 bool isLooping = true;                 lock (_locker)                 {                     isLoopingList.Add(deviceID, isLooping);                 } //Som

View Complete Post

More Related Resource Links

Microsoft's Collaborative Application Markup Language (CAML) Loop

discovered an interesting error recently while working with Microsoft's Collaborative Application Markup Language (CAML) that, surprisingly, had received no ink. Partly what surprises me about this is that the error may require you to rewrite large sections of your code if you haven't previously considered this SharePoint limitation. I'll start with some context, but first of all the error is:

Hello World to C# Threading Part 1

In this post we will see how we can implement simple threading. Why do we need threading? When you want you application to do more then one thing at same time, then you need to implement threading. One usual and common scenario would be when your application needs to get data from some other resource like web services and you don't want you application GUI to be freeze during this time. In that sort of case you will run the code to access web services in separate thread so your application GUI won't freeze. The System.Threading namespace .NET provide interfaces to implement multi threading.

Threading in C#

What is Threading?
-Parallel execution of code pieces on CPU, commonly compiled codes started in a single thread but it is possible to create multi-threads within a program.


Multi-core processors are new standards on the user's hardware. By taking advantage of the multi-processing capabilities of these machines you can achieve significant performance upgrade with some minor changes in your code. When an application does not make use of threading techniques it is wasting valuable resources. This tutorial will cover the basic threading concepts and threading methods and properties.

Multi-threading in .NET: Introduction and suggestions

This article uses the C# type shorthands throughout - int for Int32 etc. I hope this makes it easier for C# developers to read, and won't impede any other developers too much. It also only talks about the C# ways of declaring variables to be volatile and locking monitors. Developers using other languages can find the equivalents in their own preferred environment, I'm sure.

Multiple Paramatized Threading issue


I need the funcitonality to have two methods run concurrently of each other and the parent thread (aka the method that makes these two calls) to halt until BOTH are done. After the threads are done, I need to perform logic on the returns of the method, so it is essential for both of them to be complete before the parent thread continues. I have searched the internet and tried varous attacks, and I always come up with the same result, this being the parent thread continues operation after the first thread finishes, leaving the second thread running.  Can someone show me a quick example of how I can do this. I have tried checking states of the threads, halting the threads, but I always end up with the same thing. Any help would be great. Thanks.

for each loop



Need help in for each loop. i'm poor in for each loop. i wanna create a for each loop that can give me 2 value in each rows.

i create a for loop.. but is a mulitple rows. Anyone can help ?

here is my code.

Dim x As Integer, y As Double, x1 As Integer

        Dim d As Date, prevD As Date
        Dim d1 As TimeSpan

        temp_sec = ac.getTempTime(ddl_date.SelectedValue, ddl_id.SelectedValue)

        For I As Int32 = 0 To temp_sec.Tables(0).Rows.Count - 1
            If I Mod 1 = 0 Then
                ' To prevent list out of bounds
                If I + 1 < temp_sec.Tables(0).Rows.Count Then
                    d = temp_sec.Tables(0).Rows(I).Item(0)
                    prevD = temp_sec.Tables(0).Rows(I + 1).Item(0)
                    d1 = prevD - d
                    x = d1.TotalSeconds
                    x = x1 + x
                    y = temp_sec.Tables(0).Rows(I).Item(1)
                    list.Add(x, y)
                    x1 = x
                End If
            End If

Make It Snappy: Juice Up Your App with the Power of Hyper-Threading


In this article, the author explores the hyper-threading technology found on newer Intel Pentium 4 processors and demonstrates how adding parallelism to your code can improve performance on hyper-threaded machines. He covers advanced optimizations for hyper-threading and shows a number of useful patterns. Code samples are in C#, but you can apply the same principles in other languages as they apply to both managed and unmanaged applications.

Yaniv Pessach

MSDN Magazine June 2005

Threading: Break Free of Code Deadlocks in Critical Sections Under Windows


Critical sections, a mechanism that prohibits more than one thread at a time from executing a particular section of code, is a topic that has not received much attention and thus tends not to be well understood. A solid understanding of critical sections in Windows can really come in handy when you need to track down multithreading performance issues in your code. This articles delves under the hood of critical sections to reveal information useful in finding deadlocks and in pinpointing performance problems. It also includes a handy utility program that shows all of your critical sections and their current states.

Matt Pietrek and Russ Osterlund

MSDN Magazine December 2003

Visual Basic .NET: Tracing, Logging, and Threading Made Easy with .NET


Visual Basic has always been a language of innovation, but at the same time it's left its proponents wanting certain high-level features. With the upcoming version, Visual Basic .NET, the language takes advantage of the Microsoft .NET Framework to provide several new features that will be vital to developers. This article begins by discussing tracing and error logging in Visual Basic .NET. Next, you'll see how to write a Windows service without third-party help. Monitoring the file system, a previously daunting task, is also covered. Finally, the author ties these concepts together with a sample application that waits for XML files to be deposited in a directory and then imports them into SQL Server database.

Yasser Shohoud

MSDN Magazine July 2001

System.Threading AND email


Hi guys,

I really need some help (and also some code example), in how to use System.Threading to send emails.

The issue is:

a) I have a function that sends emails;

b) But I don't want to keep the customers waiting for the email being sent;

c) so, I figure it out that the System.Threading is the best way to do that; But I don't know how exactly I cant use it.

PLEASE, i'm VB - not C# - developer, and I really appreciate help in that language.

I can't use Windows Services or SQL Jobs...

Could you help me on this?



Loop for every record


Hi guys.

I've got a simple table with an ID, a NAME and an ARCHIVE booleen.

I want to write piece of code that goes through each record and...

If ARCHIVE is false and a folder hasn't been created for it, it will create it and go to the next record.

If ARCHIVE is false and a folder has been created for it, it will do nothing and go to the next record.

If ARCHIVE is true the whole folder will be moved to another folder and go to the next record.

When all records have been checked, the code will end.

I was thinking of using a SQL count to find out how many records there were and making a loop that starts at record one. runs the folder checking/creating/archiving code then +1 until the loop ends but I couldn't work out how to assign the query results to an integer...

I'm not asking for anyone to write the code. I think I probably already know 80% of the theory behind doing this I just can't glue it all together. I know you can do the folder stuff with the System.IO Directory Class...

Anyway, all input is welcome. Cheers guys!


asp.net c# DELEGATE THREAD: Multi threading



In my asp.net web application i have lengthy excution process like word merge. I want to merge the word documents and send to client in a procedure. Basically i am looking for run and forget kind of asynchronous process.

In my for loop i tried to create delegate and rstrat. like this

filenames is a string array, creating in the same loop and assiging values before calling delegate.


MailSender mail = new MailSender();



TestDelegate testdele = new

Loop with DataReader


Hi.  I am having a really hard time with this code trying to loop through a datareader, and then enter the data into another table.  The logic is that I get 2 columns path and document_no from an Oracle Database.  I then need to check if the file in the path column is a valid path.  If it is not valid, it should be inserted into a SQL Server 2005 database.  My code is below, can anyone help?  I'm getting a headache from it.  If any further info is needed just let me know.  Thanks. 

Do While oraReader.Read()
                path1 = oraReader("Path")
                docno = oraReader("Document_No")
                If (System.IO.File.Exists(path1)) Then                    
                    sqlCmd = New SqlCommand("INSERT INTO CHECKLINKS1 (DOCUMENT_NO) VALUES (@doc)", conn)
                    sqlCmd.Parameters.AddWithValue("@doc", docno)
                End If


how to get current loop order number in "for each" ?

For Each item As pages.tbl1Row In pagesdim x as integerx = ?????response.write("current loop is :" & x )               Nextwhat code i need to replace with "?????" to get current loop order?


I'm currently experiencing a spinloop wait type with command of Unknown Token. The issue occured when I copied DB filed from another server and attempted to attach it -using the SSMS wizard - to the affected DB. The attach failed due to an OS permission error but the database engine - for no clear reason - mounted the DB in a Recovery/Read Only mode; the transaction did not rollback. This rogue DB cannot be altered, dropped, detached, etc and KILL command fails against the culprit SPID. In the DB log there is a stack dump at Location:  "logmgr.cpp":5527, * Expression:  !(minLSN.m_fSeqNo < lfcb->lfcb_fSeqNo). And the errors: Error: 17066, Severity: 16, State: 1; Error: 3624, Severity: 20, State: 1. The logs continue thereafter with the following: Process 29:0:0 (0xe48) Worker 0x0000000015D6E1C0 appears to be non-yielding on Scheduler 18. Thread creation time: 12927361417467. Approx Thread CPU Used: kernel 46 ms, user 4040 ms. Process Utilization 7%. System Idle 88%. Interval: 132567 ms. I also discovered that sql server agent will not run jobs although the serive is running. Its logs read: 2010-08-27 08:36:26 - ? [393] Waiting for SQL Server to recover databases... 2010-08-27 10:41:59 - ? [131] SQLSERVERAGENT service stopping due to a stop request from a user, process, or the OS... 2010-08-27 10:42:00 - ? [098] SQLServerAgent term
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend