Vars in TServerClientThread

Posted on 1998-10-22
Last Modified: 2010-04-06

I have encountered the following problem.
I am write a service for windows NT with Delphi 3 C/S. I use some NTService
unit and create a TServerSocket runtime.
I use a Threadblocking methode to spawn new threads for each new connection
to my server. This seems to work fine,
except for the fact that a global variable decleared within th
TServerClientThread wich I want to give the port of the current
connection seems to overwrite the data for this var in all the open threads.
What I mean is that if a second connection is made the variable in the first
connection gets the same value as the variable
in the second (last) connection.

This problem seems to go away when I assign a value to the variable lateron
in the program but that doesn't make verry much
sense to me, because no paticulair thing happen before it that would be able
to solve the problem.

piece of my code:

Unit ClientThread;

  POP3Thread = class(TServerClientThread)

    procedure Execute; override;
var UserPort : integer;
procedure POP3Thread.Execute;
...(loop for receiving commands follows)

Thanx for anny help in advance,

Martijn Tieland

Question by:mtieland
  • 4
  • 2
  • 2
LVL 27

Expert Comment

ID: 1343862
hi mtieland,

your global-var should be in the implementation section of your unit, if so?


Author Comment

ID: 1343863
I have put the vars behind the implementation section...

The strange thing is when I assign a value to these vars lateron
in the thread it some to work propperly...

Author Comment

ID: 1343864
Edited text of question
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 27

Expert Comment

ID: 1343865
hi mtieland,

maybe your var should be initialized

var userport : Integer = 0; {or OtherNumber}

LVL 12

Expert Comment

ID: 1343866
Perhaps the variable being overwritten should be declared with ThreadVar?

Author Comment

ID: 1343867
Thanks for the hint on ThreadVar, it works great,
1 question remains dough, what is a ThreadVar kind of a thing??

Author Comment

ID: 1343868
By the way I am new to this expert-exchange thing but i think rwilson should get the points, because he gave the solution to the problem.

LVL 12

Accepted Solution

rwilson032697 earned 100 total points
ID: 1343869
Thanks mtieland!

Threadvar is used to declare a variable that is in thread local storage. What this means is that every thread will see a different instance of the variable. Generally these kinds of variables are placed into a TThread derived class but sometimes you need to use threadvar...



Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question