|
Why do you use a Queue? the main purpose of a queue is to act as a pipe where things get added on one side, stored, and removed on the other side, which inherently means their order is always preserved.
What you need is another kind of collection, such as a simple List<treenode>, on which you can call Sort(). Then provide a Comparer to explain how you compare two TreeNode instances. This[^] explains and illustrates it.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hi,
I am trying to write a vbscript which should give the difference between to given excel sheet. I am actually updating a excel sheet i.e adding a row somewhere in between. I want to vbscript show only the added row as the difference.
Any help will do a lot.
Thanks in advance for your help.
|
|
|
|
|
In general, if I start a thread in sub-1 that will thread sub-2...
when sub-2 runs (and sub-1 may never run again), once sub-2 has completed, can I start the sub-2 thread at the end of sub-2 before it finishes?
In other words, sub-1 started the thread for sub-2, then sub-1 ends.
Sub-2 runs and I want it to run over and over in the background..
So can I initiate the sub-2 thread from within (at the end of) sub-2?
Can a sub call its own thread?
Hope I made it clear?
Thanks
|
|
|
|
|
crashcup wrote: Hope I made it clear?
No you didn't; actual code would be easier to understand.
crashcup wrote: Can a sub call its own thread?
That doesn't make any sense. Nothing can call a thread; a thread executes code.
Yes a method A can start a thread that runs method B.
and yes a method A can start a thread that runs method A (Some kind of recursion, dangerous, and most often not very sensible).
If some code needs to run all the time, why not run it in a loop (possibly by one thread)?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Message Closed
modified 5-Dec-16 6:40am.
|
|
|
|
|
you seem confused.
A method is like a job, a piece of work that needs to be done.
A thread is a worker, something that performs a job.
A process start off having a single thread, and starts executing your Main() method.
You can write:
public void someJob() {
while(!done) {
doSomething();
}
}
and now you can let your main thread execute someJob() by simply calling it.
Or you can start one more thread that executes someJob(), which causes doSomething() to run forever (or until done is set true).
That is the scheme you most often need, e.g. when dealing with TCPListener. No need to create threads all the time.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Thanks, no confusion here as I've been programming for years, just not in .Net.
I'll accept Daves answer as it relates directly to my question about subs started by a thread.
|
|
|
|
|
yes you are confused, and your vocabulary is wrong.
threads don't start a "sub", in fact there are no "subs".
it is code (inside methods) that start some Thread, then the thread executes the code in the method given to it.
Also what Dave and I are telling you is basically the same. Use a loop. I have shown you code that does that.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hmm still no confusion here.
As you can see below the Sub Read() is indeed started by a thread.
And the Read Sub is started from whthin itself.
The program works fine.
And there are Subs as I have 3, 'connect', 'send', and 'read'.
My vocabulary is correct.
These are also considered methods I suppose. But VB calls them Subs or Functions.
Sub procedures are methods which do not return a value.
Function is a method which returns a value.
---------------------------------------------------------------
Public Sub connect(ByVal IP, ByVal Port)
Try
'ccSocket.Connect("127.0.0.1", 2000)
ccSocket.Connect(IP, Port)
Catch ex As Exception
End Try
myThread = New Thread(AddressOf read)
myThread.Start()
End Sub
Public Sub send(ByRef q)
'Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("connect")
'serverStream.Write(outStream, 0, outStream.Length)
'serverStream.Flush()
'Dim serverStream As NetworkStream
Try
serverStream = ccSocket.GetStream()
Dim sendBytes As [Byte]()
sendBytes = Encoding.ASCII.GetBytes(q)
serverStream.Write(sendBytes, 0, sendBytes.Length)
Catch
End Try
End Sub
Public Sub read()
'myThread = New Thread(AddressOf read)
'myThread.Start()
' Read the NetworkStream into a byte buffer.
Dim bytes(ccSocket.ReceiveBufferSize) As Byte
Try
Dim serverStream As NetworkStream
serverStream = ccSocket.GetStream()
serverStream.Read(bytes, 0, CInt(ccSocket.ReceiveBufferSize))
' Output the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
''Console.WriteLine(("Host returned: " + returndata))
DataIn = returndata
Catch ex As Exception
End Try
Try
If Asc(DataIn) = 0 Then
myThread.Abort()
DataIn = "Connection Closed"
End If
Catch ex As Exception
Exit Sub
End Try
Try
myThread = New Thread(AddressOf read)
myThread.Start()
Catch ex As Exception
End Try
End Sub
modified on Monday, November 9, 2009 5:53 AM
|
|
|
|
|
Luc Pattyn wrote: You can write:
public void someJob()
{
while(!done)
{
doSomething();
}
}
Does that work in VB.NET?
Steve Jowett
-------------------------
Real programmers don't comment their code. If it was hard to write, it should be hard to read.
|
|
|
|
|
what have you tried so far?
did you check the documentation?
ask Google!
hint: probably not, not enough capitals used.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
A thread doesn't call "it's own sub" as threads are not limited to a single sub, as you put it. You setup a loop inside the sub to keep itself running until some bailout condition is met. Using Thread.Abort is a poor choice to end a thread.
|
|
|
|
|
You wrote " A thread doesn't call "it's own sub""
I have done just this and things seem to work ok, or have I inadvertently started a memory leak?
You wrote "You setup a loop inside the sub to keep itself running until some bailout condition is met."
So to clarify, I guess you are saying once a sub is started as a thread, inside that sub, if you want it to continue to run (like TCPListener) keep looping as long as you want it to listen.
"Using Thread.Abort is a poor choice to end a thread."
As an aside, what is the proper method to terminate the thread and can it be terminated from within the 'threaded' sub?
|
|
|
|
|
crashcup wrote: I have done just this and things seem to work ok, or have I inadvertently started a memory leak?
crashcup wrote: As an aside, what is the proper method to terminate the thread and can it be terminated from within the 'threaded' sub?
Your description is a bit vague, but, you may have done that. You do not need to keep launching a new thread to execute the sub again. Like I said, you can setup a loop in the sub that keeps executing until a bailout condition is met, such as a boolean variable (flag) being set. With each iteration of the loop, it should check to see if this flag is set and, if so, bail out of the loop and let the code end itself, which also kills the thread automatically.
|
|
|
|
|
Thanks. I'll accepth this answer, and change my sub to keep looping.
|
|
|
|
|
For future reference, I was only using your terminology. If you want to avoid some confusion later on (like someone mistaking you for VB6 code), use the term "method" instead of "sub". "sub" and "function" are just syntax leftovers from the old VBx days.
|
|
|
|
|
Hey guys well i am trying to sort an array that is already filled with string data at form load. in 4 different labels the array displays the values. When a user click an arrange button I am trying to sort each element in the labels in ascending order. So for example the labels are loaded with this data: (please HELP)
'this code is at form LOAD and sends each value in the array to each label one at a time
For i As Integer = 0 To ShuffleCards.cards2.Length - 1 Step 4
'gets the corresponding value from the array and pass to labelbox
lblPlayer1.Text += ShuffleCards.cards2(i)
lblPlayer2.Text += ShuffleCards.cards2(i + 1)
lblPlayer3.Text += ShuffleCards.cards2(i + 2)
lblPlayer4.Text += ShuffleCards.cards2(i + 3)
Next 'end for
'*********************************************************
'this next code is where i am having trouble trying to sort in ascending order each value the label already has in it
'RIGHT NOW ALL IT IS DOING IS ONLY DISPLAYING ONE VALUE AND NOT THE CORRECT DATA THAT THE LABEL HAS ALREADY BEEN LOADED
'WITH
Private Sub btnArrange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArrange.Click
'*************************************************
'this sub will sort the players cards in increasing
'order using sort
Dim j, temp, count As Integer
lblPlayer1.Text = ""
lblPlayer2.Text = ""
lblPlayer3.Text = ""
lblPlayer4.Text = ""
For i As Integer = 0 To ShuffleCards.cards2.Length - 1 Step 4
'this will sort your array in ascending order
For count = 0 To ShuffleCards.cards2.Length - 1
For j = count + 1 To ShuffleCards.cards2.Length - 1
If ShuffleCards.cards2(count) > ShuffleCards.cards2(j) Then
ShuffleCards.cards2(count) = ShuffleCards.cards2(j)
ShuffleCards.cards2(j) = temp
End If
Next j
Next count
lblPlayer1.Text += ShuffleCards.cards2(temp)
lblPlayer2.Text += ShuffleCards.cards2(temp + 1)
lblPlayer3.Text += ShuffleCards.cards2(temp + 2)
lblPlayer4.Text += ShuffleCards.cards2(temp + 3)
Next
End Sub
|
|
|
|
|
Hi,
arrays and collections in .NET have a Sort() method that takes care of it, with a default sort order depending on element type, and a way to specify your own sort order; I explained it here[^].
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
PAguilar09 wrote: For count = 0 To ShuffleCards.cards2.Length - 1
For j = count + 1 To ShuffleCards.cards2.Length - 1
If ShuffleCards.cards2(count) > ShuffleCards.cards2(j) Then
ShuffleCards.cards2(count) = ShuffleCards.cards2(j)
ShuffleCards.cards2(j) = temp
End If
Next j
Next count
you must set a value for temp when swapping two cards, a normal swap takes 3 statements inside the IF.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Okay i tried fixing my code but I am still not getting my labels to sort the values they contain in ascending order??
the labels look like this at form load:
Player 1: D7 HK D2 CK D10 H5 HA C4 C6 C7 H2 SJ D3
Player2: DA S6 HQ CJ DQ D9 CQ H6 C3 SA S8 D5 D8
When i hit Arrange button the output should
sort them in ascending order: P1: C4 C6 D2 D3 D7 D10 H2 H5 HK HA SJ
AND SO ON AND SO ON FOR 4 OTHER LABELS
but i dont know what I am missing or not doing correctly??? Please help
For i As Integer = 0 To ShuffleCards.cards2.Length - 1
'this will sort your array in ascending order
' For count = 0 To ShuffleCards.cards2.Length - 1 Step 4
For j = count + 1 To ShuffleCards.cards2.Length - 1
If ShuffleCards.cards2(count) > ShuffleCards.cards2(j) Then
ShuffleCards.cards2(count) = ShuffleCards.cards2(j)
'temp = ShuffleCards.cards2(j)
lblPlayer1.Text += ShuffleCards.cards2(j)
lblPlayer2.Text += ShuffleCards.cards2(j + 1)
lblPlayer3.Text += ShuffleCards.cards2(j + 2)
lblPlayer4.Text += ShuffleCards.cards2(j + 3)
End If
Next j
'Next count
Next
|
|
|
|
|
1.
you are not following up on earlier replies.
I told you a swap needs 3 statements, you had 2, and now you only have 1.
If you were to use the available Sort() method, you would need less of your code, and hence would have fewer bugs.
2.
what kind of order is "C4 C6 D2 D3 D7 D10 H2 H5 HK HA SJ"?
- it is not numeric as these aren't numbers;
- it is not alphabetic as then D10 would precede D2, and HA would precede HK.
Hence you need your own Comparer; read my article on sorting.
3.
I think you're not up to the job you set yourself.
I recommend you read a basic book on VB.NET and work your way through it.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
i tried using .Sort()but i just keep getting an error. In class we were told to use a two-dim sort to arrange this type of array containing values such as: "DK C2 SJ H10"
|
|
|
|
|
hi,how are you?
What code would do this?
Please help me.
Thanks!
|
|
|
|
|
I found the answer
The code is
Private Sub FillComboBox()
Tv.PathSeparator = ">>>"
Dim nodes As New List(Of TreeNode)
Dim queue As New Queue(Of TreeNode)
Dim top As TreeNode
Dim nod As TreeNode
For Each top In Tv.Nodes
queue.Enqueue(top)
Next
While (queue.Count > 0)
top = queue.Dequeue
nodes.Add(top)
For Each nod In top.Nodes
queue.Enqueue(nod)
Next
End While
' nodes.Sort(System.Collections.Generic.IComparer(Of TreeView))
For i As Integer = 0 To nodes.Count - 1
GroupQuestionsCbo.Items.Add(nodes(i).FullPath)
Next
End Sub
|
|
|
|
|
I'm glad you found your answer, because your question was very ambiguous.
Steve Jowett
-------------------------
Real programmers don't comment their code. If it was hard to write, it should be hard to read.
|
|
|
|