Retrieve Data/Variables Produced by a Thread in VB.NET - vb.net

I want to run some processing-intensive code in a thread upon starting Excel, and have included a simplified example below. A call to clsInit.Main is the starting point. The code executed in this thread (Sub DoIt) then modifies a variable (rng) that will be saved for use later when I access it by calling Sub GetIt. It doesn't work. Why? Thanks for the help.
Imports System.Threading
Public Class clsInit
Shared Sub Main()
Dim t As New Thread(AddressOf clsTest.DoIt)
t.Start()
End Sub
End Class
Public Class clsTest
Private Shared rng As Excel.Range
Public Shared Sub DoIt()
rng = Globals.ThisAddin.Application.ActiveCell
End Sub
Public Shared Sub GetIt()
MsgBox(rng.Address)
End Sub
End Class

Related

Multi-Threading (Calling Sub From WorkerThread)

Need to call a sub that is coded written inside the block of form1 form an external worker thread. This is what I have written:
In Form1:
Public Delegate Sub UpdateControlDelegate(ByVal C As Label, ByVal txt As String)
Private Sub UpdateControl(ByVal C As Label, ByVal txt As String)
If C.InvokeRequired Then
C.Invoke(New UpdateControlDelegate(AddressOf UpdateControl), New Object() {C, txt})
Else
C.Text = txt
End If
End Sub
Public Sub DoStuff()
'we do some stuff then when it comnes time update a certain control:
Call UpdateControl(MyLabel, "My Text For The Label)
End Sub
In The workerThread that is located in a class:
Public Class MyClass
Public Sub UpdateData
Call Form1.DoStuff
End Sub
End Class
Does this look correct? The most simplest terms on what I am trying to achieve:
WorkerThread to call a Sub that is located in Class Form1
and that sub contains code that updates a couple controls in Form1.
After doing a little more research. I have figured it out. The initial code I have written is correct. The only thing missing is a reference to the form I need to update.
Here is the COMPLETE solution when needing to run a SUB from the UI that is called from the Worker Thread:
Public Class MyClass
'working thread is being within the subs of this class
Public MyForm1111 As Form1 '<------ The variable in this class that will reference to the form1 that we need
Public Sub MySubThatIsOnAWorkerThread
MyForm1111.DoStuff '<==== must call MyForm1111.DoStuff and NOT Form1.DoStuff
End Sub
End Class
The Sub Located In Form1:
Public Class Form1
Public Delegate Sub UpdateControlDelegate(ByVal C As Label, ByVal txt As String) 'Required Delegate
Private Sub UpdateControl(ByVal C As Label, ByVal txt As String) 'Sub to update controls
If C.InvokeRequired Then
C.Invoke(New UpdateControlDelegate(AddressOf UpdateControl), New Object() {C, txt})
Else
C.Text = txt
End If
End Sub
Public Sub DoStuff() 'the sub we need to call from the worker thread
'do some calculations and code
Call UpdateControl(MyLabel, "Some Text For Label")
End Sub
Private Sub Form1_Load()
MyClass.MyForm1111 = Me <==== Set the reference here in your Form1_Load
End Sub
End Class

Communicating with a class that's in use in VB.NET

I'm trying to communicate with a class that's already in use, something like this.
Assume "PROJECTNAME" as a namespace or the whole project
Class1:
Public Class Class1
Dim Text1 As String
Sub ChangeString(input as String)
Text1 = String
End Sub
Sub Main()
Do
' Code to prevent the Process stopping (Ignore This)
Loop
End Sub
End Class
Class 2:
Imports PROJECTNAME
Public Class Class2
Dim ForeignClass1 As New Class1
Dim ForeignClass2 As New Class1
Sub Main()
Do
'Code to prevent the Process stopping(Ignore This)
Loop
End Sub
End Class
Class 3:
Imports PROJECTNAME
Public Class Class3
Dim CustomClass As New Class2
Sub Main()
'Code here to interact between "ForeignClass2" and "ForeignClass1"
End Sub
End Class
The codes above is a concept that I'm trying to explain, not the actual code as it's a private code that I'm not suppose to expose. So what I want is to make "ForeignClass2" interact with "ForeignClass1" or make "ForeignClass1" interact with "ForeignClass2" without touching or making a new "Class1" array and just "ForeignClass1" and "ForeignClass2" only. How can I do that?

VB.NET: Declaration expected error when starting a new thread

Public Class Class1
Dim thread As New System.Threading.Thread(AddressOf AMethod)
thread.Start()
Public Sub AMethod()
Console.writeline("Thread start")
End Sub
End Class
The "thread.Start()" is the problematic line, according to vb.
Declaring the thread in a sub gives an overload error, whatever that means.
If you didn't understand what #Plutonix meant by "executable code floating around" (I love that phrase) here is an example:
Public Class Class1
Dim thread As New System.Threading.Thread(AddressOf AMethod)
Public Sub StartingThread()
thread.Start()
End Sub
Public Sub AMethod()
Console.WriteLine("Thread start")
End Sub
End Class
See, the executable code thread.Start() is now inside a method. I, myself, am steering clear of threading until I know much more about it.

Access variable in Shared Sub

is there a way to access a variable in Form_Load from an event handler?
Please dont mind the code, this is just a representation of my question.
Public Class Form
Public Sub Form_Load()
Dim x as string
x = MyClass.MethodGetValue()
End Sub
Private Shared Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub
End Class
It's about the scope of the variable. In your situation you need a class variable. This allows it to be used anywhere inside of this class.
Public Class Form1
Private x As Object 'pick the datatype that matches your needs
Public Sub Form_Load()
x = MyClass.MethodGetValue()
End Sub
Private Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub
End Class

Variable Protection Level in Script

I am creating a script programatically. However I cannot access the dataset from the modeule. See "Sample" below. ('ds' is not declared. It may be inaccessible due to its protection level.)
In the Sub main() I can access ds without a problem
In the Sub test() in Cacl_Module I cannot access ds.
Namespace Evaluator
Public Class Evaluator
Public ds As New DataSet
Public ComboBox1 As New ComboBox
Public CheckBox1 As New CheckBox
Public TextBox1 As New TextBox
Sub main()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Class
Public Module Calc_module
Sub test()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Module
End Namespace
That's because ds is a member of the Evaluator class. In the Main method of the Evaluator class, you can access its members. But in the test method of the Calc_Module, you don't have an instance of Evaluator. You should create one, like that:
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
Debug.Print(MyEvaluator.ds.Tables("Table1").Rows.Count)
Debug.Print(MyEvaluator.CheckBox1.Checked)
End Sub
End Module
Or even better (no need to duplicate code)
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
MyEvaluator.main()
End Sub
End Module