0

All I am trying to do is update a textBox (in this case txtInit) from another class. I have been reading a lot about how a UI Thread has to change itself, and something about using a dispatcher. I found an answer on here that seemed close, but I couldnt get it to work for me... it said to try using the line:

MainForm.Dispatcher.Invoke(new Action(delegate() {MainForm.myInstance.txtInit.Text = "Text"};);

In my ServerSide class, I need to send a String to the txtInit textbox on my MainForm.. and that is all I need to do.. thanks for any help.

Ramrod
  • 378
  • 7
  • 18
  • 2
    Please add a tag to indicate whether you are using C# in a desktop app (WPF, WinForms) or a web app (ASP.Net). – DOK Apr 18 '12 at 13:25
  • This will be a desktop application, I am using Visual Studio if that matters at all... which I'm not sure that it does. – Ramrod Apr 18 '12 at 13:30

3 Answers3

1

Classes have nothing to do with threads(which is your problem right now). Each Control has an Invoke method which will do the right thread synchronization for you. So you can do

MainForm.myInstance.txtInit.Invoke((sender, args) => (sender as TextBox).Text = "text");

To improve performance you can test(which basically tells you if you're in the same thread) the Control.IsInvokeRequired property.

Another way to do it is by using the SynchronizationContext of the UI thread which you need to capture in the constructor of the form from SynchronizationContext.Current and then do

syncContext.Send((obj) =>  MainForm.myInstance.txtInit.Text = "Text", null);
Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28
  • Ok, how exactly do I create an Instance of my MainForm? I know the code I gave uses that, but I'm unsure of what and how to use it. – Ramrod Apr 18 '12 at 13:33
  • ah - put a static field on your MainForm class and store the instance there - this is called Singleton pattern – Ventsyslav Raikov Apr 18 '12 at 15:12
1

I would probably just create a public method on the MainForm that you can pass a string to and let that method set the text for the text box. You can also control whether or not you need to us the Invoke call (different threads) so you never have to worry about coding this in other areas - just call the method and pass the string.

Here is an example:

 public partial class Form1 : Form
    {
        public delegate void UpdateText(string text);
        public Form1()
        {
            InitializeComponent();
        }
        public void SetTextBoxText(string text)
        {
            // Check to see if invoke required - (from another thread)
            if(textBox1.InvokeRequired)
            {
                textBox1.Invoke(new UpdateText(this.SetTextBoxText),

                    new object[]{text});
            }
            else
            {
                textBox1.Text = text;
            }
        }
    }
tsells
  • 2,751
  • 1
  • 18
  • 20
  • But I can't call the method from my other Thread if I can't access the MainForm to use the method... I think I'm thinking about this correctly. In my server class I can't say: MainForm.updateText("Text"); – Ramrod Apr 18 '12 at 13:46
  • You will need to have a reference to your main form. So if you launch another form - before you call ShowDialog() - you need to either set a property on the child form - or pass a reference via the constructor to the main form. – tsells Apr 18 '12 at 13:48
  • Ok, so I'm not launching any other forms. public MainForm() { InitializeComponent(); } [STAThread] public static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } I do not understand where I need to "name"? my MainForm. – Ramrod Apr 18 '12 at 14:01
  • Won't you be updating from a thread that was spawned from the MainForm processing? Pass it there. – tsells Apr 18 '12 at 17:22
  • I will be updating the Main UI Thread (Main Thread) from a Thread that was spawned by the Main Thread. – Ramrod Apr 21 '12 at 19:25
0

If I understand correctly, it seems you want to access the Windows form elements from another thread or from some asynchronous events. In such case following links may help you.

  1. http://msdn.microsoft.com/en-us/library/ms171728.aspx

  2. Update UI from multiple worker threads (.NET)

  3. Controlling form elements from a different thread in Windows Mobile

Community
  • 1
  • 1
VPK
  • 31
  • 4