Solved

Is it OK to call my method from any thread?

Posted on 2011-09-16
4
329 Views
Last Modified: 2012-08-13
I get the part about not updating the UI unless you're on the main thread.

My question now is: on line 39 of the sample code below, is it OK to call my LogMsg() method even though I may be running on a different thread?

namespace xyzzy
{
  public partial class Form1 : Form
  {
    // delegate enables asynchronous calls from other threads
    delegate void AppendTextBox1Delegate(string text);

    public Form1()
    {
      InitializeComponent();
    }

    /* Call this method to append text to textBox1 */
    public void AppendTextBox1(string text)
    {
      if (this.textBox1.InvokeRequired)  // Check if we are running on a different thread
      {
        // It's on a different thread, so use Invoke. (We call ourself through a delegate.)
        AppendTextBox1Delegate d = new AppendTextBox1Delegate(AppendTextBox1);
        this.textBox1.Invoke (d, new object[] { text });
      }
      else
      {
        // It's on the main UI thread, no need for Invoke
        this.textBox1.AppendText(text + Environment.NewLine);
      }
    }


    private void LogMsg(string text)
    {
      AppendTextBox1(text);
    }


    private void serialPort1_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
    {
      //I could be on any thread. Can I still call LogMsg?
      LogMsg("serialPort1_ErrorReceived.");
    }

  }
}

Open in new window

0
Comment
Question by:deleyd
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 36552950
Yes it is OK, as you are using Invoke if you are calling this.textBox1.Invoke from a different thread.
If you a solution with anonymous methods, check:
http://stackoverflow.com/questions/2367718/c-automating-the-invokerequired-code-pattern
0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
ID: 36552973
Forgot to add this link:
http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx
(see SetText method)
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 36553065
Agreed...it's safe.  But why not call AppendTextBox1() directly?...
0
 
LVL 13

Assisted Solution

by:Corey2
Corey2 earned 125 total points
ID: 36553133
You might want to consider BeginInvoke if there is any chance that the update could cause a subsequent log message (shouldn't in your sample), it will also allow the logging to happen without stopping the calling thread to wait for the logging to finish.

this.textBox1.BeginInvoke (d, new object[] { text });
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now