当前位置: 首页>>代码示例>>C#>>正文


C# CancellationTokenSource.Wait方法代码示例

本文整理汇总了C#中System.Threading.CancellationTokenSource.Wait方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationTokenSource.Wait方法的具体用法?C# CancellationTokenSource.Wait怎么用?C# CancellationTokenSource.Wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.CancellationTokenSource的用法示例。


在下文中一共展示了CancellationTokenSource.Wait方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InvokeCutAsync

        public async Task InvokeCutAsync()
        {
            _sentCutToken = new CancellationTokenSource();
            _ahkEngine.ExecRaw("SendInput ^x\nRun,Pastr.Messaging.exe sentcut");

            await _sentCutToken.Wait();
        }
开发者ID:jaywick,项目名称:pastr,代码行数:7,代码来源:Events.cs

示例2: InvokePasteAsync

        public async Task InvokePasteAsync()
        {
            _sentPasteToken = new CancellationTokenSource();
            _ahkEngine.ExecRaw("SendInput ^v\nRun,Pastr.Messaging.exe sentpaste");

            await _sentPasteToken.Wait();
        }
开发者ID:jaywick,项目名称:pastr,代码行数:7,代码来源:Events.cs

示例3: ConnectInternal

		public async Task<string> ConnectInternal(string emailOrUsername, string password, string token)
		{
			bool success = false;
			await Disconnect();
			_blockEvent.Reset();
			_disconnectToken = new CancellationTokenSource();

			string url = (await _api.GetWebSocket()).Url;

			if (token != null)
			{
				try
				{
					//Login using cached token
					await _webSocket.ConnectAsync(url);
					if (_isDebugMode)
						RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
					_http.Token = token;

					await _webSocket.Login(_http.Token);
					if (_isDebugMode)
						RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token.");
					success = true;
				}
				catch (InvalidOperationException) //Bad Token
				{
					if (_isDebugMode)
						RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket had a bad token.");
					if (password == null) //If we don't have an alternate login, throw this error
						throw;
				}
			}
			if (!success) 
			{
				//Open websocket while we wait for login response
				Task socketTask = _webSocket.ConnectAsync(url);

				if (password != null) //Normal Login
				{
					var response = await _api.Login(emailOrUsername, password);
					if (_isDebugMode)
						RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token.");
					token = response.Token;
				}
				else //Anonymous login
				{
					var response = await _api.LoginAnonymous(emailOrUsername);
					if (_isDebugMode)
						RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket generated anonymous token.");
					token = response.Token;
				}
				_http.Token = token;

				//Wait for websocket to finish connecting, then send token
				await socketTask;
				if (_isDebugMode)
					RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
				await _webSocket.Login(_http.Token);
				if (_isDebugMode)
					RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket logged in.");
				success = true;
			}

			if (success)
			{
				if (_config.UseMessageQueue)
					_mainTask = MessageQueueLoop();
				else
					_mainTask = _disconnectToken.Wait();
				_mainTask = _mainTask.ContinueWith(async x =>
				{
					await _webSocket.DisconnectAsync();
#if !DNXCORE50
					if (_config.EnableVoice)
						await _voiceWebSocket.DisconnectAsync();
#endif

					//Clear send queue
					Message ignored;
					while (_pendingMessages.TryDequeue(out ignored)) { }

					_channels.Clear();
					_messages.Clear();
					_roles.Clear();
					_servers.Clear();
					_users.Clear();

					_blockEvent.Set();
					_mainTask = null;
				}).Unwrap();
				_isConnected = true;
			}
			else
				token = null;
			return token;
		}
开发者ID:Mercurial,项目名称:Discord.Net,代码行数:96,代码来源:DiscordClient.cs


注:本文中的System.Threading.CancellationTokenSource.Wait方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。