473,395 Members | 2,689 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,395 software developers and data experts.

invoke in events

Ole
Hi,

I have an instance of a class in which a thread is running. When a special
condition in that instance is met it raises an event to my main class (UI),
but I can't directly interact with the user interface controls in my
eventhandler without some sort of invoke. How should that be done - is it
possible to locate the invoke in the instance so that the eventhandler
actually belongs to the UI?

Thanks,
Ole
Nov 26 '07 #1
3 1497
"Ole" <ol*@blabla.comwrote in message
news:er**************@TK2MSFTNGP02.phx.gbl...
I have an instance of a class in which a thread is running. When a special
condition in that instance is met it raises an event to my main class
(UI), but I can't directly interact with the user interface controls in my
eventhandler without some sort of invoke. How should that be done - is it
possible to locate the invoke in the instance so that the eventhandler
actually belongs to the UI?
The Invoke method should be invoked on a Control belonging to th UI. If
you pass to the instance of your class one such control (for instance the
"this" from the main form in your application), then it can use such control
to call Invoke and marshall execution into the main thread before raising
the event. Something similar to the following:

class MyClass
{
private Control ctl;
public Myclass(Control ctl)
{
this.ctl=ctl;
}

public event EventHandler theEvent;
private void EventRaiser()
{
if (theEvent!=null) theEvent(this, EventArgs.Empty);
}

void CodeThatRaisesEvent()
{
ctl.Invoke(new MethodInvoker(EventRaiser));
}
}

Nov 26 '07 #2
Ole,

If you are concerned about not having to pass the instance of the form
to the class, what you could do is get the invocation list of the delegate
for the event you are firing. You would then cycle through the list, and
check the MethodInfo for the each delegate in the list. If the type it is
associated with implements ISynchronizeInvoke, then you can cast the target
of the delegate to that and invoke the event on through the Invoke
implementation (which is, in fact, what the Invoke method of Control is).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ole" <ol*@blabla.comwrote in message
news:er**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have an instance of a class in which a thread is running. When a special
condition in that instance is met it raises an event to my main class
(UI), but I can't directly interact with the user interface controls in my
eventhandler without some sort of invoke. How should that be done - is it
possible to locate the invoke in the instance so that the eventhandler
actually belongs to the UI?

Thanks,
Ole

Nov 26 '07 #3
Ole
Thanks for your help!
Ole

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP02.phx.gbl...
Ole,

If you are concerned about not having to pass the instance of the form
to the class, what you could do is get the invocation list of the delegate
for the event you are firing. You would then cycle through the list, and
check the MethodInfo for the each delegate in the list. If the type it is
associated with implements ISynchronizeInvoke, then you can cast the
target of the delegate to that and invoke the event on through the Invoke
implementation (which is, in fact, what the Invoke method of Control is).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ole" <ol*@blabla.comwrote in message
news:er**************@TK2MSFTNGP02.phx.gbl...
>Hi,

I have an instance of a class in which a thread is running. When a
special condition in that instance is met it raises an event to my main
class (UI), but I can't directly interact with the user interface
controls in my eventhandler without some sort of invoke. How should that
be done - is it possible to locate the invoke in the instance so that the
eventhandler actually belongs to the UI?

Thanks,
Ole


Nov 28 '07 #4

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

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
10
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker...
0
by: Roeland | last post by:
Hello, When calling the Invoke() method of a web service proxy class, this normally is a synchroneous call, that stays untill the results are received. Gui events are not handled in the mean...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
15
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
2
by: ThunderMusic | last post by:
Hi, I have a class library with some classes... One of those classes fire events from a different thread. When I try to display the information contained in the event args of this event in my UI,...
1
by: Paul | last post by:
Hi all, I'm trying to trigger an event from threadB which will be processed by threadA. It sounds simple, but I cannot seem to find any documentation about this anywhere.
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.