Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is it OK to call my method from any thread?

Posted on 2011-09-16
4
Medium Priority
?
347 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 36

Accepted Solution

by:
Miguel Oz earned 1000 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 1000 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 500 total points
ID: 36553065
Agreed...it's safe.  But why not call AppendTextBox1() directly?...
0
 
LVL 13

Assisted Solution

by:Corey Scheich
Corey Scheich earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview

963 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