?
Solved

c# event throws cross-thread error

Posted on 2014-07-23
2
Medium Priority
?
708 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 2000 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

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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