I have a Windows App that is doing some work and then writing a "Now
Processing..." line to the status line of the window as well as the Textbox
on the form.
But the problem is that the work is in another class from the main class.
So it couldn't access the Status Line or textbox.
So what we did was set them up as properties:
string IStatusDisplay. Status
{
get
{
return Status.Text;
}
set
{
Status.Text = value;
}
}
string IStatusDisplay. StatusBar
{
get
{
return toolStripStatus Label1.Text;
}
set
{
toolStripStatus Label1.Text = value;
this.Refresh();
}
}
Then in my code I would call it like:
Maintenance.Che ckAll(this);
And the function is:
public static void CheckAll(IStatu sDisplay display)
{
...
display.StatusB ar = "Now Processing... " +
Path.GetFileNam e(file);
display.Status += file + Environment.New Line;
}
This works fine until you try to set it up and call it from a thread - for
one thing you can't use the this.Refresh() because you get an error:
this.Refresh();
Gets me this error:
Cross-thread operation not valid: Control 'FieldNameMapSe tup'
accessed from a thread other than the thread it was created on.
Here I am in another function doing a - "display.Status Bar". It is
executing the above property until it hits the "this" statement.
or if you access a button on the page like:
btnExit.Enabled = true;
I get the error:
Cross-thread operation not valid: Control 'btnExit' accessed
from a thread other than the thread it was created on.
I am trying to find out how to access the controls on the Form from either
the same class or a different class (which was why we were passing "this" to
the other class).
This all works fine until you put it in a thread.
Thanks,
Tom
Processing..." line to the status line of the window as well as the Textbox
on the form.
But the problem is that the work is in another class from the main class.
So it couldn't access the Status Line or textbox.
So what we did was set them up as properties:
string IStatusDisplay. Status
{
get
{
return Status.Text;
}
set
{
Status.Text = value;
}
}
string IStatusDisplay. StatusBar
{
get
{
return toolStripStatus Label1.Text;
}
set
{
toolStripStatus Label1.Text = value;
this.Refresh();
}
}
Then in my code I would call it like:
Maintenance.Che ckAll(this);
And the function is:
public static void CheckAll(IStatu sDisplay display)
{
...
display.StatusB ar = "Now Processing... " +
Path.GetFileNam e(file);
display.Status += file + Environment.New Line;
}
This works fine until you try to set it up and call it from a thread - for
one thing you can't use the this.Refresh() because you get an error:
this.Refresh();
Gets me this error:
Cross-thread operation not valid: Control 'FieldNameMapSe tup'
accessed from a thread other than the thread it was created on.
Here I am in another function doing a - "display.Status Bar". It is
executing the above property until it hits the "this" statement.
or if you access a button on the page like:
btnExit.Enabled = true;
I get the error:
Cross-thread operation not valid: Control 'btnExit' accessed
from a thread other than the thread it was created on.
I am trying to find out how to access the controls on the Form from either
the same class or a different class (which was why we were passing "this" to
the other class).
This all works fine until you put it in a thread.
Thanks,
Tom
Comment