Skip to content

Instantly share code, notes, and snippets.

View peace2048's full-sized avatar

naoki ochiai peace2048

View GitHub Profile
@peace2048
peace2048 / PropertyChangedExtensions.vb
Created October 24, 2013 11:16
PropertyChanged イベントを IObservable で監視しやすいように、INotifyPropertyChanged を拡張します。
Imports System
Imports System.ComponentModel
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Reactive.Linq
Imports System.Runtime.CompilerServices
''' <summary>
''' INotifyPropertyChanged に拡張メソッドを追加し、PropertyChanged イベントを IObservable で監視可能にします。
''' </summary>
@peace2048
peace2048 / gist:7560933
Created November 20, 2013 10:25
指定時間待機する。 Thread.Sleep の代わりに TaskCancellationSource を使ってみる
'例えば、30秒間隔で状態監視を実行する場合とか
Dim ループ = New CancellationTokenSource()
Dim 状態監視間隔の待機 As CancellationTokenSource
Dim 状態監視タスク = Task.Factory.StartNew(
Sub()
While True
' 状態を監視する処理を実行
@peace2048
peace2048 / Buffer.vb
Created November 27, 2013 10:54
T型のシーケンスを、任意サイズの配列のシーケンスに変える ref: http://qiita.com/peace2048/items/3ffd5dd1d5ddee4f6c06
Module EnumerableExBuffer
<Extension>
Public Function Buffer(Of T)(
ByVal source As IEnumerable(Of T),
ByVal count As Integer
) As IEnumerable(Of T())
If source Is Nothing Then Throw New ArgumentNullException("source")
@peace2048
peace2048 / PropertyChain.vb
Last active December 29, 2015 17:19
ある `INotifyPropertyChanged` なオブジェクトのプロパティが変化したら 別のオブジェクトのプロパティも更新する
Enum PropertyChainMode
CopyOnly
RaiseOnly
CopyAndRaise
End Enum
Class PropertyChain(Of TSource As {Class, INotifyPropertyChanged}, TTarget)
Private WithEvents _source As TSource
Private _target As TTarget
@peace2048
peace2048 / ReceiptPrinter.cs
Last active December 31, 2015 00:59
レシートプリンターに出力するのに使うつもり
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Printer.Sample
{

TaskCompletionSource でハマる

別に TaskCompletionSource でハマったわけではないですが、TaskCompletionSource で作成したタスクを Async/Await で待ったときにハマってしまった。

元々、OPC Automation で非同期入出力が、コマンドに対しイベントで完了を通知していますので、TaskCompletionSource を使って Task にする説明を考えていました。

最初に、チョロット書こうとしていたことを事実を捻じ曲げて説明します。(本当は引数等が違います。) OPC Automation では、AsyncRead に対し完了を AsyncReadComplete イベントで通知し、操作のキャンセルも AsyncCancel に対し AsyncCancelComplete で完了を通知しています。

void Main()
{
var connection = ConnectionMultiplexer.Connect("192.168.5.40");
var sx1 = connection.ObservableSubscription("pa");
using (sx1.Subscribe(_ => Console.WriteLine(_))) Console.ReadLine();
var sx2 = connection.ObservableList("qa", TimeSpan.FromSeconds(5));
using (sx2.Subscribe(_ => Console.WriteLine(_))) Console.ReadLine();

サンプルデータの作成

<Extension>
Public Iterator Function Repeat(Of T)(element As T) As IEnumerable(Of T)
	While True
		Yield element
	End While
End Function
@peace2048
peace2048 / Form1.vb
Last active March 2, 2016 06:41
forms_databinding.md
Imports System.ComponentModel
Public Class Form1
Private _model As New Model()
Public Sub New()
InitializeComponent()
<Query Kind="Statements">
<Reference>&lt;RuntimeDirectory&gt;\System.Windows.Forms.dll</Reference>
<NuGetReference>OxyPlot.WindowsForms</NuGetReference>
<Namespace>OxyPlot</Namespace>
<Namespace>OxyPlot.Axes</Namespace>
<Namespace>OxyPlot.Series</Namespace>
</Query>
var pm = new PlotModel
{