Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 30th, 2011, 07:26 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default Problem with multithreading..

Hi All,
I am creating windows application in vb.net and I have problem related to multiple threading. Let me describe you the scenario and what are the problems I am facing.
On form Load I am creating the thread the calling the relevant methods.

like below:
Code:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Dim objThread As System.Threading.Thread    
 Dim i as integer
 
   For i=0 to 4
                
                objThread = New Thread(AddressOf _startExtractThread)
                objThread.Start()
                
   Next

 End Sub
Here in this method I am creating one sTempName variable and then I pass that variable as the Name of file and temporary tables to other methods.

Code:
 
 Public Sub _startExtractThread()
  dim sTempName as string = "Temp_" & Format(Date.Now, "yyMMdd_HHmmss")
    ''Now this variable I am passing to other methods for different task.
        ''Below method will create temporary table
   
         getDMTableSchema(objlistCollection, sTempName, sLogFile)
         
        
   ''I am inserting records in this table so in that also i will pass the same variable name
        InsertRecords(sTempName,sUserID, sUserPermID, sGroupID, sLogFile) 
 'then I am writing those records in to the CSV file, so for creating csv file again I am passing this sTempName variable
 writeRecords(sTempName,sExportFolder)
  
 'then I am deleting the temporary created table
        sLogSQL = "drop table " & sTempName
        iSuccess = dbExecuteSQL(sLogSQL, sLogFile) 
 
 End Sub
In the above scenario what I want is if 4 thread are getting created then it should create 4 temporary table and 4 CSV files with different names. after that all the 4 temporary table should be dropped.

But at the movement it is creating only 1 table and 1 csv file and repeating the records in the csv which it retireve from the table.

Please shed some light and give some idea and hint.

Many Thanks
-Nelly
Reply With Quote
  #2 (permalink)  
Old May 2nd, 2011, 07:59 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hello Experts,

Could someone please shed some light on this problem who is expert and have good knowledge in Threading.

Please give some hint or work around on this problem.

Thanks
Nelly
Reply With Quote
  #3 (permalink)  
Old May 2nd, 2011, 09:51 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

This could be weird, but did you try to not use a for next to start the four threads???

What I can see weird with this code, if that you need a reference somewhere to the thread you are starting, and you are droping the reference every time you start a new thread.. So you are probably using only one thread...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
Reply With Quote
  #4 (permalink)  
Old May 2nd, 2011, 10:32 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

i Gonzalo,
Thank you very much for the reply.
Quote:
Originally Posted by gbianchi View Post
This could be weird, but did you try to not use a for next to start the four threads???
...
Could you please explain me what you meant by 'Not to use a for next'.

Please let me explain my question in more detail:

Here I am creating four thread as example but in real scenario I will be getting count form XML and based on the count

I will create the number of thread. So without for next how will I create the number of thread.

If i create only one thread then everthing is working fine.

As I m new to multi thread could you please give some exmple code, how to create more than one thread which will call the same methods and perfom different task.

Many thanks
_Nelly
Reply With Quote
  #5 (permalink)  
Old May 2nd, 2011, 10:39 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

You need something to control the states of the threads, so Just use a collection instead of a single variable to hold the thread..

What I meant with not use the for thread, if that you try creating the 4 threads (just for try it) one at a time without using a for...

something like:

Dim objThread1 As System.Threading.Thread
Dim objThread2 As System.Threading.Thread
Dim objThread3 As System.Threading.Thread
Dim objThread4 As System.Threading.Thread
objThread1 = New Thread(AddressOf _startExtractThread)
objThread.Start()
objThread2 = New Thread(AddressOf _startExtractThread)
objThread.Start()
objThread3 = New Thread(AddressOf _startExtractThread)
objThread.Start()
objThread4 = New Thread(AddressOf _startExtractThread)
objThread.Start()

Another problem could be you choise of the temporary name.. maybe apply another method (like passing the number of thread started) or getting a temporary name from windows would be best.. there could be the case that 2 threads starts at the same second (you know that a second is a hell lot of time for a computer...)
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
Reply With Quote
  #6 (permalink)  
Old May 2nd, 2011, 12:48 PM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

I have found the solution for this problem using 'lock' like below and it is working as expected.

if I put the code between 'SyncLock Me' and 'End SyncLock'

Code:
Public Sub _startExtractThread()
 
SyncLock Me

dim sTempName as string = "Temp_" & Format(Date.Now, "yyMMdd_HHmmss")

    ''Now this variable I am passing to other methods for different task.
    ''Below method will create temporary table
   
         getDMTableSchema(objlistCollection, sTempName, sLogFile)
        
   ''I am inserting records in this table so in that also i will pass the same variable name

        InsertRecords(sTempName,sUserID, sUserPermID, sGroupID, sLogFile) 

   'then I am writing those records in to the CSV file, so for creating csv file again I am  
   passing this sTempName variable
 
   writeRecords(sTempName,sExportFolder)
  
   'then I am deleting the temporary created table
   sLogSQL = "drop table " & sTempName
   iSuccess = dbExecuteSQL(sLogSQL, sLogFile) 
 
 End Sub
End SyncLock
But the problem is with this approach is thread are not doing the task simultaneously.
One thread lock the whole code, once that thread release the lock then other thread start the processing. And If there is a lengthy task which will take one hour then second thread will start after one hour.

how to resolve this issue I want all the thread execute simultaneosly.

Any help in this matter, much appriciated.

Thanks
-Nelly
Reply With Quote
  #7 (permalink)  
Old May 2nd, 2011, 02:57 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Did you read anything I Write?? what happens when you did that??
if threads are working one behind the other, there is no point in using threads...
Another thing to ask... Did you really needs threads?? is your process sending or waiting data?? is all the process are just reading and writing from files, I don't know if threads are the beast aproach...
Are they calculating something? do you have multiple procesors?
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithreading Help jenngarcia21 General .NET 0 March 9th, 2007 02:14 PM
multithreading in c# furqanms C# 0 November 21st, 2006 10:43 AM
regarding multithreading Mystic C# 1 July 22nd, 2006 12:44 AM
Multithreading and Synclock AgentSmith ASP.NET 1.0 and 1.1 Professional 0 October 19th, 2005 08:26 AM
How to use Multithreading in VB6.0 ramk_1978 Beginning VB 6 3 January 24th, 2005 09:53 AM



All times are GMT -4. The time now is 07:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.