Solved

Call a function in a regular time interval

Posted on 2014-12-14
18
69 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
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 63

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
 
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 63

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net and creating a class 5 20
Upgrading my SSIS package in VS 2012 6 60
ASP.NET (VB) return a record 2 38
Code enhancement 4 22
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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