Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Problems with multi threading and timers

Hi, i have an app which sends data to a LED display device. I have created a thread which will call a sub which will continuously loop and send data to the LED display device. But i realised that now besides continously looping checking for differences in data and send it to LED display, i also need to send data to the LED display every 1 or 2 min regardless there are differences in data or not.

So the situation now is........i have a thread that check for differences a send data when required and i think i will create another thread which will start a timer and sends data to the device every 1 or 2 min. But what if at the same time...both threads try to send data through the comm port...will there be any implications? Are there better ways to do this? thanks
0
jaxrpc
Asked:
jaxrpc
1 Solution
 
NazermohideeenCommented:
Yes there will be error, if the comm port is opened by a process and another process tries to open the same port. You can use flag variables to check if the port is free and if it is free, send data, if not wait until the flag is reset and then send.


  If I want to do the same program, I will develop a simple class which will automatically send data to the comm port every 1 or 2 minutes. The class will also have a public procedure to force send data, if the data is changed. that is it. you can use your own logic inside.

NM
0
 
AlexFMCommented:
You can use Monitor class to ensure that two threads don't execute some code at the same time. If both threads belong to the same class instance, this can work by the following way:

' thread 1
Monitor.Enter(this);
' send data
Monitor.Exit(this);

' thread 2
Monitor.Enter(this);
' send data
Monitor.Exit(this);

Enter/Exit parameter must be some instance, the same in both cases.
0
 
PockyMasterCommented:
I think 1 thread should be enough.. but let it fall through your wait state when changes detected or timeout....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now