Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Accessing form control in separate thread.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 21 Jun 2007 21:34 GMT
I have a Windows Forms application with a ProgressBar control on a form.

In that form, I create a new process to run another program. I register an
event handler for the Exited() event.

I start the progressbar scrolling (Marquee) and start the process.

When the process exits, I want to stop the progress bar. However, when I try
to access the ProgressBar control, I get a System.InvalidOperationException.
"Cross-thread operation not valid: Control 'progressBar1' accessed from a
thread other than the thread it was created on."

What can I do in this case to access the progressBar?

Thanks,
Jeff
AlexS - 21 Jun 2007 21:39 GMT
Check InvokeRequired property for control and related Multithreaded Control
Sample in MSDN

HTH
Alex

>I have a Windows Forms application with a ProgressBar control on a form.
>
[quoted text clipped - 14 lines]
> Thanks,
> Jeff
Jeff - 21 Jun 2007 22:04 GMT
Hi Alex.

Thanks for the info. How do I go about finding this "Multithreaded Control
Sample" in MSDN. I have read up on the Invoke, BeginInvoke etc methods but
really need to see a C# example to figure this out.

Thanks,
Jeff

> Check InvokeRequired property for control and related Multithreaded Control
> Sample in MSDN
[quoted text clipped - 20 lines]
> > Thanks,
> > Jeff
Jeff - 21 Jun 2007 22:08 GMT
Sorry - found it!

Thanks,
Jeff
Jeff - 21 Jun 2007 23:14 GMT
That example was long and involved. So thought I would post a simpler
delegate example for C#.

public class form1 : form
{
   private delegate void myDelegate();
   private myDelegate theDelegate;

   public form1()
   {
       theDelegate = new myDelegate(delegateMethod);
   }

   private void delegateMethod()
   {
       // modify form controls
   }

   void process_Exited(Object sender, EventArgs e)
   {
       // this event occurs in a different thread from the form1 class
constructor
       myFormControl.Invoke(theDelegate);
   }
}
AlexS - 22 Jun 2007 12:26 GMT
One last point to consider is BeginInvoke against Invoke. Look up
differences between synchronous and asynchronous execution. Me personally, I
prefer BeginInvoke

Glad you were able to sort this out

Alex
> That example was long and involved. So thought I would post a simpler
> delegate example for C#.
[quoted text clipped - 21 lines]
>    }
> }
Jeff - 22 Jun 2007 14:57 GMT
Thanks again Alex. I chose synchronous as the form update needs to occur only
after the asynchronous process exits. So there isn't any more work being done
in the background and synchronous works just as well as async. As I
understand it, that is the only difference. But correct me if I am missing
something please.

Jeff

> One last point to consider is BeginInvoke against Invoke. Look up
> differences between synchronous and asynchronous execution. Me personally, I
[quoted text clipped - 28 lines]
> >    }
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.