?
Solved

Call a function in a regular time interval

Posted on 2014-12-14
18
Medium Priority
?
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 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