c# - Text label automatically updates with temperature reading every one second -
i created code read temperature reading memory address using memoryreader.dll found online, , list temperature reading in label currenttemp.text.
the thermometer updating, i'd label update temperature. able interrupt loop updating temperature clicking button, allow me change other features on menu (not yet implemented).
i couldn't figure out way have start/stop button edits variable accessible both button1_click
method , stop_click
method, have stop_click
method change status.text
label on off, allows on off control.
right code works except for loop. when add loop in, causes application freeze when click start button, , temperature value on form not update.
i searched thoroughly on stackoverflow , google, couldn't seem find answer work. here's code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using memoryeditor; namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } public void button1_click(object sender, eventargs e) { (; ; ) { if (status.text == "on") { memory omemory = new memory(); //create memory class if (omemory.openprocess("d4ithermometer")) //open handle { double data1 = omemory.readdouble(0x0049e054, new int[] { 0x8 }); currenttemp.text = data1.tostring(); } } system.threading.thread.sleep(1000); this.refresh(); } } private void currenttemp_click(object sender, eventargs e) { } public void stop_click(object sender, eventargs e) { if (status.text == "on") { status.text = "off"; } else if (status.text == "off") { status.text = "on"; } } } }
edit: @asadali bit rusty on how objects/classes/methods worked, took few days work on this. managed timer work, , initialized button click. had problems timer being able access windows form object change text due cross-thread access, , solved calling delegate on thread runs form. having problems enabling , disabling timer. put button controlled eventhandler stop_click, says "the name 'atimer' not exist in current context" here code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using memoryeditor; using system.timers; using system.threading; namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } public void button1_click(object sender, eventargs e) { system.timers.timer atimer = new system.timers.timer(); atimer.elapsed += new elapsedeventhandler(mytimer_tick); atimer.interval = 1000; atimer.enabled = true; } public void mytimer_tick(object sender, eventargs e) { memory omemory = new memory(); //create memory class if (omemory.openprocess("d4ithermometer")) //open handle { double data1 = omemory.readdouble(0x0049e054, new int[] { 0x8 }); this.settext(data1.tostring()); } //this.refresh(); } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ private void settext(string text) { // invokerequired required compares thread id of // calling thread thread id of creating thread. // if these threads different, returns true. if (this.currenttemp.invokerequired) { settextcallback d = new settextcallback(settext); this.invoke(d, new object[] { text }); } else { this.currenttemp.text = text; } } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //public void changetext(string text) //{ // currenttemp.text = text; //} private void currenttemp_click(object sender, eventargs e) { } public void stop_click(object sender, eventargs e) { if (status.text == "on") { status.text = "off"; atimer.enabled = false; } else if (status.text == "off") { status.text = "on"; atimer.enabled = true; } } } }
i agree larstech. use timer instead has 1000 interval , stuff (on tick event) current loop does.
private void mytimer_tick(object sender, eventargs e) { memory omemory = new memory(); //create memory class if (omemory.openprocess("d4ithermometer")) //open handle { double data1 = omemory.readdouble(0x0049e054, new int[] { 0x8 }); currenttemp.text = data1.tostring(); } this.refresh(); }
as noticed, removed if statement: that's because on stop button, should add statement enable/disable timer.
public void stop_click(object sender, eventargs e) { if (status.text == "on") { status.text = "off"; mytimer.enabled = false; } else if (status.text == "off") { status.text = "on"; mytimer.enabled = true; } }
the mytimer
variable above system.windows.forms.timer
. if want use system.timers.timer
, see approach below:
system.timers.timer mytimer = new system.timers.timer(); void updatemytimer() //a one-time call function must made. { mytimer.elapsed += new elapsedeventhandler(mytimer_tick); mytimer.interval = 1000; mytimer.enabled = true; }
if button1
's purpose start timer, paste code above in body of button1_click
.
Comments
Post a Comment