tags:

views:

92

answers:

3

Hi all

how can i get actions ,i did when my thread is paused after resumed (Sorry for my bad english ) ok i will explain with code

function mythreadf(p:Pointer):DWORD stdcall;

var   i:Integer;

begin  

  for i:=0 to 1000000 do    begin

     if myevent.WaitFor(INFINITE)=wrsignaled
then

     begin

       if Form1.RadioButton1.Checked then ShowMessage('Checked');

       Form1.Label1.Caption:=IntToStr(i);

     end; 
   end;

end;

i am pausing and resuming my thread using resetevent and setevent after i paused my thread by clicking resetevent button and then i checked radiobotton1 after that when resume my thread by using setevent again .dont send error occuring and applications closing :(

can any one help me in this issue

regards Edit 1: Error image http://i49.tinypic.com/11r7nkn.jpg

+5  A: 

Accessing VCL UI controls directly in a worker thread is NOT thread-safe (even ShowMessage() is not thread-safe. Use the Win32 API MessageBox() directly instead). All kinds of bad things can happen, including crashes. You must delegate your UI access to the main thread instead. The TThread class has a Synchronize() method for that purpose. Or you can use any other inter-thread synchronization of your choosing, such as by using SendMessage() to send custom messages to a hidden window created in the main thread via AllocateHWnd() or CreateWindow/Ex().

Remy Lebeau - TeamB
A: 

Remy: Can you provide a small example to explain "sending custom messages to a hidden window with AllocateHWnd or CreateWindow/Ex. Thanks in advance! Torsten R.

TorstenR
Welcome to Stack Overflow. Please use the "ask question" button at the top of the page to ask questions. This space is reserved for answers, but what you've written obviously isn't an answer. When you get enough reputation points (50), you'll be allowed to post *comments* to other people's answers, which would be a better way to get Remy's attention. But you don't necessarily want an answer from Remy; you want an answer from anyone willing and able to provide one, which is yet another reason to use the "ask question" button to post a new standalone question — a bigger audience. Good luck.
Rob Kennedy
A: 

Your thread, as Remy says, should not be accessing the checkbox. Also it's bad programming style. Your background thread has a purpose? That purpose will help you find a name. If you create a class that inherits from TThread, you will get farther, faster.

interface
 type
   TMyElephantCountingThread = class(TThread)
        protected
            FResultStr:String; // holds something for later display on the user interface
            FOptionChecked:Boolean; // set from main thread, to tell background thread whether or not a checkbox option is checked.
  ....
 end;
....
implementation
 ....
function TMyElephantCountingThread.ElephantCounterResults;
begin  
  // all data fields in here is local to this thread 
   if FOptionChecked then 
          FResultStr := IntToStr(FIntegerValue);

end;

As you can see my code above uses only fields that belong to my thread object.

If FOptionChecked needs to be set equal to the value of Checkbox.checked, that must be done in the main thread.

You can not copy and paste write code from your foreground thread (which can access your VCL objects) into your background thread code (which can not safely access those objects), and not expect problems.

Warren P