silverstormer
Programmer
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!
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!