Write an Article Write a Tutorial Add a Fast Code* *Fast Code might be any helpful code with brief comment
Stem Cells Blood Banks Insurances .Net Hosting Credit Cards PC Rental Business
Tutorials: ASP .NET C# .NET VB .NET Articles: ASP .NET C# .NET VB .NET Fast Code: ASP .NET C# .NET VB .NET
Windows Forms controls can only execute on the thread on which they were created, that is, they are not thread-safe. If you want to get or set properties, or call methods, on a control from a background thread, the call must be marshaled to the thread that created the control.
Create and start the Thread
When a thread is created, a new instance of the Thread class is created using a constructor that takes the ThreadStart delegate as its only parameter. However, the thread does not begin executing until the Start method is invoked. When Start is called, execution begins at the first line of the method referenced by the ThreadStart delegate. private Thread timerThread; timerThread = new Thread(new ThreadStart(ThreadProc)); timerThread.IsBackground = true; timerThread.Start();
When a thread is created, a new instance of the Thread class is created using a constructor that takes the ThreadStart delegate as its only parameter. However, the thread does not begin executing until the Start method is invoked. When Start is called, execution begins at the first line of the method referenced by the ThreadStart delegate.
private Thread timerThread;
timerThread = new Thread(new ThreadStart(ThreadProc)); timerThread.IsBackground = true; timerThread.Start();
Calling a Function from the Thread
MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's invoke method, or when you need a simple delegate but don't want to define one yourself. When you create a MethodInvoker delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. BeginInvoke executes the given delegate on the thread that owns this Control's underlying window handle. The delegate is called asynchronously and this method returns immediately. You can call this from any thread, even the thread that owns the control's handle. If the control's handle does not exist yet, this will follow up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, BeginInvoke will throw an exception. Exceptions within the delegate method are considered untrapped and will be sent to the application's untrapped exception handler. // This Background Thread is called from the ThreadStart Delegate. public void ThreadProc() { try { MethodInvoker mi = new MethodInvoker(this.UpdateProgress); while (true) { this.BeginInvoke(mi); Thread.Sleep(500) ; } } } // This function is called from the Background Thread private void UpdateProgress() { if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum ; } progressBar1.PerformStep() ; }
MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's invoke method, or when you need a simple delegate but don't want to define one yourself. When you create a MethodInvoker delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.
BeginInvoke executes the given delegate on the thread that owns this Control's underlying window handle. The delegate is called asynchronously and this method returns immediately. You can call this from any thread, even the thread that owns the control's handle. If the control's handle does not exist yet, this will follow up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, BeginInvoke will throw an exception. Exceptions within the delegate method are considered untrapped and will be sent to the application's untrapped exception handler.
// This Background Thread is called from the ThreadStart Delegate. public void ThreadProc() { try { MethodInvoker mi = new MethodInvoker(this.UpdateProgress); while (true) { this.BeginInvoke(mi); Thread.Sleep(500) ; } } }
// This function is called from the Background Thread private void UpdateProgress() { if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum ; } progressBar1.PerformStep() ; }
27/08/2007 22:34:19 UTC