私はそれが各5分間隔で実行されなければならない方法を持っています。私は2時間ごとにfloat配列を渡す必要があり、配列の各要素の値はenConsumption()の量です。したがって、私はそれが正しい時間間隔にあった場合、それはtempList配列を更新します、関数が時間を制御、この機能を書いた:は常に
public float[] passEnergyConsumptionToGarph()
{
if(compareTime(curruntTime,0,2)){
tempList[0] = enConsumption();
}
if (compareTime(curruntTime, 2, 4))
{
tempList[1] = enConsumption();
}
if (compareTime(curruntTime, 4, 6))
{
tempList[2] = enConsumption();
}
if (compareTime(curruntTime, 6, 8))
{
tempList[3] = enConsumption();
}
if (compareTime(curruntTime, 8, 10))
{
tempList[4] = enConsumption();
}
if (compareTime(curruntTime, 10, 12))
{
tempList[5] = enConsumption();
}
if (compareTime(curruntTime, 12, 14))
{
tempList[6] = enConsumption();
}
if (compareTime(curruntTime, 14, 16))
{
tempList[7] = enConsumption();
}
if (compareTime(curruntTime, 16, 18))
{
tempList[8] = enConsumption();
}
if (compareTime(curruntTime, 18, 20))
{
tempList[9] = enConsumption();
}
if (compareTime(curruntTime, 20, 22))
{
tempList[10] = enConsumption();
}
if (compareTime(curruntTime, 22, 24))
{
tempList[11] = enConsumption();
}
return tempList;
}
最後に、私は私のメインの活動で)(passEnergyConsumptionToGarphを呼び出す必要がありますその結果を私のオキシプロットグラフに渡します。
したがって、バックグラウンドで常にenConsumptionを実行する必要があります。ユーザーがbutton show graphをクリックした場合、passEnergyConsumptionToGarph()を呼び出す必要があります。私はネットで新しく、この状況で非同期を使用する方法を知っておく必要があります。あなたが私を導くなら、私は感謝します。受信した応答に考慮
を更新し
は、私がenConsumption()メソッドに各5分を実行するためにタイマーを使用:
public void timerClass(){
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(5);
var timer = new System.Threading.Timer((e) =>
{
enConsumption();
}, null, startTimeSpan, periodTimeSpan);
}
'async/await'はここでは役に立たないかもしれませんが、なぜこれを使用する必要があると思いますか?あなたが見ているべきことは、適切なタイマーです。スレッドセーフである[System.Threading.Timer](https://msdn.microsoft.com/en-us/library/system.threading.timer.aspx)をお勧めします。 – Igor
あなたが ')(' passEnergyConsumptionToGarphを簡略化することができる: '公共フロート[] passEnergyConsumptionToGarphを() {ため は(INT i = 0; iが= 22 <; I + = 2) { IF(compareTime(curruntTime、I、I + 2)) { tempList [i/2] = enConsumption(); } } } –
@AndersonPimentelご意見ありがとうございます。 –