问题由来:
想把部分异步函数改一下,可以支持同步
语言:
.NET C#
------------------------------------------------------------------------------
原来代码:
>先注册登录结果通知 OnLogin;
>调用 Login(userId,password) 后马上返回不阻塞
>成功或失败时在 OnLogin 中通知
代码如下:
public delegate void delegateOnLogin(string userId,bool loginStatus,string info);
public delegateOnLogin OnLogin;
public void Login(string userId,string password)
{
//调用第三方库进行登录
return;
}
//登录成功或失败时会调用该通知
public void OnLogin(string userId,bool loginStatus,string info)
{
if( OnLogin != null )
OnLogin( userId,loginStatus,info );
}
------------------------------------------------------------------------------
改过的代码:
定义一个全局变量如 m_LoginStatus ,如 Login 执行时先初始化 m_LoginStatus = -1;当 OnLogin 成功则改为 m_LoginStatus = 1,失败则改为 m_LoginStatus = 0; 在Login 中加一个while 循环一直判断
private m_LoginStatus = -1;
public bool Login(string userId,string password)
{
m_LoginStatus = -1;
//调用第三方库进行登录
while(true)
{
if ( m_LoginStatus == -1)
Thread.Sleep(10);
else if ( m_LoginStatus == 0)
return true;
else
return false;
}
}
public void OnLogin(string userId,bool loginStatus,string info)
{
if ( loginStatus == true )
m_LoginStatus = 1;
else
m_LoginStatus = 0;
}
登录函数只是其中一种,有多个函数都得改成同步的,大家有更好的方式吗?谢谢。
MSDN的例子。
// You can run this code in Visual Studio 2013 as a WPF app or a Windows Store app. // You need a button (StartButton) and a textbox (ResultsTextBox). // Remember to set the names and handler so that you have something like this: // <Button Content="Button" HorizontalAlignment="Left" Margin="88,77,0,0" VerticalAlignment="Top" Width="75" // Click="StartButton_Click" Name="StartButton"/> // <TextBox HorizontalAlignment="Left" Height="137" Margin="88,140,0,0" TextWrapping="Wrap" // Text="TextBox" VerticalAlignment="Top" Width="310" Name="ResultsTextBox"/> // To run the code as a WPF app: // paste this code into the MainWindow class in MainWindow.xaml.cs, // add a reference to System.Net.Http, and // add a using directive for System.Net.Http. // To run the code as a Windows Store app: // paste this code into the MainPage class in MainPage.xaml.cs, and // add using directives for System.Net.Http and System.Threading.Tasks. private async void StartButton_Click(object sender, RoutedEventArgs e) { // ExampleMethodAsync returns a Task<int>, which means that the method // eventually produces an int result. However, ExampleMethodAsync returns // the Task<int> value as soon as it reaches an await. ResultsTextBox.Text += "\n"; try { int length = await ExampleMethodAsync(); // Note that you could put "await ExampleMethodAsync()" in the next line where // "length" is, but due to when '+=' fetches the value of ResultsTextBox, you // would not see the global side effect of ExampleMethodAsync setting the text. ResultsTextBox.Text += String.Format("Length: {0}\n", length); } catch (Exception) { // Process the exception if one occurs. } } public async Task<int> ExampleMethodAsync() { var httpClient = new HttpClient(); int exampleInt = (await httpClient.GetStringAsync("http://msdn.microsoft.com")).Length; ResultsTextBox.Text += "Preparing to finish ExampleMethodAsync.\n"; // After the following return statement, any method that's awaiting // ExampleMethodAsync (in this case, StartButton_Click) can get the // integer result. return exampleInt; } // Output: // Preparing to finish ExampleMethodAsync. // Length: 53292