473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Thread issue

I create a thread where I pass thru a message. When I click very fast many
times (like 50 times) to create 50 threads, the message did not get pass
thru ProcessMessage.
For example: strBuffer = "#TRADE, D1410-123456, BUY, 1, ESM7, DAY, LIMIT,
1490.00, , , 0, 0, 0, 0, 0, links |52994/25/2007 10:47:17 AM !A", when I
trigger to create many threads (like 50), this message did not get to sub
ProcessMessage in clsEachMessage.
I have added Thread.Sleep(0) after the Start method of the thread.
What is the best way to do the thread so that every message get pass thru to
it ?
Is there a maximum number of thread that I can create ?
Shall I use thread pool instead of creating a new thread each time ?
Thank you very much.

Dim EachMessageThread As Threading.Thread = Nothing
Dim clsEachMessage As New EachMessage

clsEachMessage.sMessage = strBuffer
EachMessageThread = New Threading.Thread(AddressOf
clsEachMessage.ProcessMessage)
threadCount += 1
EachMessageThread.Name = "Thread " & threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)

Public Class EachMessage
Public sMessage As String

Public Sub ProcessMessage()
Dim swError As StreamWriter

swLog = New StreamWriter(sPath & "\log\Input.txt", True)
swLog.Write(sMessage & vbCrLf)
swLog.Close()
swLog = Nothing
Apr 25 '07 #1
4 1404
fniles wrote:
<backposted/>

I don't think the problem has to do with you creating threads in a
rapid (!) sequence. It seems to me it has more to do with the lack of
synchronization between your threads when accessing the file.

The problem is that I can only suppose what is really going on,
because the code you posted is incomplete and has some logical
inconsistencies (I assume you typed the code directly in the message
instead of copy-pasting).

Anyway, it becomes somewhat evident that you want to create a
multithreaded log system. If this is the case, then you must consider
that logging must respect a first in, first out sequence. Also,the
file system may complain of multiple writing attempts from different
threads to the same file. Both issues require some sort of thread
synchronization.

A possible approach would be to have a logging class that each thread
shared (instead of creating a different class for each thread).
Something in the lines of:

<code>
Public Class Log
Private mLogFile As String
Public Sub New(ByVal Path As String)
mLogFile = Path
LogMessage("")
End Sub

Public Sub LogMessage(ByVal Text As String)
SyncLock Me
System.IO.File.AppendAllText(mLogFile, Text)
End SyncLock
End Sub

End Class
</code>

You can test the class with the following:

<aircode>
'At module or class level
Const LOG_PATH = "..."
Private mLog As New Log(LOG_PATH)

'...
Sub SaveMessage(Msg As Object)
Static Random as New System.Random
Dim T As Integer
SyncLock Random
T = Random.Next(50, 300)
End SyncLock
System.Threading.Thread.Sleep(T)
mLog.LogMessage(Msg.ToString & ControlChars.CrLf)
End Sub

Sub TestLog
For I As Integer = 1 to 50
Dim Msg As String= "Message #" & I.ToString
Dim T As New System.Threading.Thread(AddressOf SaveMessage)
T.Start(Msg)
Next
End Sub
</aircode>

HTH.

Regards,

Branco.
I create a thread where I pass thru a message. When I click very fast many
times (like 50 times) to create 50 threads, the message did not get pass
thru ProcessMessage.
For example: strBuffer = "#TRADE, D1410-123456, BUY, 1, ESM7, DAY, LIMIT,
1490.00, , , 0, 0, 0, 0, 0, links |52994/25/2007 10:47:17 AM !A", when I
trigger to create many threads (like 50), this message did not get to sub
ProcessMessage in clsEachMessage.
I have added Thread.Sleep(0) after the Start method of the thread.
What is the best way to do the thread so that every message get pass thru to
it ?
Is there a maximum number of thread that I can create ?
Shall I use thread pool instead of creating a new thread each time ?
Thank you very much.

Dim EachMessageThread As Threading.Thread = Nothing
Dim clsEachMessage As New EachMessage

clsEachMessage.sMessage = strBuffer
EachMessageThread = New Threading.Thread(AddressOf
clsEachMessage.ProcessMessage)
threadCount += 1
EachMessageThread.Name = "Thread " & threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)

Public Class EachMessage
Public sMessage As String

Public Sub ProcessMessage()
Dim swError As StreamWriter

swLog = New StreamWriter(sPath & "\log\Input.txt", True)
swLog.Write(sMessage & vbCrLf)
swLog.Close()
swLog = Nothing

Apr 26 '07 #2
Thank you for your reply.
I actually do not want to create a log system. In my sample code, I write to
a log file to check what string I receive in the thread.

Your method creates the class once that each thread shared, instead of
creating a different class for each thread. Will it be a problem if I create
a different class for each thread ? Will it be slower ?
If I create a different class for each thread then call threadpool like the
following, is that ok ?
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
clsEachMessage.ProcessMessage))

Thank you.

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@n15g2000prd.googlegro ups.com...
fniles wrote:
<backposted/>

I don't think the problem has to do with you creating threads in a
rapid (!) sequence. It seems to me it has more to do with the lack of
synchronization between your threads when accessing the file.

The problem is that I can only suppose what is really going on,
because the code you posted is incomplete and has some logical
inconsistencies (I assume you typed the code directly in the message
instead of copy-pasting).

Anyway, it becomes somewhat evident that you want to create a
multithreaded log system. If this is the case, then you must consider
that logging must respect a first in, first out sequence. Also,the
file system may complain of multiple writing attempts from different
threads to the same file. Both issues require some sort of thread
synchronization.

A possible approach would be to have a logging class that each thread
shared (instead of creating a different class for each thread).
Something in the lines of:

<code>
Public Class Log
Private mLogFile As String
Public Sub New(ByVal Path As String)
mLogFile = Path
LogMessage("")
End Sub

Public Sub LogMessage(ByVal Text As String)
SyncLock Me
System.IO.File.AppendAllText(mLogFile, Text)
End SyncLock
End Sub

End Class
</code>

You can test the class with the following:

<aircode>
'At module or class level
Const LOG_PATH = "..."
Private mLog As New Log(LOG_PATH)

'...
Sub SaveMessage(Msg As Object)
Static Random as New System.Random
Dim T As Integer
SyncLock Random
T = Random.Next(50, 300)
End SyncLock
System.Threading.Thread.Sleep(T)
mLog.LogMessage(Msg.ToString & ControlChars.CrLf)
End Sub

Sub TestLog
For I As Integer = 1 to 50
Dim Msg As String= "Message #" & I.ToString
Dim T As New System.Threading.Thread(AddressOf SaveMessage)
T.Start(Msg)
Next
End Sub
</aircode>

HTH.

Regards,

Branco.
>I create a thread where I pass thru a message. When I click very fast
many
times (like 50 times) to create 50 threads, the message did not get pass
thru ProcessMessage.
For example: strBuffer = "#TRADE, D1410-123456, BUY, 1, ESM7, DAY, LIMIT,
1490.00, , , 0, 0, 0, 0, 0, links |52994/25/2007 10:47:17 AM !A", when I
trigger to create many threads (like 50), this message did not get to sub
ProcessMessage in clsEachMessage.
I have added Thread.Sleep(0) after the Start method of the thread.
What is the best way to do the thread so that every message get pass thru
to
it ?
Is there a maximum number of thread that I can create ?
Shall I use thread pool instead of creating a new thread each time ?
Thank you very much.

Dim EachMessageThread As Threading.Thread = Nothing
Dim clsEachMessage As New EachMessage

clsEachMessage.sMessage = strBuffer
EachMessageThread = New Threading.Thread(AddressOf
clsEachMessage.ProcessMessage)
threadCount += 1
EachMessageThread.Name = "Thread " & threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)

Public Class EachMessage
Public sMessage As String

Public Sub ProcessMessage()
Dim swError As StreamWriter

swLog = New StreamWriter(sPath & "\log\Input.txt", True)
swLog.Write(sMessage & vbCrLf)
swLog.Close()
swLog = Nothing


Apr 26 '07 #3
How about if I do not use class, but using the threadpool like the
following:
Public Class ClientSession
Dim strBuffer As String

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ProcessMessage),
strBuffer)

Public Sub ProcessMessage(ByVal Data As Object)
sMessage = CType(Data, String)
:
end Sub

end Class

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@n15g2000prd.googlegro ups.com...
fniles wrote:
<backposted/>

I don't think the problem has to do with you creating threads in a
rapid (!) sequence. It seems to me it has more to do with the lack of
synchronization between your threads when accessing the file.

The problem is that I can only suppose what is really going on,
because the code you posted is incomplete and has some logical
inconsistencies (I assume you typed the code directly in the message
instead of copy-pasting).

Anyway, it becomes somewhat evident that you want to create a
multithreaded log system. If this is the case, then you must consider
that logging must respect a first in, first out sequence. Also,the
file system may complain of multiple writing attempts from different
threads to the same file. Both issues require some sort of thread
synchronization.

A possible approach would be to have a logging class that each thread
shared (instead of creating a different class for each thread).
Something in the lines of:

<code>
Public Class Log
Private mLogFile As String
Public Sub New(ByVal Path As String)
mLogFile = Path
LogMessage("")
End Sub

Public Sub LogMessage(ByVal Text As String)
SyncLock Me
System.IO.File.AppendAllText(mLogFile, Text)
End SyncLock
End Sub

End Class
</code>

You can test the class with the following:

<aircode>
'At module or class level
Const LOG_PATH = "..."
Private mLog As New Log(LOG_PATH)

'...
Sub SaveMessage(Msg As Object)
Static Random as New System.Random
Dim T As Integer
SyncLock Random
T = Random.Next(50, 300)
End SyncLock
System.Threading.Thread.Sleep(T)
mLog.LogMessage(Msg.ToString & ControlChars.CrLf)
End Sub

Sub TestLog
For I As Integer = 1 to 50
Dim Msg As String= "Message #" & I.ToString
Dim T As New System.Threading.Thread(AddressOf SaveMessage)
T.Start(Msg)
Next
End Sub
</aircode>

HTH.

Regards,

Branco.
>I create a thread where I pass thru a message. When I click very fast
many
times (like 50 times) to create 50 threads, the message did not get pass
thru ProcessMessage.
For example: strBuffer = "#TRADE, D1410-123456, BUY, 1, ESM7, DAY, LIMIT,
1490.00, , , 0, 0, 0, 0, 0, links |52994/25/2007 10:47:17 AM !A", when I
trigger to create many threads (like 50), this message did not get to sub
ProcessMessage in clsEachMessage.
I have added Thread.Sleep(0) after the Start method of the thread.
What is the best way to do the thread so that every message get pass thru
to
it ?
Is there a maximum number of thread that I can create ?
Shall I use thread pool instead of creating a new thread each time ?
Thank you very much.

Dim EachMessageThread As Threading.Thread = Nothing
Dim clsEachMessage As New EachMessage

clsEachMessage.sMessage = strBuffer
EachMessageThread = New Threading.Thread(AddressOf
clsEachMessage.ProcessMessage)
threadCount += 1
EachMessageThread.Name = "Thread " & threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)

Public Class EachMessage
Public sMessage As String

Public Sub ProcessMessage()
Dim swError As StreamWriter

swLog = New StreamWriter(sPath & "\log\Input.txt", True)
swLog.Write(sMessage & vbCrLf)
swLog.Close()
swLog = Nothing


Apr 26 '07 #4
1 more question.
If I create only 1 class that each thread shared, and the class has many
module level variables (in the example below Account and Quantity). That
module level variable is modified by each thread, since the class is shared
by each thread, will the value of these module level variables be correctly
assigned to the correct thread ? Thank you.

For ex:
Dim clsEachMessage As New EachMessage

Do While Not Terminated
:
clsEachMessage.sMessage = strBuffer
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
clsEachMessage.ProcessMessage))
Loop

Public Class EachMessage
Public sMessage As String
Private Account As String -module level variable
Private Quantity As integer-module level variable

Public Sub ProcessMessage(ByVal stateInfo As Object)
Account = ... -this module level variable is modified in each
thread, since the class is shared by each thread, will this value be
correctly assigned to the correct thread ?
Quantity = ... -this module level variable is modified in each
thread, since the class is shared by each thread, will this value be
correctly assigned to the correct thread ?
end sub

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@n15g2000prd.googlegro ups.com...
fniles wrote:
<backposted/>

I don't think the problem has to do with you creating threads in a
rapid (!) sequence. It seems to me it has more to do with the lack of
synchronization between your threads when accessing the file.

The problem is that I can only suppose what is really going on,
because the code you posted is incomplete and has some logical
inconsistencies (I assume you typed the code directly in the message
instead of copy-pasting).

Anyway, it becomes somewhat evident that you want to create a
multithreaded log system. If this is the case, then you must consider
that logging must respect a first in, first out sequence. Also,the
file system may complain of multiple writing attempts from different
threads to the same file. Both issues require some sort of thread
synchronization.

A possible approach would be to have a logging class that each thread
shared (instead of creating a different class for each thread).
Something in the lines of:

<code>
Public Class Log
Private mLogFile As String
Public Sub New(ByVal Path As String)
mLogFile = Path
LogMessage("")
End Sub

Public Sub LogMessage(ByVal Text As String)
SyncLock Me
System.IO.File.AppendAllText(mLogFile, Text)
End SyncLock
End Sub

End Class
</code>

You can test the class with the following:

<aircode>
'At module or class level
Const LOG_PATH = "..."
Private mLog As New Log(LOG_PATH)

'...
Sub SaveMessage(Msg As Object)
Static Random as New System.Random
Dim T As Integer
SyncLock Random
T = Random.Next(50, 300)
End SyncLock
System.Threading.Thread.Sleep(T)
mLog.LogMessage(Msg.ToString & ControlChars.CrLf)
End Sub

Sub TestLog
For I As Integer = 1 to 50
Dim Msg As String= "Message #" & I.ToString
Dim T As New System.Threading.Thread(AddressOf SaveMessage)
T.Start(Msg)
Next
End Sub
</aircode>

HTH.

Regards,

Branco.
>I create a thread where I pass thru a message. When I click very fast
many
times (like 50 times) to create 50 threads, the message did not get pass
thru ProcessMessage.
For example: strBuffer = "#TRADE, D1410-123456, BUY, 1, ESM7, DAY, LIMIT,
1490.00, , , 0, 0, 0, 0, 0, links |52994/25/2007 10:47:17 AM !A", when I
trigger to create many threads (like 50), this message did not get to sub
ProcessMessage in clsEachMessage.
I have added Thread.Sleep(0) after the Start method of the thread.
What is the best way to do the thread so that every message get pass thru
to
it ?
Is there a maximum number of thread that I can create ?
Shall I use thread pool instead of creating a new thread each time ?
Thank you very much.

Dim EachMessageThread As Threading.Thread = Nothing
Dim clsEachMessage As New EachMessage

clsEachMessage.sMessage = strBuffer
EachMessageThread = New Threading.Thread(AddressOf
clsEachMessage.ProcessMessage)
threadCount += 1
EachMessageThread.Name = "Thread " & threadCount.ToString()
EachMessageThread.Start()
Thread.Sleep(0)

Public Class EachMessage
Public sMessage As String

Public Sub ProcessMessage()
Dim swError As StreamWriter

swLog = New StreamWriter(sPath & "\log\Input.txt", True)
swLog.Write(sMessage & vbCrLf)
swLog.Close()
swLog = Nothing


Apr 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
8
by: MuZZy | last post by:
Hi, Could someone pls help me here: If i use async sockets in the separate thread like this: void ThreadFunction() { ..... 1. MySocket.BeginAccept(AsyncCallBack(OnConnectRequest),...
2
by: Sgt. Sausage | last post by:
New to multi-threading (less than 24 hours at it <grin>) Anyway, it's all making sense, and working fairly well thus far, but I'm having a minor issue I'm not sure how to get around. I've got...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
5
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a...
0
by: puff | last post by:
When interfacing to a COM object, is it possible to pump messages in a thread? I'm working on an application that automates IE and needs to monitor IE events (yes I know about Pamie). I'm able...
22
by: Morpheus | last post by:
Hi, I have been coding in Windows for many years so have a mindset to it, so forgive any stupid questions. Is it possible to create a multithread application in C++ that is portable...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.