示例#1
0
        public MainWindow()
        {
            InitializeComponent();
            lstAvailableHosts.ItemsSource = Host.Hosts;
            _Multicaster = new Multicaster(MulticastIP, MulticastPort);
            Listener _Listener = new Listener(MulticastIP, MulticastPort);

            MulticasterThread = new Thread(new ThreadStart(_Multicaster.Cast));
            ListenerThread    = new Thread(new ThreadStart(_Listener.Listen));
            MulticasterThread.Start();
            ListenerThread.Start();
            RefreshViewThread = new Thread(new ThreadStart(RefreshView));
            RefreshViewThread.Start();
            TCPServer _TCPServer = new TCPServer(ConnectionPort);

            TCPServerThread = new Thread(new ThreadStart(_TCPServer.Start));
            TCPServerThread.Start();
        }
        public static void Tell(List<SearchResult> Results, string Addr)
        {
            Multicaster Caster = null;
            Thread TellThread = null;
            try
            {
                List<string> message = new List<string>();
                foreach (var r in Results)
                {
                    message.Add("have:" + Path.GetFileName(r.Path) + ":" + r.Size);
                }
                Caster = new Multicaster(MainWindow.MulticastIP.ToString(), MainWindow.MulticastPort);
                TellThread = new Thread(new ParameterizedThreadStart(Caster.Cast));
                TellThread.Start(message.ToArray());

            }
            catch (Exception ex)
            {
                if (TellThread != null)
                    TellThread.Abort();
                MessageBox.Show(ex.Message);
            }
        }
        public void Search_Click(object sender, RoutedEventArgs e)
        {
            Multicaster Caster = null;
            Thread SearchThread = null;
            try
            {

                string message = "give:" + txtSearch.Text;
                Caster = new Multicaster(MainWindow.MulticastIP.ToString(), MainWindow.MulticastPort);
                SearchThread = new Thread(new ParameterizedThreadStart(Caster.Cast));
                SearchThread.Start(message);

                new Thread(new ThreadStart(
                delegate
                {
                    int i = Caster._CastTimes;
                    App.Current.Dispatcher.Invoke((Action)delegate
                    {
                        btnSearch.IsEnabled = false;
                    });

                    while (i-- > 0)
                    {
                        App.Current.Dispatcher.Invoke((Action)delegate
                        {
                            btnSearch.Content = "Search (" + i + ")";
                        });
                        Thread.Sleep(1000);
                    }
                    App.Current.Dispatcher.Invoke((Action)delegate
                    {
                        btnSearch.IsEnabled = true;
                        btnSearch.Content = "Search";
                    });
                })).Start();

            }
            catch (Exception ex)
            {
                btnSearch.Content = "Search";
                if (SearchThread != null)
                    SearchThread.Abort();
                MessageBox.Show(ex.Message);
            }
        }