Click here to Skip to main content
15,887,421 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
///Client window
public partial class Autorization : Window
    {
        private bool Autos;
        public Autorization()
        {
            InitializeComponent();
            NetNamedPipeBinding binding = new NetNamedPipeBinding();
            EndpointAddress address = new EndpointAddress("net.pipe://localhost/Service");
              CurUser._clientCallback = new ClientCallback();
            CurUser._clientCallback.RecieveAut += UpdateAutos;
            //InstanceContext створює оболонку для екземпляра класу ClientCallback
            //завдяки цьому служба може викликати обєкт через канал
              InstanceContext context = new InstanceContext(CurUser._clientCallback);

            DuplexChannelFactory<duplexdllclass.imessagecallbackservice> factory = new DuplexChannelFactory<duplexdllclass.imessagecallbackservice>(context, binding, address);
            CurUser.Proxy = factory.CreateChannel();
         
           
        }
        void UpdateAutos()
        {
            Autos = CurUser._clientCallback.Autos;
        }
        private void btn_LoginClick(object sender, RoutedEventArgs e)
        {
            CurUser.Proxy.AutorizationSendToServiceMessage(new User());
           UpdateAutos();
            Autos.ToString();
            if (Autos) MessageBox.Show("Happy");
            MessageBox.Show("dfasdf");
        }

        private void btn_RegClick(object sender, RoutedEventArgs e)
        {
            
        }

///Service

public class Service : DuplexDllClass.IMessageCallbackService
    {

       private void ThreadAutorizationCallback(object callback)
       {
           DuplexDllClass.IMessageCallbackClient messageCallback = callback as DuplexDllClass.IMessageCallbackClient;

           messageCallback.AutorizationSendToClient(true);
           Thread.Sleep(1000);

       }

        public void AutorizationSendToServiceMessage(User user)
        {
            DuplexDllClass.IMessageCallbackClient proxy = OperationContext.Current.GetCallbackChannel<duplexdllclass.imessagecallbackclient>();
            //посилаємо повідомлення клієнту
            // proxy.MessageToClient("Message from server");     //create new thread
            //запускаємо потік та передаємо йому об’єкт проксі класу
            new Thread(ThreadAutorizationCallback).Start(proxy);
        }
    }
//Service window
 internal static ServiceHost serviceHost;
        internal static void StartService()
        {
            serviceHost = new ServiceHost(typeof(Service), new Uri("net.pipe://localhost/Service"));
            serviceHost.AddServiceEndpoint(typeof(DuplexDllClass.IMessageCallbackService), new NetNamedPipeBinding(), "");
            serviceHost.Open();
        }
        internal static void StopService()
        {
            if (serviceHost.State != CommunicationState.Closed)
                serviceHost.Close();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Service runnig");
            Service service= new Service();
            StartService();
            Console.ReadLine();
            StopService();
        }
    }
//Client
 class ClientCallback : DuplexDllClass.IMessageCallbackClient
    {
        public Action RecieveAut;
        public bool Autos;
        public void AutorizationSendToClient(bool rezalt)
        {
            Autos = rezalt;
        }
    }</duplexdllclass.imessagecallbackclient></duplexdllclass.imessagecallbackservice></duplexdllclass.imessagecallbackservice>
Posted
Updated 13-Oct-11 2:18am
v2
Comments
LittleYellowBird 13-Oct-11 8:19am    
Hi, you need to explain what your problem is, in what way is it not working properly. Use the 'Improve question' link at the bottom right to modify your question. Best of Luck.

1 solution

Eye-ball compiling this code (without using a computer), you have a race-condition. Since you call the callback and set the flag in a thread, it might not finish setting the flag until after you have returned in the client and updated the client side flag.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900