Silverlight中同步調(diào)用WebClient的解決辦法,是同步!
private void button2_Click(object sender, RoutedEventArgs e)
{
Service1Client sc = new Service1Client();
sc.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(sc_DoWorkCompleted);
sc.DoWorkAsync(textBox1.Text);
}
void sc_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
{
textBox2.Text = e.Result;
}
若是你的調(diào)用非常復(fù)雜的話,比如當這個調(diào)用完成的時候開始下一個調(diào)用,然后又進行下一個調(diào)用,各個調(diào)用之間存在關(guān)聯(lián)關(guān)系的話,一直XX_DoWorkCompleted會讓你頭大,并且不利于代碼的管理。若碰到過這樣的問題的朋友一定很希望如果能夠同步調(diào)用就好了,這篇文章將幫到你?;蛘攥F(xiàn)在不需要,等你需要的時候記得用就行了,別像我當初那樣難為的不行。
主要是需要引用一個類庫的問題,這個類庫是外國人寫的,名稱為DanielVaughan.dll,下載完之后,首先需要在項目中添加對它的引用,如下圖,
然后在程序中添加對兩個空間的引用,如下圖:
將原來的添加botton1事件:
private void button1_Click(object sender, RoutedEventArgs e)
{
string dd = textBox1.Text;
string res = "NULL";
ThreadPool.QueueUserWorkItem(delegate
{
Service1 sv = ChannelManager.Instance.GetChannel<Service1>();
/* Perform synchronous WCF call. */
res = SynchronousChannelBroker.PerformAction<string, string>(sv.BeginDoWork, sv.EndDoWork, dd);
Dispatcher.BeginInvoke(delegate
{
textBox2.Text +="\r\n同步調(diào)用--"+ res+"\r\n";
});
});
}
這樣就可以實現(xiàn)對WebClient的同步調(diào)用了,當你需要關(guān)聯(lián)調(diào)用WebClient3次以上的時候 可以考慮使用這個類庫,如果只是簡單的調(diào)用下的話,沒有必要使用。
頁面全部代碼:
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<Grid.Background>
<LinearGradientBrush EndPoint="0.443,0.621" StartPoint="0.443,-2.509">
<GradientStop Color="#FF5C6768"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Button Content="同步調(diào)用服務(wù)" Height="40" HorizontalAlignment="Left" Margin="67,98,0,0" Name="button1" VerticalAlignment="Top" Width="120" Click="button1_Click" />
<dataInput:Label Height="50" HorizontalAlignment="Left" Margin="67,188,0,0" Name="label2" VerticalAlignment="Top" Width="46" Content="狀態(tài):" FontSize="16" />
<TextBox Height="40" HorizontalAlignment="Left" Margin="165,27,0,0" Name="textBox1" VerticalAlignment="Top" Width="300" FontSize="16" />
<TextBox Height="100" HorizontalAlignment="Left" Margin="146,188,0,0" Name="textBox2" VerticalAlignment="Top" Width="400" FontSize="16" TextWrapping="Wrap" Text="尚未調(diào)用服務(wù)" />
<Button Content="異步調(diào)用服務(wù)" Height="40" HorizontalAlignment="Left" Margin="346,98,0,0" Name="button2" VerticalAlignment="Top" Width="120" Click="button2_Click" />
<dataInput:Label Height="40" HorizontalAlignment="Left" Margin="67,27,0,0" Name="label1" VerticalAlignment="Top" Width="92" FontSize="16" Content="輸入文本:" />
</Grid>
</UserControl>
處理程序全部代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication2.ServiceReference1;
using System.Threading;
using DanielVaughan;
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
UISynchronizationContext.Instance.Initialize(Dispatcher);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string dd = textBox1.Text;
string res = "NULL";
ThreadPool.QueueUserWorkItem(delegate
{
Service1 sv = ChannelManager.Instance.GetChannel<Service1>();
/* Perform synchronous WCF call. */
res = SynchronousChannelBroker.PerformAction<string, string>(sv.BeginDoWork, sv.EndDoWork, dd);
Dispatcher.BeginInvoke(delegate
{
textBox2.Text +="\r\n同步調(diào)用--"+ res+"\r\n";
});
});
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Service1Client sc = new Service1Client();
sc.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(sc_DoWorkCompleted);
sc.DoWorkAsync(textBox1.Text);
}
void sc_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
{
textBox2.Text += "異步調(diào)用--" + e.Result + "\r\n";
}
}
}
Service代碼:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace SilverlightApplication2.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public string DoWork(string aa)
{
// 在此處添加操作實現(xiàn)
return "調(diào)用服務(wù)完成,返回你輸入的值:"+aa;
}
// 在此處添加更多操作并使用 [OperationContract] 標記它們
}
}
程序運行截圖:
1.
2.
3.
歡迎大家共同探討,覺得好的話請推薦下。本人技術(shù)水平有限,如有不足之處,還請園友多多批評指正,謝謝。
相關(guān)文章
ASP.NET Core MVC學(xué)習(xí)教程之路由(Routing)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core MVC學(xué)習(xí)教程之路由(Routing)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用ASP.NET Core MVC具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07TreeView創(chuàng)建IHierarchicalDataSource類型的數(shù)據(jù)源實現(xiàn)
為TreeView創(chuàng)建IHierarchicalDataSource類型的數(shù)據(jù)源實現(xiàn)2009-01-01讓GridView只更新某些特定的數(shù)據(jù)的方法
我又不希望所有的數(shù)據(jù)都可以修改,只希望修改某些特定的列,用下面的方法即可2008-10-10比較簡單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
史上最簡單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)2010-01-01vs2010出現(xiàn)error MSB8008的解決方法
這篇文章主要為大家詳細介紹了vs2010問題error MSB8008: 指定的平臺工具集(v110)未安裝或無效的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Asp.Net 網(wǎng)站優(yōu)化系列之數(shù)據(jù)庫優(yōu)化分字訣上 分庫
當我們的數(shù)據(jù)量很小的時候,我們會把用戶表,博客表,論壇表,閃存表等等都砸在一個庫里,我們的業(yè)務(wù)增長的很好,在不久之后我們盡力的優(yōu)化了查詢,但是效果依然不佳,這時候用分字訣的時機到了。2010-06-06