raspberry pi - C# Windows IoT - Update GUI from task -
i have tried lot can't find out how update gui element example textblock.text
running task on windows universal app windows iot on raspberry.
is there way this?
it should work out of running task without stopping it.
according answer have tried this:
task t1 = new task(() => { while (1 == 1) { byte[] writebuffer = { 0x41, 0x01, 0 }; // buffer write mcp23017 byte[] readbuffer = new byte[3]; // buffer read mcp23017 spidisplay.transferfullduplex(writebuffer, readbuffer); // send writebuffer mcp23017 , receive results readbuffer byte readbuffer2 = readbuffer[2]; // extract correct result string output = convert.tostring(readbuffer2, 2).padleft(8, '0'); // convert result output format // update frontend textblock status5 result windows.applicationmodel.core.coreapplication.mainview.corewindow.dispatcher.runasync(coredispatcherpriority.normal, () => { // ui update code goes here! status6.text = output; }); } }); t1.start();
but following 2 errors:
error cs0103 name 'coredispatcherpriority' not exist in current context
and
cs4014 because call not awaited, execution of current method continues before call completed. consider applying 'await' operator result of call.
am doing wrong using code?
i'm not sure problem is, guess might problem different threads. try use dispatcher. need integrate windows.ui.core namespace:
using windows.ui.core;
here's call (slightly modified work out of box).
private void doit() { task t1 = new task(async () => { while (1 == 1) { await windows.applicationmodel.core.coreapplication.mainview.corewindow.dispatcher.runasync(coredispatcherpriority.normal, () => { // ui update code goes here! status6.text = "hello" + datetime.now; }); await task.delay(1000); } }); t1.start(); }
little hint: while (1=1) sounds infinite loop me. hint: added "await task.delay(1000);" have little break during loops.
also check out answer regarding dispatcher. correct way coredispatcher in windows store app
Comments
Post a Comment