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

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by Student :), 3 Mar 2008.

  1. Student :)

    Student :) Elder - Старейшина

    Joined:
    9 Feb 2007
    Messages:
    278
    Likes Received:
    18
    Reputations:
    -15
    Привет всем знатокам!!
    вопрос есть некий XML документ на сервере Я ЕГО загружаю и парсю но при этом процесе
    главная форма зависает ! как мне ето обойти ? без потоков делаю так.

    PHP:
    procedure TForm1.Button1Click(SenderTObject);
    var
     
    //CityID: string;
     
    CoDocCoDOMDocument;
     
    XMLDDOMDocument;
     
    rIXMLDOMElement;
     
    FNodeIXMLDOMNode;
     
    i,yinteger;
    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;
     
  2. madnet

    madnet Умиротворенный

    Joined:
    9 Dec 2004
    Messages:
    868
    Likes Received:
    343
    Reputations:
    423
    Оттрассируй эти 2 строчки
    while not XMLD.load(URL) do
    Application.ProcessMessages;

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

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

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

    Student :) Elder - Старейшина

    Joined:
    9 Feb 2007
    Messages:
    278
    Likes Received:
    18
    Reputations:
    -15
    спасибо
     
  4. madnet

    madnet Умиротворенный

    Joined:
    9 Dec 2004
    Messages:
    868
    Likes Received:
    343
    Reputations:
    423
    Можешь еще воспользоваться режимами загрузки
    XMLD.Async := True;

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

    Student :) Elder - Старейшина

    Joined:
    9 Feb 2007
    Messages:
    278
    Likes Received:
    18
    Reputations:
    -15
    может так
    XMLD.load(URL);
    for I:=0 to 20 do
    begin
    Application.ProcessMessages;
    end;
     
  6. z01b

    z01b Муджахид

    Joined:
    5 Jan 2007
    Messages:
    494
    Likes Received:
    382
    Reputations:
    22
    Пихай код в процедуру и запусти отдельный поток.
    ИМХО лучший вариант.
     
  7. madnet

    madnet Умиротворенный

    Joined:
    9 Dec 2004
    Messages:
    868
    Likes Received:
    343
    Reputations:
    423
    Ты не понял, в синхронном режиме, что бы ты не писал, пока не выполнится XMLD.load(URL); дальше не пойдет.
     
    _________________________
  8. Student :)

    Student :) Elder - Старейшина

    Joined:
    9 Feb 2007
    Messages:
    278
    Likes Received:
    18
    Reputations:
    -15
    а как потоки создать? покажите примерчик
     
  9. z01b

    z01b Муджахид

    Joined:
    5 Jan 2007
    Messages:
    494
    Likes Received:
    382
    Reputations:
    22
    Code:
    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.