473,239 Members | 1,800 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,239 software developers and data experts.

Delegates or events ?

GTi
[now with correct date and time]

I have this solution:

[File A - WIndows EXE] -> [File B - LIB DLL] -> [File C - Plugin DLLs]

A knows about B but not C
C knows about B but not A

Type A can be a EXE file or NT Service
There can be many of type A loaded but not necessarily the same file.

Type C is a plugin that is loaded by A on runtime.
There can be many of type C available but not necessarily the same file.

Now the problem.
A have a status window giving the end user status of the program.
The status is given by C.
So I think I must implement a interfaces in B and implment it into A and C.
C send statuses to B and use A as subscribber to events/delegates from B.

But I have problem understanding how this can be done.


Jan 9 '06 #1
4 1245
GTi wrote:
[now with correct date and time]

I have this solution:

[File A - WIndows EXE] -> [File B - LIB DLL] -> [File C - Plugin DLLs]

A knows about B but not C
C knows about B but not A

Type A can be a EXE file or NT Service
There can be many of type A loaded but not necessarily the same file.

Type C is a plugin that is loaded by A on runtime.
There can be many of type C available but not necessarily the same file.

Now the problem.
A have a status window giving the end user status of the program.
The status is given by C.
So I think I must implement a interfaces in B and implment it into A and C.
I don't think you'll need to implement the interface in A - A will just
*use* the interface, I suspect.
C send statuses to B and use A as subscribber to events/delegates from B.

But I have problem understanding how this can be done.


Well, you'll need a "custom" delegate in B if none of the ones in the
standard framework are suitable. Either way, your interface should
contain the event, so that A can subscribe to the events without
knowing the details of what the implementation is.

I'll try to write a quick sample later on, if that would help.

Jon

Jan 9 '06 #2
GTi
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
GTi wrote:
[now with correct date and time]

I have this solution:

[File A - WIndows EXE] -> [File B - LIB DLL] -> [File C - Plugin DLLs]

A knows about B but not C
C knows about B but not A

Type A can be a EXE file or NT Service
There can be many of type A loaded but not necessarily the same file.

Type C is a plugin that is loaded by A on runtime.
There can be many of type C available but not necessarily the same file.

Now the problem.
A have a status window giving the end user status of the program.
The status is given by C.
So I think I must implement a interfaces in B and implment it into A and
C.


I don't think you'll need to implement the interface in A - A will just
*use* the interface, I suspect.
C send statuses to B and use A as subscribber to events/delegates from B.

But I have problem understanding how this can be done.


Well, you'll need a "custom" delegate in B if none of the ones in the
standard framework are suitable. Either way, your interface should
contain the event, so that A can subscribe to the events without
knowing the details of what the implementation is.

I'll try to write a quick sample later on, if that would help.

Jon


Jon,
I can't wait for a quick sample.
Maybe I can then understand the missing bit.
Jan 9 '06 #3
Hi,

A knows about B but not C
C knows about B but not A
This is correct, in B you will declare the interfaces that both A & C will
implement/use
Type A can be a EXE file or NT Service Does not matter, except that the service cannot have a console window
There can be many of type A loaded but not necessarily the same file.
Not clear of what this means, are you refering that you may load several
instances of A (under different service names I assume) or you mean that
there several projects that conform to A description?

Type C is a plugin that is loaded by A on runtime.
There can be many of type C available but not necessarily the same file.
Not a problem, you load the DLL dynamically and handle the instance using
the interfaces declared in B , see Jon's plugin article.
Now the problem.
A have a status window giving the end user status of the program.
Note that a service should not have interaction with the desktop, so in this
case is better to use the eventlog
The status is given by C.
So I think I must implement a interfaces in B and implment it into A and
C.
C send statuses to B and use A as subscribber to events/delegates from B.


No really, You declare an interface in B , C has a class that implement this
interface and you use it from A.

Here I wrote how it may looks like, note that I wrote it here so it WILL
NOT compile for sure !!!
But I hopoe you will get the idea of how it's done.

Like:
IN B
interface ILog {
EventHandler event GotNewNotif;
public string theNotif;
}

IN C:
Public class theClass: ILog{
public EventHandler event GotNewNotif ;
public string theNotif;

private void SendNotif()
{
theNotif = " sdfas fas fawqr qwr";
GotNewNotif( this, null);
}
}
In A:
class Class
{
void GotMotif( object sender, EventArgs e)
{
Console.Write( ((ILog)sender).theNotif );
}

void WireAll()
{

Assemble a = Assembly.LoadFromFile(
Configuration.AppSettings["c_file_source"] );
ILog l = (ILog) a.CreateInstance(
Configuration.AppSettings["what_class_to_create"] );
l.GotNewNotif += new EventHandler( GotNotif);
}
}


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 9 '06 #4
GTi <tu****@gmail.com> wrote:
I can't wait for a quick sample.
Maybe I can then understand the missing bit.


Okay, here it is:

A.cs: (The "driver" class)
using System.Reflection;

public class MainApp
{
static void Main(string[] args)
{
foreach (string x in args)
{
Assembly assm = Assembly.Load(x);

foreach (Type t in assm.GetTypes())
{
if (typeof(IPlugin).IsAssignableFrom(t))
{
IPlugin plugin=(IPlugin)Activator.CreateInstance(t);
plugin.Notification += new EventHandler(WriteArgs);
plugin.Start();
}
}
}
}

static void WriteArgs(object sender, EventArgs e)
{
Console.WriteLine (e.ToString());
}
}

B.cs: (The interface)
using System;

public interface IPlugin
{
event EventHandler Notification;

void Start();
}

C.cs: (The plugins)
using System;
using System.Threading;

// Plugin1 fires the notification event once a second for
// 10 seconds
public class Plugin1 : IPlugin
{
// Using field-like events for simplicity
public event EventHandler Notification;

public void Start()
{
new Thread(new ThreadStart(ThreadEntry)).Start();
}

void ThreadEntry()
{
for (int i=0; i < 10; i++)
{
Notification(this, new NamedArg("Plugin1"));
Thread.Sleep (1000);
}
}
}

// Plugin2 just fires the notification event once
// and stops
public class Plugin2 : IPlugin
{
// Using field-like events for simplicity
public event EventHandler Notification;

public void Start()
{
Notification(this, new NamedArg("Plugin2"));
}
}

class NamedArg : EventArgs
{
string name;

internal NamedArg (string name)
{
this.name = name;
}

public override string ToString()
{
return name;
}
}

Compiling from the command line:
csc /target:library B.cs
csc /target:library C.cs /r:B.dll
csc A.cs /r:B.dll

Then run it with
A.exe C

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 9 '06 #5

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

Similar topics

8
by: STom | last post by:
I have a C# Winforms app that has 5 Winforms, lets say A through E. A: Data entry. When data is entered here in any field, values are updated on forms C, D, E.(Not B) B: Data entry form. When...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
10
by: jjkboswell | last post by:
I'm trying to pin down a good naming convention for the 3 things required to implement an event. You need: 1) a delegate 2) an event 3) an event handler Below is my understanding of a...
1
by: CB | last post by:
When designing an object, is there a best practice for how to allow the user of the object to tell the object to remove itself from any delegates / events it has registered with? For example.....
7
by: Peter Larsen [] | last post by:
Hi, I have a problem passing an event in a method call. Please see the following sample: //This class holds information about listeners (in MyEvent). public class First { public event...
1
by: lallous | last post by:
Hello I don't have C++/CLI 2.0 reference handy and the code below was written after long hours of research and shallow readings. From what I see, the code works and looks logical to me. Can you...
5
by: studio60podcast | last post by:
I have been fighting with this for almost two days and I can't figure it out. I'm hoping someone can shed some light on my problem. I have a web user control (NewAccountHolders) that contains a...
1
by: swatisjb | last post by:
hi, need help on basic concept behind indexer,delegates ,events and enum... pls help
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.