1

I'm pretty new to VB .net still and I'm attempting to write a program that constantly watches information pulled from a PLC and updates when it changes.

I've worked some with microcontrollers and I know making an infinite loop works well. What I'm running into with my VB .net code is when it loops, the form basically freezes up and the label never updates. I attempted using the code below.

Do While True
        *Code*
    Loop

So, I guess my first question is, what is the best way to go about having a continuous program to update data. It seems an infinite loop is not the answer.

My second question is, like microcontrollers, is it possible to have VB .net be an interrupt driven program instead of an event driven. My program is always running, updating the data as it comes in, but if a button is pressed it can essentialy break out of the loop. Or what I have in mind, temporarly leave the loop to perform another function then continue back into the loop.

I've looked at the System.Timers namespace, and am not 100% clear on how they work. It seems like it is potential answer to my problem. If I have a timer that does the code every one second, this one second occures as long as the form is running correct? Assuming the previous statement is correct, if the code is running from the 1 second tick, and a button is pushed while that code is running, will the button be ignored, or will the event happen after the code has finished.

Anything to help me learn will be greatly appeciated!

Timmy
  • 543
  • 1
  • 7
  • 19
  • 4
    use a thread or backgroundworker – Ňɏssa Pøngjǣrdenlarp Feb 25 '15 at 14:53
  • As @Plutonix says, you can keep the UI responsive by putting your loop in another thread. Alternatively (and more in line with your "interrupt-driven" idea) you could use a Timer to check for data on a periodic basis. It's also possible that whatever you are doing to "pull information from a PLC" will raise an event when the data is available, in which case the best approach is to handle that event. – Blackwood Feb 25 '15 at 15:11
  • Thanks! I haven't had time to look at the threading yet, I will twoards the end of the day, but the backgroundworker seems to be what I'm looking for. Also, how I'm obtaining the information doesn't raise an event, which would have been great because i didn't think to check that. Thanks for the suggestions though! Cheers! – Timmy Feb 25 '15 at 15:22
  • I whipped up a multi-threading example for [this question](http://stackoverflow.com/a/22600021/914779) that might help you. – Mike_OBrien Feb 25 '15 at 17:32
  • Hey, thanks for the example! I ended up sticking with the BackGroundWorker. It was pretty simple and did what needed to get done. Thanks everyone for all the new information, now I have a little more firepower under my belt! – Timmy Feb 25 '15 at 20:09

1 Answers1

1

You can use the

Application.DoEvents()

-method provided by the .NET framework, it essentially updates your form to apply all the changes that happened in the meantime.

  • 1
    Before recommending to use Application.DoEvents it is better to stop a moment and [read the most upvoted answer in this question](http://stackoverflow.com/a/5183623/1197518) – Steve Feb 25 '15 at 16:17