The solution to synchronous invocation of WebClient in Silverlight is synchronization!

Source: Internet
Author: User
The code is as follows:
Copy CodeThe code is as follows:
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;
}

If your call is very complex, for example, when the call completes the next call, and then the next call, there is a relationship between the calls, always xx_doworkcompleted will make you head large, and not conducive to the management of the code. If you've ever had a problem with a friend like that, it would be nice to be able to sync it up, and this article will help you. Or now do not need, when you need to remember to use the line, do not like my original so difficult.

Mainly need to reference a class library problem, this class library is written by foreigners, the name is DanielVaughan.dll, after downloading, first need to add a reference to it in the project, the following figure,

Then add a reference to the two spaces in your program, as shown in the following figure:



To add the original Botton1 event:
Copy CodeThe code is as follows:
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 Synchronous Call-" + res+ "\ r \ n";
});
});
}

In this way, you can implement the WebClient synchronization call, when you need to call the associated WebClient3 more than the time you can consider the use of this class library, if only a simple call, there is no need to use.
Page All code:
Copy CodeThe code is as follows:
<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= "d:designwidth=" "xmlns:datainput=" clr-namespace:system.windows.controls;assembly= System.Windows.Controls.Data.Input "width=" 640 "height=" >
<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= "Synchronous invocation Service" height= "horizontalalignment=" "left" margin= "67,98,0,0" Name= "Button1" Verticalalignment= "Top" width= "click=" Button1_Click "/>"
<datainput:label height= "horizontalalignment=" left "margin=" 67,188,0,0 "name=" Label2 "verticalalignment=" top "Width=" "content=" Status: "Fontsize="/>
<textbox height= "horizontalalignment=" "left" margin= "165,27,0,0" name= "TextBox1" verticalalignment= "Top" Width = "the" fontsize= "/>"
<textbox height= "horizontalalignment=" margin= "146,188,0,0" name= "TextBox2" verticalalignment= "Top" Width= "fontsize=" textwrapping= "Wrap" text= "service not yet invoked"/>
<button content= "Asynchronous invocation Service" height= "horizontalalignment=" "left" margin= "346,98,0,0" name= "Button2" Verticalalignment= "Top" width= "click=" button2_click "/>"
<datainput:label height= "horizontalalignment=" "left" margin= "67,27,0,0" name= "Label1" verticalalignment= "Top" Width= "fontsize=" "content=" Input text: "/>
</Grid>
</UserControl>

Handler All code:
Copy CodeThe code is as follows:
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 Synchronous Call-" + 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 + = "Asynchronous call--" + E.result + "\ r \ n";
}
}
}

Service Code:
Copy CodeThe code is as follows:
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)
{
Add an action implementation here
Return "invokes service completion, returns the value you entered:" +AA;
}
Add more actions here and mark them with [OperationContract]
}
}

Program Run screenshot:

1.

2.

3.




Welcome everybody to discuss together, feel good words please recommend next. I have limited technical level, if there are deficiencies, but also ask the park friends a lot of criticism, thank you.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.