(背景) 私はC#の知識を促進するために株式市場のエミュレーションプログラムを作成しています。 以前にこのプログラムをJavaで作成しました。C#マルチスレッド型「System.OutOfMemoryExceptionに」の未処理の例外が発生している
(問題) Javaでは、GUIのラベルをx回ごとにループして更新するために、新しいスレッドを作成することができました。私はC#でこれを行う方法を研究し、タイマーに遭遇しましたが、これは私のためには機能しませんでしたので、私はマルチスレッドに頼っていました。私はここに新しいスレッド
/// <summary>
/// stock emulation startup
/// </summary>
public stockEmu()
{
CheckForIllegalCrossThreadCalls = false; //if this is true, then cross-thread changes cannot be made (repeater cannot set labels if true)
initializeValues(); //this will set the startup values e.g stock prices and user money
ThreadStart loopThread = new ThreadStart(repeater); //opens a new thread
Thread openThread = new Thread(loopThread); //opens a new thread
openThread.Start(); //opens a new thread
InitializeComponent(); //initializes the form
this.updateLabels(); //needs to be after initializecomponent or null exception is thrown (because the labels are not drawn yet)
}
を作成し、フォームの起動時に
は、新しいスレッドの方法であって、ここで
/// <summary>
/// infinite loop to execute every x seconds (using a new thread)
/// repeater uses cross-thread operation(s) and so CheckForIllegalCrossThreadCalls has been set to false
/// MSDN recommends against this, it is executed safely however
/// </summary>
private void repeater()
{
while(true)
{
Thread.Sleep(5000); //sleep (pause) the thread for 5 seconds
instance = instance + 1; //add to the current instance (this is used to display what "day" we're on
changePrices(); //change the prices of the stocks
updateLabels(); //update the form's labels to display the new values
}
}
はメソッドですリピータは5秒ごとに
/// <summary>
/// this will change the prices every x seconds based on current prices etc
/// </summary>
private void changePrices()
{
marketController mC = new marketController();
for(int i = 0 ; i < stocks.Length ; i++)
{
mC.changePrices(stocks [ i ] , i); //mc for marketController, object reference, change prices will calc the price changes
}
return;
}
を呼び出します
mC.changePricesは実際にはまだ何も実行していませんが、そこに停滞することはありません。
/// <summary>
/// method used to update all display labels every x seconds (based on repeater)
/// </summary>
public void updateLabels()
{
try
{
this.userMoneyLabel.Text = "Your money: " + this.getUserMoney().ToString(); //changes the user's money label
this.currentDayLabel.Text = "Day: " + this.getInstance().ToString(); //changes the day label
this.setStocksCombined(); //sets the array of combined stock prices and stock names
this.stockListBox.Items.Clear(); //clear the list box because it will constantly stack the items
this.stockListBox.Items.AddRange(stocksCombined); //adds the combined array to the list box (stocks + stockNames)
}
catch(Exception e)
{
MessageBox.Show("Error: " + e);
}
}
関連するラベルのすべてが罰金更新し、私はsetStocksCombinedを追加する前に、この問題は、(永続化)ので、私は問題があるとは思いません。
にスローされるエラーです。
型「System.OutOfMemoryExceptionに」の未処理の例外は、私がリピーターとは別に、追加のスレッドを開いていないがmscorlib.dll
に
を発生それは、例えば7
事前のおかげで(たぶん)
に達したときに、リピータは、通常、このエラーをスロー編集:@RichardSzalayへ
おかげで、@MichaelThePotato私はこの例を使用して、タイマーを実装している:https://stackoverflow.com/a/12535833/6639187使用方法の
'MSDNは、それが実行され、これに反対を推奨し、安全however'は奇妙なように思えますアプリケーションがクラッシュすると仮定します。それを引き起こしていると言っているわけではありませんが、そうかもしれません。 –
あなたは実際にあなたのバックグラウンド作業を行うために 'Task'を使って調べるべきです。 2016年に.netに直接スレッドを作成することは本質的には聞き取れず、常に眉を浮かべる価値があります。一方、タスクは、コースのためのパーです。 –
サンプルのサイズはどのくらいですか?どのようなデータが含まれていますか? – fofik