Results 1 to 4 of 4

Thread: [RESOLVED] Correct way to "MethodInvoke" on Option Strict

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Resolved [RESOLVED] Correct way to "MethodInvoke" on Option Strict

    Hi, Consider following line will appending text in textbox while BackgroundWorker is running asynchronously. (Based on previous treads and regarding to jmcilhinney pointed BackgroundWorkers are for huge process-occupying things not UI, you have to make it a sort of pause or invoke to access UI update thing)
    Code:
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            While (CLIENT.Connected)
                Try
                    RECEIVE = STREAM.ReadLine
                    Me.Convtxt.Invoke(New MethodInvoker(Function()
                                                            Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine)
                                                        End Function))
                    RECEIVE = ""
                Catch ex As Exception
                    MessageBox.Show(ex.Message.ToString())
                End Try
            End While
        End Sub
    This code works with warnings on underlined line. But since I'm using Option Strict = On, Mentioned line throws an error:
    Cannot infer a return type. Consider adding an 'As' clause to specify the return type.

    How can I rectify it?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Correct way to "MethodInvoke" on Option Strict

    This line:-
    Code:
    Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine)
    It is the direct cause of the problem. That doesn't return a value so the compiler cannot infer the return type of the anonymous function where's it's being called from.

    Use a Sub instead of a Function for the MethodInvoker call. Assuming that Convtxt is a Control, you could also do away with the MethodInvoker altogether.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Correct way to "MethodInvoke" on Option Strict

    You clearly still haven't paid attention to what you were told if you're asking this question at all. The whole point of the BackgroundWorker is so that you can just call methods and handle events, just as you always do, rather than using Invoke at all. I provided you with a link to a thread that explains how to use a BackgroundWorker and, in that thread, i specific explain and demonstrate that, if you want to update the UI during the background work, you call ReportProgress and handle the ProgressChanged event. Why even ask for help if you're just going to ignore it when it's provided?

    If you were going to use Invoke, which you shouldn't if you're using BackgroundWorker, then lambdas follow the sane rules as named methods: a Function returns a value and a Sub doesn't. This:
    vb.net Code:
    1. Me.Convtxt.Invoke(New MethodInvoker(Function()
    2.                                         Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine)
    3.                                     End Function))
    would correctly be written like this:
    vb.net Code:
    1. Me.Convtxt.Invoke(Sub() Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine))
    Of course, neither is the correct way to update the UI when using a BackgroundWorker.

  4. #4

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: Correct way to "MethodInvoke" on Option Strict

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. Me.Convtxt.Invoke(Sub() Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine))
    Works just fine.

    Quote Originally Posted by jmcilhinney View Post
    You clearly still haven't paid attention to what you were told...
    ...Of course, neither is the correct way to update the UI when using a BackgroundWorker.
    Relax... I know... I already read that thread... Used it for progressbar purposes... It's beautifully done... Here is just testing... Not a big deal... Thanks for being loyal and adherence to the fact "What is the correct way" that's exactly what I'm looking for. See the thread title? Love ya

Tags for this Thread

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