473,385 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Events and Threads

Hey,

I'm trying to find a way to raise events from a thread or a delegate in such
a way that it will not require the use of invoke when you are updating the
gui. In other words, make the event delegate trigger form the caller thread.
Today I'm doing this by finding the main form and invoking the event
delegate for there. But this method seems "to dirty" to be the best
solution.
Does anybody know a proper way to do this, or what I have to look for?
I have seen this done in a couple of libraries, but I don't have access to
the code behind them.

Regards Martin
Nov 10 '08 #1
2 1554
Hello Martin,

you could assigne a "control" to your thread class and then use the
<control>.BeginInvoke(..).

Regards
Rainer

"Martin Groh" <so*******@gmail.comschrieb im Newsbeitrag
news:BC**********************************@microsof t.com...
Hey,

I'm trying to find a way to raise events from a thread or a delegate in
such a way that it will not require the use of invoke when you are
updating the gui. In other words, make the event delegate trigger form the
caller thread.
Today I'm doing this by finding the main form and invoking the event
delegate for there. But this method seems "to dirty" to be the best
solution.
Does anybody know a proper way to do this, or what I have to look for?
I have seen this done in a couple of libraries, but I don't have access to
the code behind them.

Regards Martin


Nov 10 '08 #2
On Mon, 10 Nov 2008 03:24:45 -0800, Martin Groh <so*******@gmail.com>
wrote:
I'm trying to find a way to raise events from a thread or a delegate in
such a way that it will not require the use of invoke when you are
updating the gui. In other words, make the event delegate trigger form
the caller thread.
It is possible, of course. After all, this is what BackgroundWorker
does. But BackgroundWorker is a special kind of class, designed for a
very specific purpose involving exactly that. I would say that
_generally_ it is much better to have the event _subscriber_ deal with
handling the cross-thread invocation, and that will necessarily involve
the explicit use of a call to Control.Invoke() or Control.BeginInvoke() in
the event handler itself.
Today I'm doing this by finding the main form and invoking the event
delegate for there. But this method seems "to dirty" to be the best
solution.
Yes, I'd agree that if you insist on a feature like this for your own
class, that's probably one of the least-preferable ways to do it. Not the
least reason being that you have no way to know for sure that the main
form is indeed owned by the thread where the client of your class really
wants the event raised.

Depending on your needs, there are at least a couple of ways to manage
this that would be better than just picking some arbitrary form instance
to call Invoke().

Doing it the BackgroundWorker way would involve using the
SynchronizationContext class. You would have the same requirement that
BackgroundWorker does, in that the class raising the event would have to
be instantiated on the same thread where you want the event raised. In
the constructor of that class, you'd get the current thread's
synchronization context (SynchronizationContext.Current property), save
that to a private field, and then use it later to call the Post() or
Send() methods when you're raising the event.

If instead you would like to tie the thread being used to raise the event
to the class where the event handler is implemented, you can in your
event-raising code deconstruct the delegate for the event by enumerating
the invocation list (Delegate.GetInvocationList() method), and checking
each Delegate.Target reference to see if it implements
ISynchonizedInvoke. If it does, then use the ISynchronizedInvoke.Invoke()
or ISynchronizedInvoke.BeginInvoke() method to raise the event. If it
doesn't, then just raise the event normally.

Pete
Nov 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
3
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
4
by: jehugaleahsa | last post by:
Hello: Is there a way to prevent one event from firing while another event is already being fired? I have a tool that extracts media from web pages and it has multiple events firing when the...
9
by: thiago777 | last post by:
Question details: VB .NET / threads / events / GUI Imagine the following situation: A method from object "A" creates "n" threads. Variables from these threads contains values that should...
0
by: thiago777 | last post by:
Hi! Im still trying to make my application work in an event-driven way. It had worked so far with my threads, but the problem Im having is only when modifying a GUI component from the event handler...
3
by: JohnM | last post by:
Hi there, Are there any specific rules or best-practices I should be aware regarding events and the threads they're fired on. Object 1 can be created on thread 1 for instance and an event then...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.