Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing Messages in Java Win32 applications

Status
Not open for further replies.

DmitriIk

Programmer
Aug 27, 2001
2
US
Dear Friends,

I have developed a multithreaded application for Windows
using VisualCafe.

There are two threads in this application.
The main thread is associated with the main frame
and it is responsible for user interactions.

The other thread is used to communicate with hardware;
it executes commands that require some time to be completed.

The main thread starts the second thread and
waits till the second thread is completed :

{
Thread mainThread = Thread.currentThread();
secondThread cfThread = new SecondThread();
cfThread.start();
while (! SecondThreadStatus.Finished)
{
try
{
// ProcessMessages(); !!!!! ?????????
mainThread.sleep(10);
}
catch (InterruptedException e) {
}
}
}

Class SecondThreadStatus contains static variable Finished
and is used to check the status of the second thread.

The problem is that I do not know how to process Windows
messages. As a result, this application does not respond
to user commands (pressing buttons etc) after the second
thread is started. I need the code for procedure
ProcessMessages.

I have developed similar applications using Delphi that work well. The related procedure looks like this :

procedure WaitForCompletion;
begin
while not Finished do begin
Application.ProcessMessages;
Sleep(100);
end;
end;

/* -------------------------------------------------- */
The code for TApplication.ProcessMessages (unit Forms)

function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
if not IsHintMsg(Msg) and not Handled and
not IsMDIMsg(Msg) and
not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end
else
FTerminate := True;
end;
end;

procedure TApplication.ProcessMessages;
var
Msg: TMsg;
begin
while ProcessMessage(Msg) do {loop};
end;

Thank you in advance for your advice,

Dmitri











 
There is actually nothing wrong with your threads. Your first(main) thread is still running but because of the while statement, it will put your first thread to sleep when the second thread is not done with its job thus all buttons will not work.

What you can do is to put/call the ProcessMessages() method within the second thread itself. This way, there will be no complications :)

Regards,
Leon
 
Dear Leon,

My problem is that I could not find a procedure
like ProcessMessages in Java libraries and
I do not know how to implement it.

Regards,
Dmitri
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top