0

I have written a class library(dll) which handle Telephony calls. This class library has delegates that handle phone call events such as OnCallReceived, OnHoldCall etc.

So now i want to add this class library to my Windows Forms App, and be able to handle Phone Call events(OnCall, OnHolde etc) in my Windows Forms application. How can achieve this?

For example

//My Class Library

Class Test
{
   ThirdParyLibrary tpl

   public Test()
   {
      tpl= new tpl();
      tpl.OnReceiveCall += Handler(OnReceiveCall);     
   }

   public void OnReceiveCall()
   {
      //i want this event to take place in client app
   }  
}

//My Windows Forms App

Client App

public main()
{
   Test t =new Test()
   //i want OnReceiveCall to be processed here
   //t.OnReceiveCall
   {
      Message.Show('You received a call');
   }
}
chosenOne Thabs
  • 1,480
  • 3
  • 21
  • 39
  • Sorry the problems unclear, can you explain – Clint Feb 01 '20 at 18:51
  • i elaborated the issue @Clint – chosenOne Thabs Feb 01 '20 at 19:14
  • 1
    It's still very unclear what you mean - and it doesn't help that the second part is just pseudo-code. What *exactly* do you mean by "client app" - client of what? Where does the `Test` code live? Please provide a *lot* more information about what you're trying to do. – Jon Skeet Feb 01 '20 at 19:24
  • @JonSkeet i added more information. – chosenOne Thabs Feb 01 '20 at 19:33
  • 1
    Winforms app is the same c# library as others. So your library can expose own events, which winform "client" can subscribe to in the same way your library subscribes to third party library events. [How can I make my own event in C#?](https://stackoverflow.com/questions/623451/how-can-i-make-my-own-event-in-c) – Fabio Feb 01 '20 at 19:59
  • I'd suggest reading https://learn.microsoft.com/en-us/dotnet/standard/events/ - as Fabio says, it doesn't matter that some of your code is in a class library. – Jon Skeet Feb 02 '20 at 07:16
  • @chosenOneThabs, just curious, did the solution below help out ? – Clint Feb 02 '20 at 20:51

1 Answers1

1

// i want this event to take place in client app

Since you want the Event Handling mechanism to take place in the Client App, which I suppose is another Class containing Main, I have created a small console that replicates the problem scenario


Uploaded to fiddle as well

using System;

namespace Test
{
    public class ThirdPartyLibrary
    {
        public delegate void dEeventRaiser();
        public event dEeventRaiser OnReceiveCall;
        public string IncomingCall(int x)
        {

            if (x > 0 && OnReceiveCall != null)
            { OnReceiveCall(); return "Valid "+x.ToString(); }
            return "Invalid"+x.ToString();
        }
    }



    public class EventSubscription
    {

        public EventSubscription()
        {
            ThirdPartyLibrary a = new ThirdPartyLibrary();
            a.OnReceiveCall += HandleTheCall;
            var iAnswer = a.IncomingCall(24198724);
            Console.WriteLine("Call received from "+iAnswer);
        }

        public virtual void HandleTheCall()
        {
            Console.WriteLine("Default way I handle the call");
        }

    }

    public class Program : EventSubscription
    {
        public override void HandleTheCall()
        {
            Console.WriteLine("Override sucessful, new way to handle the call ");
        }

       static void Main(string [] args)
        {

          Program pb = new Program();  // Control goes EnventSubscription constructor as it is derived 
            Console.Read();
        }

    }
}

Output:

enter image description here

Clint
  • 6,011
  • 1
  • 21
  • 28