private void CalcTasksStarter(CancellationToken token)
        {
            var threadId = Thread.CurrentThread.ManagedThreadId;

            //Запускаем расчёт c 1
            ProcessStarterCalculationsAsync(threadId, 1).GetAwaiter().GetResult();
            Debug.WriteLine($"Starter {threadId} запущен");

            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    _transportService.Close(threadId);
                    return;
                }

                //Проверяем очередь
                var valueMq = _transportService.Get(threadId);
                if (valueMq != null)
                {
                    //Запускаем расчёт
                    ProcessStarterCalculationsAsync(threadId, valueMq.Value).GetAwaiter();

                    Debug.WriteLine($"Starter {threadId} вычисляет.");
                }
                else
                {
                    Debug.WriteLine($"Starter {threadId} ждёт");
                }

                //Для наглядности
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                if (SleepTime > 0)
                {
                    Thread.Sleep(SleepTime);
                }
            }
        }
Example #2
0
        public void Done()
        {
            var proxyConfig = _transportService.GetProxyConfig() as TLProxyConfig76 ?? TLProxyConfigBase.Empty as TLProxyConfig76;

            if (proxyConfig == null)
            {
                return;
            }
            if (!IsDoneEnabled)
            {
                return;
            }

            if (IsSocks5Proxy)
            {
                TLSocks5Proxy proxy = null;
                // new proxy
                if (_proxy == null)
                {
                    proxy = new TLSocks5Proxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port     = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Username = new TLString(Username);
                    proxy.Password = new TLString(Password);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));
                    proxyConfig.IsEnabled     = TLBool.True;

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }

                proxy = _proxy as TLSocks5Proxy;
                // change type
                if (proxy == null)
                {
                    proxy = new TLSocks5Proxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port     = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Username = new TLString(Username);
                    proxy.Password = new TLString(Password);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
                // same type
                else
                {
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port     = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Username = new TLString(Username);
                    proxy.Password = new TLString(Password);

                    _proxy = proxy;
                    if (proxyConfig.SelectedIndex.Value == proxyConfig.Items.IndexOf(_proxy))
                    {
                        _transportService.SetProxyConfig(proxyConfig);
                        _transportService.Close();
                        MTProtoService.PingAsync(TLLong.Random(), result => { });
                    }

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
            }
            else
            {
                TLMTProtoProxy proxy = null;
                // new proxy
                if (_proxy == null)
                {
                    proxy = new TLMTProtoProxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port   = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Secret = new TLString(Secret);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));
                    proxyConfig.IsEnabled     = TLBool.True;

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }

                proxy = _proxy as TLMTProtoProxy;
                // change type
                if (proxy == null)
                {
                    proxy = new TLMTProtoProxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port   = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Secret = new TLString(Secret);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
                // same type
                else
                {
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port   = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Secret = new TLString(Secret);

                    _proxy = proxy;
                    if (proxyConfig.SelectedIndex.Value == proxyConfig.Items.IndexOf(_proxy))
                    {
                        _transportService.SetProxyConfig(proxyConfig);
                        _transportService.Close();
                        MTProtoService.PingAsync(TLLong.Random(), result => { });
                    }

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
            }
        }