Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Restarting Backgroundworker Component with in work Compeleted Event VB.Net

Posted on 2009-05-11
4
Medium Priority
?
843 Views
Last Modified: 2013-11-26
hi,

I have some criteria.. I will start the backgrndwrkr component and it will return a result to me,If the result is Something (*Some String) then manipulation will be done automatically by events.. but if result is Nothing then it shud restart the Background worker component from starting.. to put it short i want to restart the background worker in its thread itself.. i searched whole interne but cud not find any thing useful.. my last hope is this place..
0
Comment
Question by:Yogesh_Agarwal
[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
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24355687
The THREAD will know if it is going to return Nothing and need restarting....so instead of letting it complete and raise the RunWorkerCompleted() Event, make it restart itself internally with some kind of looping structure such as a While() loop.  You can notify the UI of the restart using the ProgressChanged() event...
0
 

Author Comment

by:Yogesh_Agarwal
ID: 24356403
i am really confused to how to use that loop.. i don know how to use loops for that.. i am really fed of searching it.. can u please tell me?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 total points
ID: 24356981
Ok...that was a pretty vague description so hopefully you can glean something from this convoluted example.  It keeps "working" until the result is retrieved in the second half of the current minute:
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        BackgroundWorker1.RunWorkerAsync()
    End Sub
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Static R As New Random
        Dim dt As DateTime
        Dim result As Integer
        Do
            System.Threading.Thread.Sleep(R.Next(2000, 7001)) ' do some "work" between 2 and 7 secs long
            dt = DateTime.Now
            result = dt.Second
            If result < 30 Then
                BackgroundWorker1.ReportProgress(-1, dt)
            End If
        Loop While result < 30
        e.Result = dt.TimeOfDay.TotalMilliseconds
    End Sub
 
    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        If e.ProgressPercentage = -1 Then
            Label1.Text = "Restarted @ " & e.UserState
        End If
    End Sub
 
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Label1.Text = "Completed: " & e.Result
        Button1.Enabled = True
    End Sub
 
End Class

Open in new window

0
 

Author Closing Comment

by:Yogesh_Agarwal
ID: 31580149
nice work..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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