Solved

Call a function in a regular time interval

Posted on 2014-12-14
18
67 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
ID: 40499199
The most obvious thing is run the work the function does inside another thread (eg. backgroundworker)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40499207
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
ID: 40499251
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
ID: 40499491
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
ID: 40499645
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
ID: 40500036
>>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
ID: 40500568
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
ID: 40500575
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
ID: 40500673
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40500917
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
ID: 40500926
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
ID: 40500950
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
ID: 40500970
What does the Function UpdateStatus do? Can you post it here please.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40500980
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
ID: 40503419
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
ID: 40503457
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
ID: 40503710
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
ID: 40558357
Thank you for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

20 Experts available now in Live!

Get 1:1 Help Now