Threading and delegates in forms

F

Frank Uray

Hi all

I have a little complicated question ... :))

I have a form with a progressbar, it is started as
a thread correctly with delegate. In the form
I have also a public string "DisplayText" decladed,
it is used to display the text in the progressbar.
This all works fine.

Now I have a separate class with a void, lets call it "Save".
This Save method is also called in the form as a new thread.
The plan is to have a variable (as the calling form) declared
within this Save-Class and filled after declaring the Save-Class.
I want to set the DisplayText variable of the form and I
need to read some controls of the form within the Save method.

It is funny, setting the DisplayText variable works fine ...
But when I try to read out some control of the form, I get the
cross thread exeption.
When I Invoke the Save method, it can read out from the controls,
but my progressbar is not displayed any more ...

I hope you understand what I mean ... :)
What is the best practice of something like this ??

Thanks for any comments.

Best regards
Frank Uray
 
P

Pavel Minaev

I have a little complicated question ... :))

I have a form with a progressbar, it is started as
a thread correctly with delegate. In the form
I have also a public string "DisplayText" decladed,
it is used to display the text in the progressbar.
This all works fine.

Now I have a separate class with a void, lets call it "Save".
This Save method is also called in the form as a new thread.
The plan is to have a variable (as the calling form) declared
within this Save-Class and filled after declaring the Save-Class.
I want to set the DisplayText variable of the form and I
need to read some controls of the form within the Save method.

It is funny, setting the DisplayText variable works fine ...
But when I try to read out some control of the form, I get the
cross thread exeption.
When I Invoke the Save method, it can read out from the controls,
but my progressbar is not displayed any more ...

I hope you understand what I mean ... :)
What is the best practice of something like this ??

Have a look at at Control.Invoke and Control.BeginInvoke methods, and
also at class BackgroundWorker. For your case, you should pay
particular attention to the latter.
 

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