Two question about event and socket Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Two question about event and socket

Post by lfjking »

first:
Events can not be received at times.
eg:(Use wxFormBuilder Inherit MainForm, in vs2010)

Code: Select all

// in the  MainForm
.......
virtual void OnThreadSend( wxThreadEvent& event ) { event.Skip(); }
.......

//in Inherit
#include "thread.h"
class MainChildForm{
........
void OnThreadSend( wxThreadEvent& event ) ;
}
.......
MainChildForm::MainChildForm(){
///here bind the event OnThreadSend
.......
}

void MainChildForm::OnThreadSend( wxThreadEvent& event ) 
{
// here  some times can't run.
	OutputDebugString(wxT("I get the thread event"));
}


//in a  thread 
.......
#defind threadsend  2035
class thread: public wxthread
{
	void* Entry(){
		.......
		OutputDebugString(wxT("I send the thread event"));
		wxPostEvent((wxApp*)mApp, wxThreadEvent(wxEVT_THREAD, threadsend));
		........
	}
}

some times , it can send the event but can't recv the event at OnThreadSend.....
I can't find the cause......

----------------------------------------------------------------------------------------------------------------
second:
while I use the wxsocketclinet or wxsocketinputstream readall in thread. how to Interrupt it.
eg:

Code: Select all


wxSocketClient mSock;
mSocket.SetTimeOut(1000); //long time to wait
mSocket.read(lpbuff, len);	//for wait,   and  the question : how can I Interrupt  here.
Last edited by lfjking on Thu Aug 24, 2017 4:30 pm, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Two question about event and socket

Post by ONEEYEMAN »

Hi,
What is your wx version?
What Windows version you are trying this on?
Could you please compile the socket sample and see if you can reproduce it there?

If not - what do you do differently?

What are the conditions on which the event are not received? Is it just stops receiving or it stops then start again, missing one or two?

Thank you.
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: Two question about event and socket

Post by lfjking »

ONEEYEMAN wrote:Hi,
What is your wx version?
What Windows version you are trying this on?
Could you please compile the socket sample and see if you can reproduce it there?

If not - what do you do differently?

What are the conditions on which the event are not received? Is it just stops receiving or it stops then start again, missing one or two?

Thank you.
Event problems happen occasionally, so I don't know where the problem is.
I compile the environment is: win7-64 + wxWidgets 3.10(unicode) + wxFormBuilder + vs2010.
The problem with socket is that I want to gracefully interrupt read congestion. I will look about the socket sample, thanks.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Two question about event and socket

Post by ONEEYEMAN »

Hi,
Its not the compile environment I was asking - rather run environment.
Or they are the same and you are testing on the same machine where you building?

Try to reproduce the issue in the sample.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Two question about event and socket

Post by doublemax »

mApp->PostEvent(new wxThreadEvent(wxEVT_THREAD, threadsend));
I'm surprised this works at all. If you're sending the event to the wxApp instance, i have no idea how it can be received in a child window.

But how is this connected to the socket issue? However, if you use sockets in a worker thread, they must be in blocking mode and you should not use socket events.
Use the source, Luke!
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: Two question about event and socket

Post by lfjking »

doublemax wrote:
mApp->PostEvent(new wxThreadEvent(wxEVT_THREAD, threadsend));
I'm surprised this works at all. If you're sending the event to the wxApp instance, i have no idea how it can be received in a child window.

But how is this connected to the socket issue? However, if you use sockets in a worker thread, they must be in blocking mode and you should not use socket events.

the mapp is the parent :wxWindow* mApp;
sorry. the code is this:
wxPostEvent((wxApp*)mApp, wxThreadEvent(wxEVT_THREAD, threadsend));

you mean the question may be here?

I Know must blocking , also i need it.
but while i want to stop this thread. the socket blocking it.....
now . i try to use the ShutdownOutput() and InterruptWait() to Interrupt the socket.read.
may be ShutdownOutput() better than InterruptWait() .
I wonder if this is graceful operation.
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: Two question about event and socket

Post by lfjking »

ONEEYEMAN wrote:Hi,
Its not the compile environment I was asking - rather run environment.
Or they are the same and you are testing on the same machine where you building?

Try to reproduce the issue in the sample.

Thank you.
while I Try to reproduce the issue in the sample. However, there is no problem of occasional unexpected events.
Maybe I have to comb my code bit by bit... :? :? :? :?.
Wish me luck.....
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Two question about event and socket

Post by doublemax »

but while i want to stop this thread. the socket blocking it
I don't have much experience with wxWidgets sockets, but looking through the API, i'd try WaitForRead() with a small timeout, e.g. 100ms. Instead of a blocking Read() call, you can check if there is any data to read first. And this would block at most 100ms.
Use the source, Luke!
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: Two question about event and socket

Post by lfjking »

doublemax wrote:
but while i want to stop this thread. the socket blocking it
I don't have much experience with wxWidgets sockets, but looking through the API, i'd try WaitForRead() with a small timeout, e.g. 100ms. Instead of a blocking Read() call, you can check if there is any data to read first. And this would block at most 100ms.


thanks doublemax! I changed my code. This works better! thanks! :D
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: Two question about event and socket

Post by lfjking »

doublemax wrote:
ONEEYEMAN wrote:
thanks for help!
I rebuild my code.
I found that one of the sub events was really caused by a dead loop.
so . wxwidgets No problem!~ :D
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Two question about event and socket

Post by evstevemd »

lfjking wrote: the mapp is the parent :wxWindow* mApp;
sorry. the code is this:
wxPostEvent((wxApp*)mApp, wxThreadEvent(wxEVT_THREAD, threadsend));
Don't use wxPosEvent with secondary thread event rather use wxQueueEvent

From the docs of wxEvtHandler::QueueEvent it clearly says
QueueEvent() can be used for inter-thread communication from the worker threads to the main thread, it is safe in the sense that it uses locking internally and avoids the problem mentioned in AddPendingEvent()
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply