Solved

c# event throws cross-thread error

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

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

9 Experts available now in Live!

Get 1:1 Help Now