Solved

Update Form based on classed Thread Activity

Posted on 2012-04-10
4
125 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

19 Experts available now in Live!

Get 1:1 Help Now