Call a function in a regular time interval

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.
RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
The most obvious thing is run the work the function does inside another thread (eg. backgroundworker)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for your response. Can u please give me some code how to do it?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
>>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
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. But I am sorry, I could not understand what you mean to say.
0
AndyAinscowFreelance programmer / ConsultantCommented:
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
RadhaKrishnaKiJayaAuthor Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
RadhaKrishnaKiJayaAuthor Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
Fernando SotoRetiredCommented:
What does the Function UpdateStatus do? Can you post it here please.
0
RadhaKrishnaKiJayaAuthor Commented:
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
RadhaKrishnaKiJayaAuthor Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. Can u please tell me where do I have to make the changes in the code.
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.