Konuyu Oyla:
  • Derecelendirme: 0/5 - 0 oy
  • 1
  • 2
  • 3
  • 4
  • 5
Bu thread kullanımı doğrumu ?
#1
Question 
Merhaba ustalarım.Serial port üzerinden cihazlarlar veri alışverişinde bulunurken bazen verilerin geç gelmesi yada bağlantı kopması gibi durumlar olduğu için, main form donmaması amacıyla thread kullanmaya karar verdim. Ama bazen read başarılı olduğu halde veri 00 olarak geliyor. Thread kullanmasam bir sorun olmuyor ama kullanırsam dediğim gibi farklı hatalarla karşılaşıyorum.

Bu thread kullanımı doğrumu ? Yardımcı olursanız çok sevinirim.


var
 lOverlapped: OVERLAPPED;
 lLastError, BytesTrans: Cardinal;
 lEvent: TEvent;
 Task: ITask;
 TmpBuf: TMyByteArray;
 CanWrite: Boolean;
begin

 if UsbHandle = INVALID_HANDLE_VALUE then
exit(-1);


 SetLength(TmpBuf, Count);
 ZeroMemory(TmpBuf, Length(TmpBuf));
 lEvent := TEvent.Create(nil, FALSE, FALSE, '');
 FillChar(lOverlapped, SizeOf(lOverlapped), 0);
 lOverlapped.hEvent := lEvent.Handle;
 CanWrite := ReadFile(UsbHandle, TmpBuf[0], Count, BytesTrans, @lOverlapped);

 try
   if not CanWrite then
   begin

     lLastError := GetLastError;

     if (lLastError <> ERROR_IO_PENDING) then
       exit;
     Task := TTASK.Create(
       procedure
       begin

         case lEvent.WaitFor(12000) of
           wrSignaled:
             if not GetOverlappedResult(UsbHandle, lOverlapped, BytesTrans,
               FALSE) then
               exit;

           wrError:
             begin
               lLastError := lEvent.LastError;

               if CancelIo(UsbHandle) then
                 lEvent.WaitFor(12000);
               exit;
             end;
         end;

         CanWrite := True;

       end);
     Task.Start();

     while Task.STATUS <> TTASKSTATUS.COMPLETED do
     begin
       IF(Task.STATUS = TTASKSTATUS.EXCEPTION) THEN
       exit(-1);
       Application.ProcessMessages;

     end;

     if not CanWrite then
     exit(-1);

   end;

   Result := BytesTrans;
   SetLength(TmpBuf, BytesTrans);
   MoveMemory(@Buffer, @TmpBuf[0], Length(TmpBuf));
   Application.ProcessMessages;
 finally
   FreeAndNil(lEvent);
 end;
Cevapla


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  nduWlanAPI Kullanımı hakkında nurah 3 803 09-09-2025, Saat: 09:33
Son Yorum: RAD Coder
  FMX de Tchart kullanımı mkucuktepe@msn.com 2 710 25-07-2025, Saat: 14:42
Son Yorum: mkucuktepe@msn.com
  Thread içinde Web Browser Çalışırmı adelphiforumz 0 431 09-01-2025, Saat: 13:22
Son Yorum: adelphiforumz
  HTTPRIO ile TLS 1.2 Kullanımı Nasıl Yapılabilir? hi_selamlar 6 3.077 01-08-2024, Saat: 14:33
Son Yorum: unicasoft
  FastReport'ta IFF Kullanımı Nasıl Yapabilirim ? Anarchosa 3 1.044 22-07-2024, Saat: 12:05
Son Yorum: QuAdR



Konuyu Okuyanlar: 1 Ziyaretçi