Top.Mail.Ru Yandeks.Metrika
Forum: "Main";
Current archive: 2002.04.01;
Download: [xml.tar.bz2];


TThread: How to create your own signaling object? Find similar branches

lipskiy   (2002-03-20 21:46) [0]

People! I can not figure out how it is done.
I have a stream that updates the data through the Internet.
I create it, I make some initialization, then I launch DialUp. And I need to wait for the connection to be established. The DialUp component has an OnConnect event — when this event occurs, I need to continue executing the stream.
It is clear that you need to use WaitForSingleObject, but he needs a Handle, and where should I get it? That is, how to get the Handle events OnConnect, or something ... Confused at all.
If you can answer - then in more detail, plz, the first time with a stream I mess around.

lipskiy   (2002-03-20 21:56) [1]

Where would the flow of something for teapots and read in Russian?

vuk   (2002-03-20 21:56) [2]

An event synchronization object is created (see CreateEvent) and its handle is passed to both the thread and the one who will wake it up. At the right time, just calling SetEvent.

Sometimes for convenience, you can use the TEvent wrapper class.

vuk   (2002-03-20 22:00) [3]

You can read from Richter. Pretty good everything is painted.

lipskiy   (2002-03-20 22:03) [4]

2 vuk
A reference to Mona "read"?

Набережных С.   (2002-03-20 22:03) [5]

WaitFor is not needed here ... Just declare a global flag that you install in OnConnect, and write something like in the stream
Flag: = false; DialUp.Open; while not Flag do;

vuk   (2002-03-20 22:05) [6]

Honestly, I do not have a link. : o (Book only.

Suntechnic   (2002-03-20 22:06) [7]

In English, I can throw a whole book. In Russian, you can hardly find the whole Internet ... except for some chapters ...

Suntechnic   (2002-03-20 22:09) [8]

> Naberezhnye S. (20.03.02 22: 03)
Well no. The checkbox here is just inappropriate. While waiting for such a flow will "eat" resources. And we need to transfer it to the waiting state without consuming the system resources. For this purpose, synchronization objects and Wait-f-functions are used ...

Набережных С.   (2002-03-20 22:14) [9]

Probably all the same need to clarify:
While not Flag do if Terminated then .. - release of resources and exit from the stream.

Набережных С.   (2002-03-20 22:19) [10]

> Suntechnic © (20.03.02 22: 09)

No special "devouring" will not, unless you raise the priority. However, after DialUp.Open, you can simply do Syspend and wake it up again in OnConnect.

Набережных С.   (2002-03-20 23:19) [11]

> lipskiy ©

Show the flow code just in case, but now I will advise ...

Suntechnic   (2002-03-20 23:54) [12]

> Naberezhnye S. (20.03.02 22: 19)

Flag: = false; DialUp.Open; while not Flag do;

> There will be no special "devouring", if you do not bully priority.

First, as I understand it, the connection is established asynchronously to the thread that updates the data. This means that DialUp.Open is not called from this thread. Secondly special "devouring" will not be ordinary devouring.

Набережных С.   (2002-03-21 00:01) [13]

> Suntechnic © (20.03.02 23: 54)

> DialUp.Open is not called from this thread


> will be the usual devouring

You can put a sleep if it is important.

Suntechnic   (2002-03-21 00:23) [14]

> Naberezhnye S. (21.03.02 00: 01)
> Well, and?

> You can put a sleep if it is important.

It is impossible. Because Sleep has no idea when the connection will be established. How much do you pour the flow to? On 10 MSK? On 1 min? Maybe for an hour? .... at the time of the call, Sleep stream will just sleep, instead of working. There is nothing to reinvent the wheel where it has long been invented ...

lipskiy   (2002-03-21 01:14) [15]

People! Thank you all, figured out, it all worked.
DialUp.Open is called from a thread for me, since the instance of this class itself is created along with the stream.
I agree that the cycle on the check box is not very good, especially since the DialUp component works inside the stream, it needs resources.
Made with WaitForSingleObject and CreateEvent, SetEvent, as advised vuk, for which many thanks to him, everything works as it should. Very comfortable indeed.

I have another question.
Here I am doing a loop like this in the stream:
while (WaitForSingleObject (EventConnectSuccessfull, 1000) <> WAIT_OBJECT_0) and (TimeOut <> 0) do ...

Here, EventConnectSuccessfull is my signaling device, and TimeOut is a second counter with decrement in order not to hang at all during dialup.

So, I would need to have not one signaling device, but two different ones. And both check in this loop. One - EventConnectSuccessfull - the connection type was established successfully, and the other - EventConnectFailed - failed.
How in this case correctly to write a condition of a cycle?
If I do this:
while ((WaitForSingleObject (EventConnectSuccessfull, 1000) <> WAIT_OBJECT_0) or (WaitForSingleObject (EventConnectFailed, 1000) <> WAIT_OBJECT_0)) and (TimeOut <> 0) do ...
then, as I understand it, two signaling devices will be expected one by one - every second, and I need both at once. Or is it all right?
And also - how, at the exit from the cycle, to know which alarm went off?

Suntechnic   (2002-03-21 02:19) [16]

In my opinion, you have become too smart with something out. Why do you need him here? In order not to envy forever the second parameter of the function is used. And why do you need while it was in that place that was also not entirely clear. Others need to see all the code to say finally ... If you need to wait for several events, you can use WaitForMultipleObjects, although again I'm not quite sure that she is needed here. It is necessary to see all the logic.

vuk   (2002-03-21 03:20) [17]

About two objects of synchronization. I think that it is absolutely not necessary in this case. After all, the state of a connect / non-connect can always be obtained from the component responsible for the connection.

When waiting in WaitForXXXObject, you can set a timeout for
wait (the second parameter of the function, as noted by Suntechnic), so that no additional counters are simply needed.
if not terminated do case WaitForSingleObject (Handle, Timeout) of WAIT_OBJECT_0: <waited>; WAIT_TIMEOUT: <timeout handling>; else <error handling> end;

Although ... to be honest, sometimes it is better to "wake up" and check the status of the terminated flag, otherwise the stream, in the case of an attempt to stop it, will "hang" until the timeout expires.

But then there are some difficulties with the countdown to this very timeout. This can be solved in three ways:

1. A timer that counts time before a timeout. At the same time, it is necessary to periodically "wake up and look at the clock" as well as the terminated flag.

2. If it happens in NT / W2K / XP, the additional synchronization object is WaitebleTimer.

3. To terminate the stream not by the terminated flag set, but by another Event "y.

The last thing I like the most. It turns out about the following code:
var Handles: array [0..1] of THandle; ... Handles [0]: = evtTerminate; Handles [1]: = evtWake; if not terminated then case WaitForMultipleObjects (2, @Handles, false, Timeout) of WAIT_OBJECT_0: <flow termination>; WAIT_OBJECT_0 + 1: <waited>; WAIT_TIMEOUT: <timeout handling>; else <error handling> end;

Набережных С.   (2002-03-21 03:38) [18]

> Suntechnic © (21.03.02 00: 23)

while not Flag do
if not terminated then Sleep (0)
else ...
And resources - event are also resources.

Suntechnic   (2002-03-21 03:42) [19]

> Naberezhnye S. (21.03.02 03: 38)
> And resources - event resources too.

I'm not going to prove anything to anyone, the more obvious things. But I would recommend that you take a closer look at the subject before advising someone on this issue ...

Набережных С.   (2002-03-21 03:52) [20]

I am quite well and for a long time familiar with the subject. And things are really obvious.

Suntechnic   (2002-03-21 03:58) [21]

> Naberezhnye S. (21.03.02 03: 52)

Only one piece of advice last. Bring your idea to life. Run the test case and see in the Task Manager how much CPU time will be consumed by your program. You look at it and change your view on this question ...

lipskiy   (2002-03-21 11:59) [22]

My own TimeOut variable contains a full timeout for me, for example - 60 seconds, if there is no connection, stop. And by decrement every second inside the while loop, I type the remaining wait time on the main form, and this is where the cycle is needed.

You can get connect / noconnect events from the DialUp component, but I just do not know if it is possible to excite the alarm with a parameter? I now just put the global checkbox - with what result did this one signaling device appear - at connection or at non-connection. But I do not really like it.

Here, the option proposed by vuk © (21.03.02 03: 20) with WaitForMultipleObjects I just did not know, I think it would do.

Although if it is possible to excite one alarm with a parameter, then it will be more convenient (Well, something like SetEvent (True) or SetEvent (False)). So no way?

Набережных С.   (2002-03-21 14:56) [23]

> Suntechnic © (21.03.02 03: 42)

I do not know if it makes sense to continue, but I will answer all the same. There is such a thing - preemptive multitasking. It is fully implemented in NT and almost full in 9X. Any thread will receive exactly as much CPU time as the OS gives it in accordance with its priority. If there are no other active threads at the moment, it will get all the CPU time (minus the kernel expenses). If there is - get their share. In this case, it will not have any noticeable effect either on the system or on the application, and it will not “consume” any resources, especially since it starts up for a few minutes.
Count how many threads are currently active in the server through which we communicate and how this affects the response time for an individual client.
PS It's not enough to look at the Task Manager, you need to understand the meaning of what you see.

> lipskiy ©

If you work under 9x, be very careful with WaitFor ... without timeout. I haven’t had anything to do with 9x for quite some time, but I remember that it’s much easier to get into an impassable dead end with it than with NT.
This, from which the output - only through the "magic three", and even Reset.

Suntechnic   (2002-03-21 15:38) [24]

> Naberezhnye S. (21.03.02 14: 56)
You cannot understand one thing. Flow NOTHING does not do it, except for how the event waits, and the processor time is devoured in full (which the Task Manager will demonstrate to you). No, of course it can be so, but why? If you can make the thread just slept before the event did not claim anything.

In this case, it will not have any noticeable effect either on the system or on the application, and it will not “consume” any resources, especially since it starts up for a few minutes.
Count how many threads are currently active in the server through which we communicate and how this affects the response time for an individual client.

Here is this in our language. Just like a joke, "... I looked at the map, how many Kuban are there ..."

PS My nature is trying to open people's eyes, even if they don’t want it. We must probably finish with this ...

lipskiy   (2002-03-21 15:40) [25]

> I do not know if it makes sense to continue
There is! I always read my branches to the end and not my own, but those that interested me, too, always glad to have a new infe.

I work under Win2k, but the program should live normally under any OS - it is distributed freely through the Internet. By "working" is meant only to "write" or in general, where can it be launched?

And I set the timeout to 1000 (the second parameter is WaitForSingleObject, right?), So I shouldn't be stuck.

KSAT, here's another question along the way, I asked somewhere, but I did not get an answer in my branch. How to make sure the stream is dead? After all, it can not display anything anywhere by code, and in general, does not visually manifest itself, so how to be convinced that the flow is killed?

Aleksandr   (2002-03-21 15:40) [26]

Sorry, colleagues, something I did not catch a bit of the topic here ...
That is, if I have threads in my program with this body:

if FoundWork then
Sleep (2000)
until Terminated; </>
then they will eat resources?

Digitman   (2002-03-21 15:46) [27]

> Aleksandr
What did you think ?!
DoSomething - procedure? Does it require CPU time to execute instructions, its components?

Suntechnic   (2002-03-21 15:48) [28]

> Aleksandr © (21.03.02 15: 40)
In this version, as you have it, 2 will not do anything for a second.

It is necessary to see the condition of the problem, to say whether it makes sense or not. In the case of lipskiy condition described.

> lipskiy © (21.03.02 15: 40)
> How to make sure the stream is dead?
GetExitCodeThread tried it?

Aleksandr   (2002-03-21 15:53) [29]

2 Digitman
I mean, will eat resources in case of idle time, that is, when DoSomething is not performed?

vuk   (2002-03-21 15:54) [30]

to Aleksandr:
Everything depends on the code that we do not see (FoundWork and DoSomething). But you will have to wait at least 2 seconds to complete your stream.

Aleksandr   (2002-03-21 16:03) [31]

Listen, maybe I’ll give you the whole code of the module, and tell me with the looks of the specialists, where are the axes hanging there?

lipskiy   (2002-03-21 16:15) [32]

> GetExitCodeThread tried?
:)) Wow. No, what is it and how is it used?

And I had a little more in mind - not from the program, but in general. For example, I left the program, how do I know that the thread is killed?
And then I heard that he could stay there hanging.

Suntechnic   (2002-03-21 16:38) [33]

> lipskiy © (21.03.02 16: 15)
> :)) Wow. No, what is it and how is it used?
Did you try to open the help? ;)

> For example, I left the program, how do I know that the thread is killed?
And then I heard that he could stay there hanging.

Explain the term "out of the program", then maybe they will tell you whether something will stay or not to hang ....

Набережных С.   (2002-03-21 17:16) [34]

> Suntechnic © (21.03.02 15: 38)

I understand everything perfectly. But the point is that in this case, these costs have no practical value. And in any case, syspend c resume close the topic.
On the other hand, the creation and support of event are the very specific costs of quite specific system resources. But these costs in this case do not matter. And in this case, none of the solutions has any real advantages over the others.

Listen, let's just finish the topic. Tired out. Sorry if something hurt, I did not want.

lipskiy © (21.03.02 15: 40)

> “Work” means only “writing” or in general
> where can it be run?

Where is running.

> And then I heard that he can stay there to hang.

Will not stay. Under NT, all the output streams will be closed, under 9x there is a risk to hang.
In general, to find out the state of any OS object, you need its handle.

lipskiy   (2002-03-21 17:23) [35]

> Did you try to open the help? ;)
I tried, it does not work :)
But seriously - I do not know English, so there is always a problem with the helpers. Knowledge of English at the level of reading Pascal operators allows only in general terms to get an idea of ​​the subject.

> "left the program"
Well, unloaded it, how else can you get out of it? Close did or terminate, or is there a difference how to complete?
I just once watched one broom poke at random with threads too, made my own form of results display for the flow, and when I completed the main program, the streaming form remained hanging and working - some data continued to be updated. Moreover, it was launched from under the IDE, and it normally completed the program run, without errors and questions, and the thread worked ...
He killed only through the task manager.

panov   (2002-03-21 17:26) [36]

> Aleksandr © (21.03.02 15: 40)
No, while Sleep (2000) is running, the program will not occupy resources.
> Naberezhnye S. (21.03.02 17: 16)
On the other hand, the creation and support of event are the very specific costs of quite specific system resources.
Event creation and support - really uses resources.
But the use of these resources is not so important as the use of CPU time. I would even say - this absolutely incomparable things.

> Aleksandr © (21.03.02 16: 03)
Listen, maybe I’ll give you the whole code of the module, and tell me with the looks of the specialists, where are the axes hanging there?

Do not.

If you have a question, then ask it in a separate thread.

Набережных С.   (2002-03-21 18:03) [37]

> lipskiy © (21.03.02 17: 23)

To unload a program means to free the memory it occupies. The executable flow code is part of the program code, and the code cannot be executed if it is not in memory. If the stream continues to run, the program is not unloaded.

lipskiy   (2002-03-21 18:21) [38]

2 Naberezhnye S.
Is logical. That is, if in the list of processes my executor is absent, it means that there is no flow either.
That was what I needed to find out. Thank.

Pages: 1 whole branch

Forum: "Main";
Current archive: 2002.04.01;
Download: [xml.tar.bz2];


Memory: 0.72 MB
Time: 0.037 c
2002-02-19 13:08
Equation solution

2002-03-07 15:45
Execute SQL query

2002-03-21 19:50

2002-03-18 12:04
Excel Sort

2002-03-21 13:06
Transferring a string from Delphi to VC ++

afrikaans albanian Arabic armenian azerbaijani basque belarusian bulgarian catalan Chinese (Simplified) Chinese (Traditional) croatian Czech danish Dutch English estonian filipino finnish French
galician georgian German greek haitian Creole hebrew Hindi hungarian icelandic Indonesian Irish italian Japanese Korean latvian lithuanian macedonian malay maltese norwegian
persian polish portuguese Romanian russian serbian slovak Slovenian Spanish swahili Swedish Thai turkish Ukrainian urdu Vietnamese welsh yiddish bengali bosnian
cebuano Esperanto gujarati hausa hmong igbo javanese kannada Khmer lao latin maori marathi mongolian nepali punjabi somali tamil telugu yoruba
English French German Italian Portuguese Russian Spanish