Results 1 to 5 of 5

Thread: Timer Problem

  1. #1

    Thread Starter
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179

    Timer Problem

    Hi,
    I tried to do a ad rotator stuff on a form,but the problem is everytime i tried to load a picture to a dynamically created picturebox during the time_elapsed event, it gave me this error.

    "Run-time exception thrown : System.ArgumentException - Controls created on one thread cannot be parented to a control on a different thread."

    anyone knows anysolution to this problem


    Thanks

  2. #2
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    the timer must run on a different thread than that of the form - use delegates and the forms 'invoke' method to marshall the values between threads. I could post you an example later (out for the next few hours) if that is any good?

  3. #3

    Thread Starter
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    Thanks Powdir!
    It will be nice if you can provide some samples

  4. #4
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    Ok, here a quick example. Sorry i had to use a worker thread as a substitute for a timer callback - i have also never used the AdRotator but i guess the principle will be the same i.e. using the forms invoke method. Hope this points you on the right track

    Create a simple form with a command button and a treeview.

    Code:
    Public Delegate Sub UpdateTreeView(ByVal str As String)
        '
        'set up a worker thread
        'change the addressof this thread to point to AddNodesFail 
        'and see it fail. Then change it back to ThreadingAddNodes
        Public tr As New Threading.Thread(AddressOf ThreadingAddNodes)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            tr.Start()
        End Sub
        '
        Private Sub ThreadingAddNodes()
            Dim UpdateTreeViewDeleg As New UpdateTreeView(AddressOf AddNodes)
            'set up the arguments array that will be marshalled across the to the UI thread
            'from the worker thread
            Dim args() As String = {"Hello from thread "}
            'and send it across to the UI using the forms invoke method
            Me.Invoke(UpdateTreeViewDeleg, args)
        End Sub
        '
        Private Sub AddNodes(ByVal str As String)
            'this sub is used to demo passing arguments across threads
            Dim i As Integer
            For i = 0 To 100
                TreeView1.Nodes.Add(str & CStr(i))
            Next
        End Sub
        '
        Private Sub AddNodesFail()
            'use this sub to test for failure
            Dim i As Integer
            For i = 0 To 100
                TreeView1.Nodes.Add("Hello from failure" & CStr(i))
            Next
        End Sub
    ...that any help?

    Cheers

  5. #5
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    PS...looking at your original question (helps if i read it fully ) it would seem that the dynamically created control will have to be passed as the argument in the invoke method...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width