Author |
Message
|
PeterPotkay |
Posted: Fri May 13, 2005 6:09 pm Post subject: VB.NET coding question |
|
|
 Poobah
Joined: 15 May 2001 Posts: 7722
|
(fiddeling with VB programming, trying to learn...be gentle)
Stuck on this problem, and cant finger it out. I have a method called Reconnector. It takes 1 argument a string. I can start it up fine passing in a string.
Code: |
Reconnector("Hello")
.
Private Sub Reconnector(ByVal myPassedInString As String)
' do something with that string value
End Sub
|
If I change reconnector to not take in a string, I can startup threads of it like so:
Code: |
Dim ReconnectorThread1 As New System.Threading.Thread(AddressOf Reconnector()
ReconnectorThread1.Start()
.
..
Private Sub Reconnector()
' do something
End Sub
|
But I cannot combine the 2 styles. I want to be able to call up multiple threads of this method, passing each one a different value to work with. I thougt the below is pretty logical, yet it won't take. It complains that "AddressOf operand must be the name of a method, no parens needed".
Code: |
.
Dim ReconnectorThread1 As New System.Threading.Thread(AddressOf Reconnector("Hello"))
ReconnectorThread1.Start()
Dim ReconnectorThread2 As New System.Threading.Thread(AddressOf Reconnector("Goodbye"))
ReconnectorThread2.Start()
.
..
Private Sub Reconnector(ByVal myPassedInString As String)
' do something with that string value
End Sub
|
Any ideas? Surely this can be done. _________________ Peter Potkay
Keep Calm and MQ On |
|
Back to top |
|
 |
fjb_saper |
Posted: Sat May 14, 2005 4:16 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
Peter,
What is the exact purpose of the method? I doubt that it is about creating new threads. Looks more like reconnecting existing or "lost" ones.
So what is the accepted values for the string? "0xabfe235" i.e. a hex value for a thread id ?
It says (AddressOf Reconnector("Hello"))
Since when is Reconnector("Hello") and address ??
And sometimes like java .Net likes it's operands to be passed without work:
xyz = Reconnector("Hello").getAddress() ???
Thread.....(xyz) ...
Sorry don't have the .Net info at hand so the above is mostly conjecture.
But for your needs: (Sorry the code is more c# like. You'll have to find the equivalent for VB.Net)
Why not change a little bit the design of the class.
Add a static properties / hash table to your class: reconstructor_props
change the constructor: arg passed = string name
reconstructor_props.put(name, this)
So when you want to restart a particular instance
Code: |
static void Reconstructor.start(string name){
Reconstructor myinst = Reconstructor.reconstructor_props.get(name)
myinst.start()
}
|
That is assuming that your instance keeps the thread as an instance variable and that its start method is the standard overload for that case.
Now you still may have to deal with things like thread dead exceptions etc...
As well you might want to do a specific thread resume instead of start....
Enjoy  |
|
Back to top |
|
 |
jefflowrey |
Posted: Sat May 14, 2005 8:20 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
I think this is what's going on But I'm not .Net savvy enough to help.
You're trying to pass a reference to a function - but when you use fun_name(arguments) you are actually probably CALLING the function.
That is, the function name as a bareword evaluates to a reference of some kind to the function. The function name with any sort of parens after it evaluates to a call to the function.
I think you need to create your own subclass of System.Threading.Thread. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
PeterPotkay |
Posted: Sat May 14, 2005 8:26 am Post subject: |
|
|
 Poobah
Joined: 15 May 2001 Posts: 7722
|
I tried to simplify the examples above, but all the reconnector method does is client connect to a QM, open a few queues, and loop on MQINQ calls every few seconds. If the connection to the QM is broken, it will loop on trying to reconnect. I got that part down, the next step is to have the main program spawn off up to 12 threads, each thread connecting to a different QM, so that my GUI shows 12 QMs, and each QM has a few queues' depths displayed and refreshed every couple of seconds. (I already have tools that do this, I am just playing around with coding).
So what I want to do is have my main program read in all the properties from a file, and then start spawning these threads (up to 12). To each thread, I need to pass the hostname, channel name, port #, and a few q names. I can pass the args to the method OK if I don't try and start it as a separate thread, and I can start it up as a thread if I don't try and pass those values.
How do I combine the 2 ways of calling this method is the problem. _________________ Peter Potkay
Keep Calm and MQ On |
|
Back to top |
|
 |
PeterPotkay |
Posted: Sat May 14, 2005 8:32 am Post subject: |
|
|
 Poobah
Joined: 15 May 2001 Posts: 7722
|
jefflowrey wrote: |
I think you need to create your own subclass of System.Threading.Thread. |
ugh.....duh.....Where's the "Any" key?, as good ol' Homer would say.
I'll keep banging my head with this. I also sent an email to a couple of .NET dudes at my company. I'll post what I come up with. _________________ Peter Potkay
Keep Calm and MQ On |
|
Back to top |
|
 |
fjb_saper |
Posted: Sat May 14, 2005 4:19 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
I think you are trying to go a little bit too deep into the system.
Isn't there something like the java Runnable interface in .Net like
Code: |
[runnable]
public class Myclass{
public void start(){...}
public void stop(){...}
public void run(){.....}
...}
|
And remember just like in java the memory in the CLR is system managed so don't try and address the memory. What you need is an object oriented design with a reference to the object instance (the class doing the work on your qmgr)
So a static collection holding those references is what I would go for.
Instead of passing the arg by val pass it by ref.
Your class will have to manage the state of the collection. (adds, deletes, status updates to the instances (running, thread dead, suspended etc..)
Enjoy  |
|
Back to top |
|
 |
jefflowrey |
Posted: Sun May 15, 2005 9:46 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
PeterPotkay wrote: |
jefflowrey wrote: |
I think you need to create your own subclass of System.Threading.Thread. |
ugh.....duh.....Where's the "Any" key?, as good ol' Homer would say.
I'll keep banging my head with this. I also sent an email to a couple of .NET dudes at my company. I'll post what I come up with. |
Something vaguely like
Code: |
class ReconnectorClass
inherits System.Threading.Thread
Private myThreadName as String
Property ThreadName() as String
Get
Return myThreadName
End Get
Set
...
End Set
End Property
.... |
Then you can do
Code: |
Dim ReconnectorThread1 as New ReconnectorClass
Reconnector.Thread1.setThreadName("hello") |
And etc.
MSDN has some reasonably good "intro to VB" stuff that should show you a bit more on classes and inheritance. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
PeterPotkay |
Posted: Tue May 17, 2005 5:01 pm Post subject: |
|
|
 Poobah
Joined: 15 May 2001 Posts: 7722
|
OK, how about this:
This works:
Code: |
Dim ReconnectorThread1 As New System.Threading.Thread(AddressOf Reconnector()
Me.Name = "Homer"
ReconnectorThread1.Start()
.
..
Private Sub Reconnector()
MyLittlePlaceHolder = Name 'save the current name
' do something
' now drop int a loop of some sort
' start loop
TextBox1.Text = MyLittlePlaceHolder 'will display the name saved name, each time in the loop
' do some othe stuff
' end loop
End Sub
|
But is this safe? As the three threads are spinning, will each one always maintain its unique value for MyLittlePlaceHolder? Or as soon as ReconnectorThread2 says "MyLittlePlaceHolder = Name ", both it and #1's MyLittlePlaceHolder change to Bart? Is this a safe design?
Code: |
Dim ReconnectorThread1 As New System.Threading.Thread(AddressOf Reconnector()
Me.Name = "Homer"
ReconnectorThread1.Start()
Dim ReconnectorThread2 As New System.Threading.Thread(AddressOf Reconnector()
Me.Name = "Bart"
ReconnectorThread2.Start()
Dim ReconnectorThread3 As New System.Threading.Thread(AddressOf Reconnector()
Me.Name = "Moe"
ReconnectorThread3.Start()
.
..
Private Sub Reconnector()
MyLittlePlaceHolder = Name 'save the current name
' do something
' now drop int a loop of some sort
' start loop
TextBox1.Text = MyLittlePlaceHolder 'will display the name saved name, each time in the loop
' do some othe stuff
' end loop
End Sub
|
_________________ Peter Potkay
Keep Calm and MQ On |
|
Back to top |
|
 |
fjb_saper |
Posted: Tue May 17, 2005 7:20 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
How about this:
Code: |
[Runnable]
public Myclass{
public static Properties myinstances = null;
private System.threading.Thread mythread = null;
// creator
public Myclass(string plname){
super();
if (myinstances==null) myinstances = new Properties();
myinstances.put(plname, this);
}// end creator
public void start(){
if (mythread == null){
mythread = new System.threading.Thread(this);
mythread.start();
}else {
..........
}// endif
}// end start method
public void static start(string myid) throws Exception {
Myclass myinst = (Myclass) myinstances.get(myid);
myinst.start();
}// end overlayed method
public void run(){............}
.............
} // end class |
All you need to do is find the equivalent in VB.NET. This is more like C# or J#
Enjoy  |
|
Back to top |
|
 |
|