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

Threads and loops

Is anyone aware of any issues running a loop inside a thread? I have
a thread running and inside of it I do a foreach on each row of a
datagrid and then i send an event back to the form to insert a row in
a text box. For each row on the datagrid, I add a line to the text
box... textbox += data.

The problem is my thread and the loop seem to have a problem where the
textbox has a good line or two and then every line after it is the
same. If I place a Sleep within the loop (ie. sleep(25)) then it
works fine.

Anyone encounter something weird like this?

Nov 1 '07 #1
4 1238
Are you calling Invoke on the textbox and passing a delegate in to make
sure that the call to update the textbox happens on the UI thread? You can
not make changes to the UI thread from other threads (hence the reason for
the Invoke method).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Soulless" <dg*******@gmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...
Is anyone aware of any issues running a loop inside a thread? I have
a thread running and inside of it I do a foreach on each row of a
datagrid and then i send an event back to the form to insert a row in
a text box. For each row on the datagrid, I add a line to the text
box... textbox += data.

The problem is my thread and the loop seem to have a problem where the
textbox has a good line or two and then every line after it is the
same. If I place a Sleep within the loop (ie. sleep(25)) then it
works fine.

Anyone encounter something weird like this?

Nov 1 '07 #2
On Nov 1, 2:53 pm, Soulless <dgmsal...@gmail.comwrote:
Is anyone aware of any issues running a loop inside a thread? I have
a thread running and inside of it I do a foreach on each row of a
datagrid and then i send an event back to the form to insert a row in
a text box. For each row on the datagrid, I add a line to the text
box... textbox += data.

The problem is my thread and the loop seem to have a problem where the
textbox has a good line or two and then every line after it is the
same. If I place a Sleep within the loop (ie. sleep(25)) then it
works fine.

Anyone encounter something weird like this?
That sounds like you're accessing the UI from a worker thread. You
mustn't do this.

See http://pobox.com/~skeet/csharp/threads/winforms.shtml

Jon

Nov 1 '07 #3
On Nov 1, 10:05 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 1, 2:53 pm, Soulless <dgmsal...@gmail.comwrote:
Is anyone aware of any issues running a loop inside a thread? I have
a thread running and inside of it I do a foreach on each row of a
datagrid and then i send an event back to the form to insert a row in
a text box. For each row on the datagrid, I add a line to the text
box... textbox += data.
The problem is my thread and the loop seem to have a problem where the
textbox has a good line or two and then every line after it is the
same. If I place a Sleep within the loop (ie. sleep(25)) then it
works fine.
Anyone encounter something weird like this?

That sounds like you're accessing the UI from a worker thread. You
mustn't do this.

Seehttp://pobox.com/~skeet/csharp/threads/winforms.shtml

Jon
Hi, yeah I am utilizing a brThread business rule class that handles
the threading and am using invoke... however, I will check to see if
it is a worker thread which could be the problem.... just seems like
my thread is out of control and debugging doesn't really help me.
Again, adding a sleep in there fixes it, but is not really a
solution. Thanks!

Nov 1 '07 #4
On Nov 1, 3:21 pm, Soulless <dgmsal...@gmail.comwrote:
That sounds like you're accessing the UI from a worker thread. You
mustn't do this.

Hi, yeah I am utilizing a brThread business rule class that handles
the threading and am using invoke... however, I will check to see if
it is a worker thread which could be the problem.... just seems like
my thread is out of control and debugging doesn't really help me.
Again, adding a sleep in there fixes it, but is not really a
solution. Thanks!
Are you using Invoke for *all* the access to the UI though? Bear in
mind that accessing the datagrid still counts as accessing the UI,
even though it's just for reading.

Jon

Nov 1 '07 #5

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

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
3
by: Andreas Müller | last post by:
i need two loops that run forever. one of it receives data and stores it to a vector. the other one writes the elements of the vector to the disk. this means the vector is a receiver buffer. How...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
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.