Solved

c# event throws cross-thread error

Posted on 2014-07-23
2
688 Views
Last Modified: 2014-07-23
I have created a com dll in c# that includes events.  I have referenced it in my windows form application and
can call the exposed methods and get the results I expect without a problem.  The issue is with events.

The dll runs a thread that checks a website for updated information .. it will raise an event when the
information has changed.  I have included the event in my windows form and the event is raised correctly,
but when I try to update the textbox on the form I'm getting a cross thread exception:

protected void OnStateChanged(string eventData)
{
   // this part works perfectly!
   System.Diagnostics.Debug.WriteLine("eventdata: " + eventData);
   
   // this line throws the cross-thread error.
   textBox1.text = eventData;
}

how can I get around this?  (I am using vs2012 and .net 4.5)
0
Comment
Question by:dhenderson12
[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 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40215428
You can only update from the UI thread.  The way to update from the another thread is to call the invoke which will push the call to the UI thread.
if (textBox1.InvokeRequired)
{
    textBox1.Invoke(new MethodInvoker(delegate()
                {
                    textBox1.Text = eventData;
                }));
}
else 
 textBox1.Text = eventData;

Open in new window

0
 

Author Comment

by:dhenderson12
ID: 40215958
Thanks, Kyle! That was the step I was missing and you get the points.

As a follow up, is there a way to do the invoke step inside the dll?  The reason I ask is because the next step is to invoke this on a web page, using vbscript as the event method.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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