-
Mar 23rd, 2023, 05:16 AM
#1
Thread Starter
Hyperactive Member
[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?
-
Mar 23rd, 2023, 05:33 AM
#2
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.
-
Mar 23rd, 2023, 06:49 AM
#3
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:
Me.Convtxt.Invoke(New MethodInvoker(Function()
Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine)
End Function))
would correctly be written like this:
vb.net Code:
Me.Convtxt.Invoke(Sub() Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine))
Of course, neither is the correct way to update the UI when using a BackgroundWorker.
-
Mar 23rd, 2023, 09:50 AM
#4
Thread Starter
Hyperactive Member
Re: Correct way to "MethodInvoke" on Option Strict
 Originally Posted by jmcilhinney
vb.net Code:
Me.Convtxt.Invoke(Sub() Convtxt.AppendText("YOU: " + RECEIVE + vbNewLine))
Works just fine.
 Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|