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ù)雜的話,比如當(dāng)這個(gè)調(diào)用完成的時(shí)候開(kāi)始下一個(gè)調(diào)用,然后又進(jìn)行下一個(gè)調(diào)用,各個(gè)調(diào)用之間存在關(guān)聯(lián)關(guān)系的話,一直XX_DoWorkCompleted會(huì)讓你頭大,并且不利于代碼的管理。若碰到過(guò)這樣的問(wèn)題的朋友一定很希望如果能夠同步調(diào)用就好了,這篇文章將幫到你。或者現(xiàn)在不需要,等你需要的時(shí)候記得用就行了,別像我當(dāng)初那樣難為的不行。
主要是需要引用一個(gè)類庫(kù)的問(wèn)題,這個(gè)類庫(kù)是外國(guó)人寫的,名稱為DanielVaughan.dll,下載完之后,首先需要在項(xiàng)目中添加對(duì)它的引用,如下圖,

然后在程序中添加對(duì)兩個(gè)空間的引用,如下圖:
將原來(lái)的添加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";
});
});
}
這樣就可以實(shí)現(xiàn)對(duì)WebClient的同步調(diào)用了,當(dāng)你需要關(guān)聯(lián)調(diào)用WebClient3次以上的時(shí)候 可以考慮使用這個(gè)類庫(kù),如果只是簡(jiǎn)單的調(diào)用下的話,沒(méi)有必要使用。
頁(yè)面全部代碼:
<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)
{
// 在此處添加操作實(shí)現(xiàn)
return "調(diào)用服務(wù)完成,返回你輸入的值:"+aa;
}
// 在此處添加更多操作并使用 [OperationContract] 標(biāo)記它們
}
}
程序運(yùn)行截圖:
1.
2.
3.
歡迎大家共同探討,覺(jué)得好的話請(qǐng)推薦下。本人技術(shù)水平有限,如有不足之處,還請(qǐng)園友多多批評(píng)指正,謝謝。
相關(guān)文章
ASP.NET中的URL過(guò)濾實(shí)現(xiàn)代碼
最近做的一個(gè)Web項(xiàng)目需要對(duì)URL進(jìn)行過(guò)濾,在網(wǎng)上搜了一下,知道J2EE有個(gè)Filter的東西,而在.NET方面,其實(shí)也可以實(shí)現(xiàn)2013-01-01
ASP.NET Core MVC學(xué)習(xí)教程之路由(Routing)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core MVC學(xué)習(xí)教程之路由(Routing)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core MVC具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
TreeView創(chuàng)建IHierarchicalDataSource類型的數(shù)據(jù)源實(shí)現(xiàn)
為TreeView創(chuàng)建IHierarchicalDataSource類型的數(shù)據(jù)源實(shí)現(xiàn)2009-01-01
注冊(cè)表中存儲(chǔ)數(shù)據(jù)庫(kù)鏈接字符串的方法
2008-01-01
讓GridView只更新某些特定的數(shù)據(jù)的方法
我又不希望所有的數(shù)據(jù)都可以修改,只希望修改某些特定的列,用下面的方法即可2008-10-10
比較簡(jiǎn)單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
史上最簡(jiǎn)單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)2010-01-01
vs2010出現(xiàn)error MSB8008的解決方法
這篇文章主要為大家詳細(xì)介紹了vs2010問(wèn)題error MSB8008: 指定的平臺(tái)工具集(v110)未安裝或無(wú)效的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Asp.Net 網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫(kù)優(yōu)化分字訣上 分庫(kù)
當(dāng)我們的數(shù)據(jù)量很小的時(shí)候,我們會(huì)把用戶表,博客表,論壇表,閃存表等等都砸在一個(gè)庫(kù)里,我們的業(yè)務(wù)增長(zhǎng)的很好,在不久之后我們盡力的優(yōu)化了查詢,但是效果依然不佳,這時(shí)候用分字訣的時(shí)機(jī)到了。2010-06-06

