Results 1 to 7 of 7

Thread: Backgroundworker and Clipboard.Gettext()

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2017
    Posts
    147

    Backgroundworker and Clipboard.Gettext()

    i have a function that i can call from the main thread that works fine, i want to add it to a background worker as it is part of a long running loop
    Code:
       Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        lots of procedures
    
    dim clipboardtext as string = clipboard.gettext()
    
    lots of procedures
        End Sub
    the problem is now the clipboard always returns empty, i assumed it is a threading issue so i have tried

    Code:
       Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        lots of precedures
    
            Me.Invoke(Sub()
                              GettingClipBoardText()
                          End Sub)
    
    
    
    lots of procedures
        End Sub
    
    and added to the main form
    
        Public Function GettingClipBoardText()
            ClipBoardText = Clipboard.GetText
        End Function
    System.InvalidOperationException: 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

    any ideas?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Backgroundworker and Clipboard.Gettext()

    That error message seems to indicate that that code is being executed before the form is displayed. The Invoke method invokes a method on the thread that owns the control's window handle. If the form hasn't been displayed then the handle hasn't been created, so there's no thread that owns it. I'm not sure why your form hasn't been shown at that stage but, if you were going to do it that way, you might need to start your BackgroundWorker from the Shown event handler of the form, to ensure that it was already displayed.

    That said, you shouldn't do it that way. Why would you? What's the point of starting a BackgroundWorker to execute on a background thread and then immediately invoking to the UI thread? Just get the Clipboard text first, before starting the BackgroundWorker. Pass the text as an argument to RunWorkerAsync and then get it back in the DoWork event handler from the e.Argument property.

  3. #3
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Re: Backgroundworker and Clipboard.Gettext()

    The forum logged me out before I could get my response done, while multitasking other things... You should not run background threads on UI threads and vice verse, instead you should pass one to another.

    e.g.

    Code:
     
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
    
            ' Access the clipboard text directly from the background thread
            GettingClipBoardText()
    
            ' ... lots of procedures
        End Sub
    
        Public Sub GettingClipBoardText()
            ' Access the clipboard text directly from the background thread
      
            clipboardtext = Clipboard.GetText()
        End Sub
    End Class
    It is possible that its another piece of the code that is causing the overall function to fail. And without more than a small tidbit there is not much more anyone can do but guess.
    Disclaimer: When code is given for example - it is merely a example.




    Unless said otherwise indicated - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2017
    Posts
    147

    Re: Backgroundworker and Clipboard.Gettext()

    i have altered my code not to rely on clipboard.gettext as it seems to have issues when called continuously from a loop

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,372

    Re: Backgroundworker and Clipboard.Gettext()

    You could hook the clipboard and only retrieve the contents when it has changed…
    Would that be helpful?

  6. #6

  7. #7
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Backgroundworker and Clipboard.Gettext()

    Assuming youre not setting the text somewhere in your "lots of procedures" and you are setting the text by means of Ctrl+C or right-click Copy, then you should be sending the text in as a parameter and getting it back out as such, as jmc has pointed out.

    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         BGW.RunWorkerAsync(Clipboard.GetText)
     End Sub
    
     Private Sub BGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
         Dim ClipObj As Object = e.Argument
         Dim ClipTxt As String = String.Empty
         If ClipObj IsNot Nothing Then
             ClipTxt = ClipObj.ToString
    
             'Do Stuff
    
             'Send clip text back to UI thread if needed
             e.Result = ClipTxt
         End If
     End Sub
    
     Private Sub BGW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
         If e.Error Is Nothing Then
             Dim ClipText As String = e.Result.ToString
         End If
     End Sub

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