コード例 #1
0
        private async void btn_save_Click(object sender, EventArgs e)
        {
            Contact    contactDetails = this.GenerateContactObject();
            PictureBox pictureBox     = commonUtil.addLoaderImage(this.btn_save.Location.X + 205, this.btn_save.Location.Y + 2);

            this.Controls.Add(pictureBox);
            this.btn_save.Enabled = false;
            bool task = await Task.Run(() => this.DoValidations());

            if (task)
            {
                bool contact = await Task.Run(() => contactHelper.AddContact(contactDetails));

                if (contact)
                {
                    this.Controls.Remove(pictureBox);
                    this.Close();
                }
                else
                {
                    Banner banner = new Banner();
                    banner = uiMessage.AddBanner("Unable to add contact. Please try again later", "error");
                    this.Controls.Add(banner);
                    banner.BringToFront();
                    this.Controls.Remove(pictureBox);
                    this.btn_save.Enabled = true;
                }
            }
            else
            {
                this.Controls.Remove(pictureBox);
                this.btn_save.Enabled = true;
            }
        }
コード例 #2
0
        private static void StartServer()
        {
            var dbHelper = new ContactHelper();

            ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];

            server = new TcpListener(ip, port);

            server.Start();

            string comand = "";

            while (true)
            {
                Console.WriteLine("Wait for connection....");
                try
                {
                    var client = server.AcceptTcpClient();
                    Console.WriteLine("Connected. ");

                    using (var stream = client.GetStream())
                    {
                        try
                        {
                            if (comand == "")
                            {
                                var serializer_comand = new XmlSerializer(typeof(string));
                                comand = Convert.ToString(serializer_comand.Deserialize(stream));

                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine(DateTime.Now.ToShortTimeString() + " we have comand:  " + comand);
                                Console.ForegroundColor = ConsoleColor.Gray;
                            }
                            else
                            {
                                if (comand.ToString() == "refresh")
                                {
                                    var            serializer = new XmlSerializer(typeof(List <ContactByDB>));
                                    List <Contact> cont       = dbHelper.GetContacts();

                                    List <ContactByDB> readycont = new List <ContactByDB>();

                                    foreach (var item in cont)
                                    {
                                        readycont.Add(new ContactByDB {
                                            Id = item.Id, Name = item.Name, Age = item.Age, City = item.City, SurName = item.SurName, Telephone = item.Telephone, ImageContact = item.ImageContact
                                        });
                                    }

                                    serializer.Serialize(stream, readycont);

                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(DateTime.Now.ToShortTimeString() + " we sended data to UI(->refresh)");
                                    Console.ForegroundColor = ConsoleColor.Gray;
                                }

                                if (comand.ToString() == "add")
                                {
                                    var serializer = new XmlSerializer(typeof(ContactByDB));
                                    var item       = (ContactByDB)serializer.Deserialize(stream);
                                    dbHelper.AddContact(new Contact {
                                        Id = item.Id, Name = item.Name, Age = item.Age, City = item.City, SurName = item.SurName, Telephone = item.Telephone, ImageContact = item.ImageContact
                                    });

                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(DateTime.Now.ToShortTimeString() + " we have data from UI(->add)");
                                    Console.ForegroundColor = ConsoleColor.Gray;
                                }

                                if (comand.ToString() == "delete")
                                {
                                    var serializer = new XmlSerializer(typeof(ContactByDB));
                                    var item       = (ContactByDB)serializer.Deserialize(stream);
                                    dbHelper.DelContact(new Contact {
                                        Id = item.Id, Name = item.Name, Age = item.Age, City = item.City, SurName = item.SurName, Telephone = item.Telephone, ImageContact = item.ImageContact
                                    });

                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(DateTime.Now.ToShortTimeString() + " we have data from UI(->delete)");
                                    Console.ForegroundColor = ConsoleColor.Gray;
                                }

                                if (comand.ToString() == "edit")
                                {
                                    var serializer = new XmlSerializer(typeof(ContactByDB));
                                    var item       = (ContactByDB)serializer.Deserialize(stream);

                                    dbHelper.UpdateContact(new Contact {
                                        Id = item.Id, Name = item.Name, Age = item.Age, City = item.City, SurName = item.SurName, Telephone = item.Telephone, ImageContact = item.ImageContact
                                    });

                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(DateTime.Now.ToShortTimeString() + " we have data from UI(->edit)");
                                    Console.ForegroundColor = ConsoleColor.Gray;
                                }

                                comand = "";
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }