Threads and loops

  • Thread starter Thread starter Soulless
  • Start date Start date
S

Soulless

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?
 
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).
 
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
 
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!
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top