Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2004, 09:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Access Form Label From New Thread

MOVED TOPIC FROM C# TO GENERAL .NET

I am getting an error when trying to ammend the value of a label.

The error is:
Code:
Illegal cross-thread operation: Control 'HomeLabelProcessStatus' accessed from a thread other than the thread it was created on.\r\nStack trace where the illegal operation occurred
I am getting the error after a FileSystemWatcher Event has Fired.
I think the error is occuring because the FileSystemWatcher is creating a new thread to execute ist processes.
One thing I need it to do is change a label on the form.

How do I access the forms labels from the new thread?

I am using C#.NET but VB help/example ok too.



======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old November 17th, 2004, 11:14 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Rod,

I am new to threading. Some days ago, I was playing around attempting to learn a little about threading and I did something that may solve your problem.

I have a class that will be instanced and launched as a thread. On the class is a public event. This event is raise during the thread execution. The class that consumes the "thread" class defines several instances of the "thread" class as class fields. Then there are several event handlers on the consuming class that tie back to the thread class instances to handle the thread events. Those handlers are used to update some label values. This seems to solve the cross-thread operation problem.

As I mentioned, I'm very new to threading so I'm sure that this is not the optimal way to do what you need, but it seems to work ok. I know of the term "callback" though I'm not familiar with how it's used. I know that delegates are used for callback calls. I think it's something to the effect that you pass a delegate reference to a thread so the thread can call the method referenced by the delegate.

Here are the relevant parts of my code and should get you going:

Public Class ThreadTest
    Public Event ThreadTextSet(ByVal strText As String)
    Public Sub Show()
        RaiseEvent ThreadTextSet("running...")
        'do stuff...
        RaiseEvent ThreadTextSet("Finished")
    End Sub
End Class


Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents tc1 As ThreadTest
    Private WithEvents tc2 As ThreadTest

    Private Sub cmdLaunch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLaunch.Click
        'launch threads...
    End Sub

    Private Sub tc1_ThreadTextSet(ByVal strText As String) Handles tc1.ThreadTextSet
        lblT1StateValue.Text = strText
    End Sub

    Private Sub tc2_ThreadTextSet(ByVal strText As String) Handles tc2.ThreadTextSet
        lblT2StateValue.Text = strText
    End Sub

End Class
 
Old November 17th, 2004, 11:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Peter,
I to am new to threading and it will take me some time to digest what you have said.
I would have like to put Threads of a little longer but for this error I'm getting.

Thanks Again

Rod

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old November 22nd, 2004, 03:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I got this error correct without dealing with the Threads, Probably not for the best but done all the same.
The error, extra thread, was occuring because instanciated a new FileSystem Watcher in my code.
As In: FileSystemWatcher sfw = new FileSystemWatcher();
Instead I added the file system watcher to the form as a control and this now works without error.
Not sure on the specifics as to why.

Thanks for you help Peter, I'll get to a full understanding of Threads another time.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old March 3rd, 2007, 09:08 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am also getting the same problem "Cross thread operation not supported. But in my project I can't use the watcher in form as it is a class.

Can somebody help me.

thanks
Ravikumar Patra

Ravikumar Patra
 
Old March 3rd, 2007, 09:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

To handle a cross thread operation you need to first determine it is required to be handled seperately then you call the form/controls involk function to execute in the same thread.

The code is something like this.(Not tested for syntax at all)

Code:
delegate void SetCallBack()
class YourClass
{

 private void YourMethod()
{
 if (yourControl.InvolkRequired)
{
 SetCallBack cb = new SetCallBack(YourMethod);
this.Involk(cd, new object[]{});
}
else
{
 //change your form/control here
}
}
YourMethod and the SetCallBack Delegate need to have the same signature.



======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old March 5th, 2007, 02:23 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rodmcleay,

Thanks for the solution. I am just trying the same.

regards



Ravikumar Patra





Similar Threads
Thread Thread Starter Forum Replies Last Post
Form flashes when I mouseover an unbound Label? wayne62682 Access 4 March 29th, 2006 11:11 AM
Document.form can't find Label bekim Javascript How-To 1 February 13th, 2006 07:39 PM
Clearing a label in FORM LOAD sweet4511 VB How-To 2 July 19th, 2005 01:22 PM
How do you make a label visible in a form Brian263 Access 2 March 19th, 2004 03:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.