can the mainform read a thread's public variable?

Please discuss general Delphi programming topics here.

can the mainform read a thread's public variable?

Postby actalk » May 30th, 2008, 8:09 am

Hello!

There are a few thread..at most 10 threads.
They have each own buffer array.
They post their own buffer's current index to mainform
and then mainform access (read) the thread's buffer data.

There is not any Synchronization and not any criticalsection.
is there any problem ? or any suggestion?
Thanks in advance.

code like this as summary:

type
TplThread = Class(TThread)
..
..public
....Buffer:array[0..100, 0..BufSize] of char;
..

on thread execute
..ReadFile(Pipe1, Buffer[currentBufferIndex, 0], BufSize, BytesRead, nil);
..
..postmessage(threadid, currentBufferIndex) to main form
..

on MainForm
..memo.Lines.Add(thread1.Buffer[currentindex]);
or
..write thread1.Buffer[currentindex] to a logfile...

-0-
actalk
Junior Member
Junior Member
 
Posts: 29
Joined: October 5th, 2007, 3:56 pm

Postby Kambiz » May 30th, 2008, 11:23 am

The code has synchronization problem. You must access the global thread variables within a critical section.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2424
Joined: March 7th, 2003, 7:10 pm

Postby actalk » May 31st, 2008, 4:50 pm

form1= TForm
..public
....cs: TCriticalSection;
.
constructor form1.Create
..cs := TCriticalSection.Create;
.
.
proc form1.startThread
..cretaeThread(cs)
..
Threads will use form's public variable cs (TCriticalSection) to lock code parts...
is there any problem?
actalk
Junior Member
Junior Member
 
Posts: 29
Joined: October 5th, 2007, 3:56 pm

Postby Kambiz » June 1st, 2008, 8:37 am

It's correct. The CS veriable should be shared with the form and the threads.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2424
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest