Hye everybody!
I've got a problem with a textbox access. I'm using VS2008, WPF, c#.
I've got two projects in a solution. In one project i update an xml doc. In the other project, in wich my textbox is declared, i have declared a FileSystemWatcher who detect when my xml doc change. When it changes i whant to put a text in my textbox. Unfortunatly it raise a InvalidOperationExeption and write: "The calling thread cannot access this object because a different thread owns it.". I don't know how to solve this problem. Is anyone can help?
Loading
Nilanka DharmadasaPosted Aug 1, 2008, 4:52 AM
According to what i understood, the text box is owned by one thread while the file system watcher is runing on another thread. You may use a delegate to solve this issue.
public
class TextBoxHandler{
public delegate void DelegateUpdateText(String s);public DelegateUpdateText _DelegateUpdateText ;
public
TextBoxHandler(){
_DelegateUpdateText = new DelegateUpdateText(this.UpdateTextMethod);
}
public void UpdateTextMethod(string s){
TextBox1.Text = s;
}
}
Now in the file system watcher you can call this delegate '_DelegateUpdateText'. Not the method 'UpdateTextMethod'.
TextBoxHandler _obj = new TextBoxHandler ();
_obj .Invoke(_obj ._DelegateUpdateText,
new object[] { "File change detected." });Hope this one will work.