Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using delegates across projects.

Status
Not open for further replies.

silverstormer

Programmer
Jan 13, 2003
2
GB
I'm having lots of problems using a delegate to fire events in external projects.
Basically I have two projects called Service, and App. I want to have an exposable event in the Service that can be triggered from the Service, and send out events to any registered event handler, in this case: the App.

Service:


namespace Service
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// The Event Trigger
Worker.OnWorkerDelegate();
}
}

public static class Worker
{
public delegate void WorkerDelegate(string someText);

public static WorkerDelegate someWork;

public static void OnWorkerDelegate()
{
if someWork != null)
{
someWork("test");
}
}
}
}




App:


using Service;

namespace App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Worker.someWork += new Worker.WorkerDelegate(myMethod);
}

private void button1_Click(object sender, EventArgs e)
{
// Test Event Trigger...
Worker.OnWorkerDelegate();
}

private void myMethod(string someText)
{
MessageBox.Show("Woohoo: " + someText);
}
}
}


I've ensured that the two application build to the same directory, and made sure that the reference to Service is pointing to the correct built EXE (Specific Version is set to False, Copy Locally is set to True).

When run, the Service when triggered does nothing as someWork is null. When you try the test trigger on the App, it returns "Woohoo: Test" as it should, but I get the feeling its not using the same instance of the static class that the Service is.

Please someone solve my increasingly annoying nightmare!
 
I'm not sure what the solution is but this is your problem. Static variables are scoped inside a process. Because both your apps are EXEs they will both run in different processes. As a reusult they both have their own instances of Worker.someWork.

 
Are you actually running these from 2 different exes?

If this is the case, you are crossing Application Domains and you will need to use something like Remoting, Sockets, or reflection (which is actually one app running the other) in order to cross the Application Domain boundaries.

A good example of this type of inter-application communication would be Java's JMS (Java Messaging Service)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top