multi-threading problem...help

T

trint

Ok,
I have a thread that I start when user clicks to start:

ThreadStart myThreadDelegate = new ThreadStart
ThreadFunction1.getOneAtATime);
Thread thr1 = new Thread(myThreadDelegate);
thr1.Start();

The process started contains a Very large function that is in a
continuous loop (here is part of it just to demonstrate my problem):

foreach (string invoice in Class1.invoiceHolder)
{
//Constantly show a status bar
//Constantly update image control of "if tray has paper or not".
//Constantly update labels displaying info about Invoices
}

Ok,
This will need to run for days at a time in this loop...HOWEVER, I had
it in the Thread, as mentioned, and the app would just quit.
I had posted a question to this before and got the answer that
"Controls created by the initializing thread should not be touched by
processes started in other threads (creates undesirable results". Ok,
I get that, but when this thread I start runs, it MUST give updates to
users.
Ok, I have looked at Control.Invoke and don't get it. I mean that, I
couldn't figure out how to use it (mental block). Is that the answer to
my problem? (Problem being, that if I don't start this giant process in
a separate thread, when it starts in this loop, the computer starts to
crawl).
Help is appreciated.
Thanks,
Trint
 
M

Mattia Adami

I whould start two thread, one that do the work, and one (using timers)
that updates the status to the user every xxx time.

But... why don't you do a service that comunicate with a front-end with
QueueMessage or other tecnics?
 
T

trint

Not a bad idea, but they have already approved the look of the controls
on the forms.
I do need to have this large function in a new thread though.
Thanks.
 
A

Alex Passos

Lock the control in your worker thread, as stated in the article provided at
pobox.com. That will force all of your UI updates through a single thread.
 
J

Jon Skeet [C# MVP]

Alex Passos said:
Lock the control in your worker thread, as stated in the article provided at
pobox.com. That will force all of your UI updates through a single thread.

No it won't! Locking the control doesn't change what thread method
calls go through - you need to use Control.Invoke.
 
T

trint

Jon,
I am trying to figure out how to apply your info on threading with
controls to my application...I may need more help...are you still
there?
Thanks,
Trint
 
J

Jon Skeet

I am trying to figure out how to apply your info on threading with
controls to my application...I may need more help...are you still
there?

I'm at work, and only very occasionally checking the group, but I'll be
posting more tonight (UK time) and in the early morning. Put it this
way - I'm almost always bound to reply within 24 hours :)

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

Top