Solved

Call a function in a regular time interval

Posted on 2014-12-14
18
63 Views
Last Modified: 2015-01-19
Hi Experts,

I want to call a function in every 10 seconds. Now I am using a timer. But when the function is called all other programs(Which are in different tabs of the project) are not working till that function call is finished. What can I do so that the function call should not interfere other programs.

Thank you.
0
Comment
Question by:RadhaKrishnaKiJaya
  • 9
  • 6
  • 2
  • +1
18 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
The most obvious thing is run the work the function does inside another thread (eg. backgroundworker)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
If the work to be done does NOT involve the user interface in any way, then use System.Threading.Timer instead.  Otherwise use a BackgroundWorker() as AndyAinscow suggests.
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank you very much for your response. Can u please give me some code how to do it?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi RadhaKrishnaKiJaya;

Please see this web page, BackgroundWorker Class, At the bottom of the page it has two examples of how to implement it. If you have any questions please ask.
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank u for your reply. But I am having hard time understanding those examples. Can u please write some simple code for me. you can use any simple function.

Thanks.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>you can use any simple function.

Not really.  Have you looked up in the help files what a backgroundworker is, specifically what it can NOT do.  (Hint, it does not support any user interface objects - form, messagebox....).
What does this 'problem' function do that takes so long?  Also is it performing it's work inside a loop - there might be an alternative that would be simple to implement (a line of code) but good enough.
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank you for your reply. But I am sorry, I could not understand what you mean to say.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
A backgroundworker is of type of thread, easy to use BUT not suitable for everything.  It is pointless giving an detailed example if it is not suitable for you.
What are you doing in this function you call regularly that takes so much time up?
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
I am working on a store inventory. There are 200 racks. I have to display the status of all the racks. So I am displaying them using command buttons. Command button displays the name of the rack and its status. When I click on it, it displays the details of the rack. I need to update the rack in every 20/30 sec. So I need to call the program in a regular interval. I am not sure which is the best way to do it. When I call the function inside a timer, no other program works when this particular function is called.

Thank u.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
The updating of the interface you would be best keeping as it currently is.
I assume the thing taking the time is querying the database for the latest information.  That you could put into a background worker thread.
There is a complete example in the help files, with description.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.110%29.aspx
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank you for your time. I tried the first example and it is working fine. But I am confused how to call a function say "UpdateStatus" using that code.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Do you want the UpdateStatus to be run a number of times as the worker is running or to be run once when the worker completes?

Both of those are shown in the second example.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
What does the Function UpdateStatus do? Can you post it here please.
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Sorry. The name of the function is Display_Rack1()

Public Sub Display_Rack1()
        Dim arraybutt As New Button
        Dim connection As New SqlConnection(sqlConnectionString)
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim sql As String = ""
        Dim Proc_Status As String = "'"
        Dim Stock_Bay As String
        Dim Stock_Lev As String
        Try
            sql = "SELECT * FROM  BLKSTKMST WHERE STM_ROW = '01' ORDER BY STM_LEV ,STM_BAY"
            Dim i As Integer = 209  'I am created 209 command buttons
            connection.Open()
            cmd = New SqlCommand(sql, connection)
            Dim Reader As SqlDataReader = cmd.ExecuteReader

            While Reader.Read
                While i <= 209
                    Stock_Bay = Reader.Item("STM_LEV")
                    Stock_Lev = Reader.Item("STM_BAY")
                    Dim bay As Integer = Convert.ToInt32(Stock_Bay)
                    Dim lev As Integer = Convert.ToInt32(Stock_Lev)
                    Dim BayLev As String = bay & "," & lev

                    plRack1.Controls(i).Text = BayLev
                    plRack1.Controls(i).Visible = True
                    Proc_Status = Reader.Item("STM_STAT")

                    Select Case Proc_Status
                        Case 0 'Normal
                            plRack1.Controls(i).BackColor = System.Drawing.ColorTranslator.FromOle(&HC000)
                            plRack1.Controls(i).ForeColor = System.Drawing.ColorTranslator.FromOle(&H0)

                        Case 1 'Storing
                            plRack1.Controls(i).BackColor = System.Drawing.ColorTranslator.FromOle(&H80C0FF)
                            plRack1.Controls(i).ForeColor = System.Drawing.ColorTranslator.FromOle(&H0)

                        Case 2 'Retrieving
                            plRack1.Controls(i).BackColor = System.Drawing.ColorTranslator.FromOle(&HFF0000)
                            plRack1.Controls(i).ForeColor = System.Drawing.ColorTranslator.FromOle(&H0)

                        Case 9 'Abnormal

                            plRack1.Controls(i).BackColor = System.Drawing.ColorTranslator.FromOle(&HC0)
                            plRack1.Controls(i).ForeColor = System.Drawing.ColorTranslator.FromOle(&H0)

                        Case Else '7 'Empty cell
                            plRack1.Controls(i).BackColor = System.Drawing.ColorTranslator.FromOle(&HC0FFFF)
                            plRack1.Controls(i).ForeColor = System.Drawing.ColorTranslator.FromOle(&H0)
                    End Select

                    AddHandler plRack1.Controls(i).Click, AddressOf ClickHandler

                    i = i - 1
                    Exit While
                End While
            End While
            connection.Close()

        Catch ex As Exception
            MsgBox("Error in display_rack function : " + ex.Message)
        End Try
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Finally I found the answer how to do it. I created a thread and called the function
       Dim t1 As System.Threading.Thread
       If  t1.IsAlive = False Then
            t1 = New System.Threading.Thread(AddressOf display_rack1)
            t1.Start()
        End If

Thanks for all the help.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Be careful - that could result in a cross thread error as you attempt to access the controls in the main thread from the other thread.
Ideally just collect the data in one thread and instruct the main (UI) thread to update the control itself with the data.  (You can turn cross thread warnings off BUT it does that - turns all warnings about cross thread accessing of objects off, with possibly dire consequences if you code something wrong somewhere).
0
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank you for your reply. Can u please tell me where do I have to make the changes in the code.
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank you for your help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

10 Experts available now in Live!

Get 1:1 Help Now