Solved

c# event throws cross-thread error

Posted on 2014-07-23
2
655 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

24 Experts available now in Live!

Get 1:1 Help Now