c# - Using a windows forms timer to trigger a method from in a different class -



c# - Using a windows forms timer to trigger a method from in a different class -

apologies if there answers out there this, i've dug lot , found little.

i'm trying implement watchdog scheme on c# .net 4.5 windows forms application. basically, have form various buttons, text displays, etc, connects external device. have timer on main form should check device changes every 250ms or so.

i've lumped mainpage form code, including timer, 1 class, , code external device own class inherits mainpage. problem is, want able have timer's tick() function phone call method device class. here's simplified code:

namespace myapplication { public partial class mainpage : form { public mainpage() { initializecomponent(); } public static void settextbox(string message) { textbox_status.text = message; } private void timer1_tick(object sender, eventargs e) { device.devicecheck(); } } public class device : mainpage { public void devicecheck() { //do other stuff here settextbox("some text"); } } }

naturally, can't phone call device.devicecheck within timer because it's not static. however, if declare devicecheck static, error textbox_status not static. if seek declare static, visual studio gets mad @ me!

originally, had dumped same class , worked fine, ugly code. there improve way?

edit: snag = more 1 thing in form needs changed devicecheck()

so left out of simplified code: in add-on settextbox() method, have method in form sets indicators, based on info devicecheck:

public void setimages(string image) { switch(image) { case "green": image_blackdim.visible = true; image_reddim.visible = true; image_greendim.visible = false; break; case "red": image_blackdatakeydim.visible = true; image_reddatakeydim.visible = false; image_greendatakeydim.visible = true; break; case "black": image_blackdim.visible = false; image_reddim.visible = true; image_greendim.visible = true; break; case "none": default: image_blackdim.visible = true; image_reddim.visible = true; image_greendim.visible = true; break; } }

naturally indicators set in place when build form, can't static either. having devicecheck homecoming string message displayed works, can't have homecoming string setimages() uses.

well... could, returning struct multiple strings, there improve way?

edit: here's working solution, based on eric's advice:

i start out struct status elements:

public struct dev_status { public string indicator1; public string indicator2; public dk_status(string indicator1, string indicator1) { this.indicator1 = indicator1; this.indicator2 = indicator2; } } public static dev_status status = new dev_status(device.messages.thing1_offline,device.messages.thing2_offline);

the indicators grab messages status:

public void setstatusboxes() { textbox_thing1_status.text = status.indicator1; textbox_thing2_status.text = status.indicator2; } public void setimages() { string indicatorcolor = "none"; if (status.indicator2 == device.messages.greenthing) indicatorcolor = "green"; else if (status.indicator2 == device.messages.redthing) indicatorcolor = "red"; else if (status.indicator2 == device.messages.blackthing) indicatorcolor = "black"; switch(indicatorcolor) { case "green": image_blackdim.visible = true; image_reddim.visible = true; image_greendim.visible = false; break; case "red": image_blackdim.visible = true; image_reddim.visible = false; image_greendim.visible = true; break; case "black": image_blackdim.visible = false; image_reddim.visible = true; image_greendim.visible = true; break; case "none": default: image_blackdim.visible = true; image_reddim.visible = true; image_greendim.visible = true; break; } }

and timer calls devicecheck method , sets indicators:

private void timer1_tick(object sender, eventargs e) { status = device.devicecheck(); setstatusboxes(); setimages(); }

finally, devicecheck sets values status struct public, static, , inherited device class:

public static void devicecheck() { //code checks device status status.indicator1 = checkdeviceconnected(); status.indicator2 = checkdevicetype(); }

this works pretty seamlessly. in end didn't need homecoming values devicecheck after all. i'll still mark thought accepted though, since led me in right direction.

what if you

1) had devicecheck() homecoming string want in text box (instead of setting text box)

2) within timer1_tick(), set text box homecoming value devicecheck.

like this:

namespace myapplication { public partial class mainpage : form { public mainpage() { initializecomponent(); } public static void settextbox(string message) { textbox_status.text = message; } private void timer1_tick(object sender, eventargs e) { string result = device.devicecheck(); settextbox(result); } } public class device : mainpage { public string devicecheck() { //do other stuff here return("some text"); } } }

c# class timer static

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -