Problems with multi threading and timers

Posted on 2006-04-23
Last Modified: 2010-04-23
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
Question by:jaxrpc
    LVL 4

    Expert Comment

    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.

    LVL 48

    Accepted Solution

    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
    ' send data

    ' thread 2
    ' send data

    Enter/Exit parameter must be some instance, the same in both cases.
    LVL 14

    Expert Comment

    I think 1 thread should be enough.. but let it fall through your wait state when changes detected or timeout....

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now