Solved

Update Form based on classed Thread Activity

Posted on 2012-04-10
4
126 Views
Last Modified: 2012-04-11
Hi All,

Can someone please tell me how to show information on a winform based on a (classed) thread's progress. What I mean is, the thread executed as a different class and I can't find a relevant code example.

Public Class frmProgress

 Public Sub doQueue(maxConcurrentThreads As Integer)
        ThreadPool.SetMaxThreads(maxConcurrentThreads, maxConcurrentThreads)
        Dim i As Integer = 0
        Dim max As Integer = lines.Count
        While i < (max)
            Dim caller As New SingleThreadCaller(lines(i))
            ThreadPool.QueueUserWorkItem(AddressOf caller.SingleThreadDoer)
            i = i + 1
        End While
    End Sub

End Class

Public Class SingleThreadCaller

 Public Sub New(DRow As Object)
        Me.rowData = DRow
    End Sub

Public Sub SingleThreadDoer()
        Dim s As String = Me.buildString
        If s <> "" Then
            'Then do start query
            'HELP HERE PLEASE
            frmProgress.setText("Doing Stuff")
        End If
    End Sub

Public Function buildString()
   'etc
   Return s
End Function

End Class

Open in new window

0
Comment
Question by:PhilMellorDev
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 37827563
Here's a quick example:
Public Class frmProgress

    Public Delegate Sub ProgressUpdate(sender As SingleThreadCaller, status As String)

    Public Sub doQueue(maxConcurrentThreads As Integer)
        ThreadPool.SetMaxThreads(maxConcurrentThreads, maxConcurrentThreads)
        Dim i As Integer = 0
        Dim max As Integer = lines.Count
        While i < (max)
            Dim caller As New SingleThreadCaller(lines(i))
            AddHandler caller.Progress, AddressOf stc_Progress
            ThreadPool.QueueUserWorkItem(AddressOf caller.SingleThreadDoer)
            i = i + 1
        End While
    End Sub

    Private Sub stc_Progress(sender As SingleThreadCaller, status As String)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New ProgressUpdate(AddressOf stc_Progress), New Object() {status})
        Else
            Me.Label1.Text = status
        End If
    End Sub

End Class

Public Class SingleThreadCaller

    Public Event Progress(ByVal sender As SingleThreadCaller, ByVal status As String)

    Public Sub New(DRow As Object)
        Me.rowData = DRow
    End Sub

    Public Sub SingleThreadDoer()
        Dim s As String = Me.buildString
        If s <> "" Then
            'Then do start query

            'HELP HERE PLEASE
            RaiseEvent Progress(Me, "Doing Stuff")
        End If
    End Sub

    Public Function buildString()
        'etc
        Return s
    End Function

End Class

Open in new window

0
 

Author Comment

by:PhilMellorDev
ID: 37827821
Hi Idle_Mind,

It looks right from what I've read, but when I try to execute it I get "TargetParameterCountException was unhandled", but unfortunately "No source available".

Thanks,
Phil
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37828126
Sorry...change:

            Me.BeginInvoke(New ProgressUpdate(AddressOf stc_Progress), New Object() {status})

To:

            Me.BeginInvoke(New ProgressUpdate(AddressOf stc_Progress), New Object() {sender, status})
0
 

Author Comment

by:PhilMellorDev
ID: 37833705
Thanks Idle_Mind! Your solution was exactly what I was looking for!
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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

13 Experts available now in Live!

Get 1:1 Help Now