DHJJ [Hatsune's Journal Japan] blog

Hatsune's Journal Japan blog

目次

Blog 利用状況

ニュース

最新ツイート

運営サイト

Hatsune's Journal Japan
DHJJ

著作など

資格など

OCP
MCP

書庫

日記カテゴリ

わんくま同盟

[VB]非同期にはRxがあるじゃないか

Visual Basic Advent Calendar 2011(12/17)

Visual Basicで非同期通信を行うときはWithEventsを使うとスッキリした形で実装できることは前回お伝えしました。

今回ご紹介するRx (Reactive Extentions) を使うとさらにスッキリします。

なお、Windows PhoneではRxは標準項目ですが、最新版のReactive Extensionsでは一部名前空間が異なりますので、ここからダウンロードして最新版で確認ください。

まずはおさらいとして前回のコードを見てみましょう。

Friend WithEvents Cli As New WebClient

Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
    Cli.DownloadStringAsync(New Uri("http://blogs.msdn.com/b/shintak/rss.aspx", UriKind.Absolute))
End Sub

Private Sub Clit_DownloadStringCompleted(sender As Object,
                                         e As System.Net.DownloadStringCompletedEventArgs) _
                                     Handles Cli.DownloadStringCompleted
    If e.Error IsNot Nothing Then
        MessageBox.Show("エラー:" & e.Error.Message)
    Else
        Dim sr As New StringReader(e.Result)
        Dim xr As XmlReader = XmlReader.Create(sr)
        Dim sf As SyndicationFeed = SyndicationFeed.Load(xr)

        Me.DataContext = sf
    End If
End Sub

ではこのコードをRxを使った形に変更してみましょう。

System.Reactiveを参照設定に追加してから次のようにFromEventPatternを設定したからDownLoadStringAsyncで非同期ダウンロードを行います。

System.Reactive.Linq.Observable.FromEventPattern(Of DownloadStringCompletedEventArgs)(cli, "DownLoadStringCompleted") _
.Subscribe(Sub(x)
               If x.EventArgs.Error IsNot Nothing Then
                   MessageBox.Show("エラー:" & x.EventArgs.Error.Message)
               Else
                   Dim sr As New IO.StreamReader(x.EventArgs.Result)
                   Dim xr As XmlReader = XmlReader.Create(sr)
                   Dim sf As SyndicationFeed = SyndicationFeed.Load(xr)
                   Me.DataContext = sf
               End If
           End Sub)
cli.DownloadStringAsync(New Uri("http://blogs.msdn.com/b/shintak/rss.aspx", UriKind.Absolute))

Rxを使うとイベントプロシージャをイベントポイントが別々にならないため読み方さえわかってしまえばイベントプロシージャがわからず四苦八苦することもなくなるかもしれません。

投稿日時 : 2011年12月18日 0:32

Feedback

# pari street 2013/03/15 18:03 http://www.a88.fr/

When prosperity some of our friends and family fully understand usa; from hardship could some of our friends and family. pari street http://www.a88.fr/

# e22.fr 2013/03/22 10:53 http://e22.fr/

Adore might be the dynamic worry within the lifestyle while the increase of truley what all of us prefer. e22.fr http://e22.fr/

# ruezee.com 2013/04/05 12:59 http://ruezee.com/

I really like you do not credited what you are, nevertheless credited people who Probably when I feel in hand. ruezee.com http://ruezee.com/

# rueree.com 2013/04/06 9:08 http://rueree.com/

May be Immortal desires us in order to meet various incorrectly customers prior to when getting together with the correct one, make sure whenever you eventually satisfy the personal, let us get experience to seem relieved. rueree.com http://rueree.com/

# ruemee.com 2013/04/06 23:47 http://ruemee.com/

Father‘testosterone spoil as well as within the the human race/partner,of which isn‘testosterone happy to spoil any energy done to you. ruemee.com http://ruemee.com/

# modatoi 2013/04/07 11:17 http://ruenee.com/

A true buddy is just one what individual overlooks your favorite accidents along with tolerates your favorite positive results. modatoi http://ruenee.com/

タイトル
名前
Url
コメント