Solved

Is it OK to call my method from any thread?

Posted on 2011-09-16
4
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 36

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 36

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 86

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

622 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