namespace D_Duplicated
{
classProgram
{
staticvoid Main(string[] args)
{
int N =int.Parse(Console.ReadLine());
var A_i =new int[N];
for (int i =0; i < N; i++)
{
A_i[i] = (int.Parse(Console.ReadLine()));
}
//書き換えがなければある整数は1つしかないはずなので個数を数えるvar count =new int[N];
for (int i =0; i < N; i++)
{
count[A_i[i] -1]++;
}
//個数から重複を探すint duplicatedNum =0;
int missingNum =0;
for (int i =0; i < N; i++)
{
if (count[i] ==0)
{
missingNum = i +1;
}
elseif (count[i] ==2)
{
duplicatedNum = i +1;
}
}
//出力if (duplicatedNum ==0)
{
Console.WriteLine("Correct");
}
else {
Console.WriteLine($"Duplicated Number is {duplicatedNum}, Missing Number is {missingNum}");
}
}
}
}
なぜエラーが出力されるのか?
それはWindowsフォームやWPFにおけるコントロール(UI)は、コントロールが作成されたメインスレッド=UIスレッドからのみ呼び出すことが許されており、ワーカースレッドでコントロールを呼び出すとエラーが起きるからである。 マイクロソフトさんのサイトに以下のように言及されてる。
「Most methods on a control can only be called from the thread where the control was created.」 learn.microsoft.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScreenTransition1.ViewModels
{
publicclassAViewModel:MainContent
{
}
}
MainContent
INotifyPropertyChangedを継承したクラスである。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace ScreenTransition1.ViewModels
{
publicclassMainContent : INotifyPropertyChanged
{
//UIに自動更新を行うためのイベントpublicevent PropertyChangedEventHandler PropertyChanged;
protectedvirtualvoid RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler !=null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace ScreenTransition1.ViewModels
{
publicclassMainContent : INotifyPropertyChanged
{
//UIに自動更新を行うためのイベントpublicevent PropertyChangedEventHandler PropertyChanged;
protectedvirtualvoid RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler !=null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public MainContent(string title)
{
Title = title;
}
publicstring Title { get; privateset; }
}
}
MainViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScreenTransition1
{
using ViewModels;
publicclassMainViewModel:MainContent
{
public MainViewModel() :base("MainView")
{
}
private IList<MainContent> _contents;
public IList<MainContent> Contents
{
get { return _contents ?? (_contents = CreateContents().ToArray()); }
}
private IEnumerable<MainContent> CreateContents()
{
yieldreturnnew AViewModel();
yieldreturnnew BViewModel();
}
private MainContent _activeContent;
public MainContent ActiveContent
{
get { return _activeContent ?? (_activeContent = AViewModel); }
set
{
if (_activeContent !=value)
{
_activeContent =value;
RaisePropertyChanged("ActiveContent");
}
}
}
public AViewModel AViewModel { get { return Contents.First() asAViewModel; } }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;//for INotifyPropertyChangednamespace DataContext
{
publicclassMainViewModel : INotifyPropertyChanged
{
//UIに自動更新を行うためのイベントpublicevent PropertyChangedEventHandler PropertyChanged;
protectedvirtualvoid RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler !=null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
//TextBlock用プロパティpublicstring name ="Nameの初期設定を表示";
publicstring Name
{
get { return name; }
set
{
if (name !=value)
{
name =value;
RaisePropertyChanged("Name");
}
}
}
}
}