PDA

Просмотр полной версии : Xml и зависание главной формы


Student :)
03.03.2008, 14:16
Привет всем знатокам!!
вопрос есть некий XML документ на сервере Я ЕГО загружаю и парсю но при этом процесе
главная форма зависает ! как мне ето обойти ? без потоков делаю так.

procedure TForm1.Button1Click(Sender: TObject);
var
//CityID: string;
CoDoc: CoDOMDocument;
XMLD: DOMDocument;
r: IXMLDOMElement;
FNode: IXMLDOMNode;
i,y: integer;
begin
XMLD:=CoDoc.Create ;
XMLD.async:=false;
URL:=ComboBox1.Text;
XMLD.load(URL);
////////////////////////// здесь чтото не так
while not XMLD.load(URL) do
Application.ProcessMessages;
////////////////////////////
memo1.Clear;
listbox1.Clear;
r:=XMLD.documentElement;
FNode:= r.SelectSingleNode('//rss');
if FNode.attributes.getNamedItem('version').text<>'2.0'
then
begin
Memo1.Lines.Add('error no 2.0');
Exit;
end;

madnet
03.03.2008, 14:55
Оттрассируй эти 2 строчки
while not XMLD.load(URL) do
Application.ProcessMessages;

Мне кажется, что XMLD.load не вернет управление до тех пор пока не загрузится, а посему и ProcessMessages ты не вызовешь.

Здесь варианта 2
1) Вынеси этот кусок кода в отдельный поток

2) Посмотри по документации может у твоего класса есть метод обработки времени загрузки.

Student :)
03.03.2008, 14:58
спасибо

madnet
03.03.2008, 15:01
Можешь еще воспользоваться режимами загрузки
XMLD.Async := True;

Поидее в асинхронном режиме ты сразу получишь управление, но в таком случае надо добавить обработчик на завершение загрузки.

Student :)
03.03.2008, 15:15
может так
XMLD.load(URL);
for I:=0 to 20 do
begin
Application.ProcessMessages;
end;

z01b
03.03.2008, 16:31
Пихай код в процедуру и запусти отдельный поток.
ИМХО лучший вариант.

madnet
03.03.2008, 16:35
может так
XMLD.load(URL);
for I:=0 to 20 do
begin
Application.ProcessMessages;
end;

Ты не понял, в синхронном режиме, что бы ты не писал, пока не выполнится XMLD.load(URL); дальше не пойдет.

Student :)
03.03.2008, 17:54
а как потоки создать? покажите примерчик

z01b
03.03.2008, 18:11
а как потоки создать? покажите примерчик
The CreateRemoteThread function creates a thread that runs in the address space of another process.

HANDLE CreateRemoteThread(

HANDLE hProcess, // handle to process to create thread in
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
DWORD dwStackSize, // initial thread stack size, in bytes
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // pointer to argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to returned thread identifier
);


Parameters

hProcess

Identifies the process in which the thread is to be created.
Windows NT: The handle must have PROCESS_CREATE_THREAD access. For more information, see Process Objects.

lpThreadAttributes

Pointer to a SECURITY_ATTRIBUTES structure that specifies a security descriptor for the new thread and determines whether child processes can inherit the returned handle. If lpThreadAttributes is NULL, the thread gets a default security descriptor and the handle cannot be inherited.

dwStackSize

Specifies the size, in bytes, of the stack for the new thread. If this value is zero, the stack size defaults to the same size as that of the primary thread of the process. The stack is allocated automatically in the memory space of the process and is freed when the thread terminates. Note that the stack size grows as necessary.

lpStartAddress

Points to the starting address of the new thread. This is typically the address of a function declared with the WINAPI calling convention that never returns and that accepts a single 32-bit pointer as an argument.

lpParameter

Points to a single 32-bit value passed to the thread.

dwCreationFlags

Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation.

lpThreadId

Points to a 32-bit variable that receives the thread identifier.