thread event while scrolling 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
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

thread event while scrolling

Post by mael15 »

hello,
i have a minimal exaple that demonstrates the following problem:
a) scrolling with the scrollbar triggers a thread
b) thread sends wxQueueEvent(scrollingWindow, new wxThreadEvent(wxEVT_THREAD, 5)) back to the scrolling window
c) Problem: the wxThreadEvent only gets executed AFTER scrolling has ended and the left mouse button is lifted.
in the documentation for QueueEvent it sais "Finally notice that this method automatically wakes up the event loop if it is currently idle". but the event loop obviously starts only after the left mouse button is lifted.
how can i make the scrollingWindow event loop run the wxThreadEvent while scrolling?
thank you!

app.h

Code: Select all

#pragma once
#include <wx/thread.h>

class MyFrame1;

class threadClass : public wxThread {
public:
	threadClass(MyFrame1 *mf) : myframe(mf) {
		sema = new wxSemaphore();
	}
	~threadClass() {
		delete sema;
	}

	virtual void *Entry();

	wxSemaphore *sema = nullptr;
	MyFrame1 *myframe = nullptr;
};

class MyFrame1 : public wxFrame 
{
public:
	MyFrame1( wxWindow* parent = NULL, 
		wxWindowID id = wxID_ANY, 
		const wxString& title = wxEmptyString, 
		const wxPoint& pos = wxDefaultPosition, 
		const wxSize& size = wxSize( 200,100 ), 
		long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
	~MyFrame1() {
		thread->sema->Post();
		if(thread->Delete() != wxTHREAD_NO_ERROR )
			OutputDebugString(wxT("Can't delete the thread!\n"));
	}

private:
	threadClass *thread = nullptr;

	void onThreadEvent(wxThreadEvent &evt) {
		OutputDebugString(wxT("onThreadEvent\n"));
	}

	void onScroll(wxScrollEvent &evt) {
		wxScrolled<wxPanel>*obj = (wxScrolled<wxPanel>*)evt.GetEventObject();
		OutputDebugString(wxT("onScroll\n"));
		static_cast<MyFrame1*>(obj->GetParent())->thread->sema->Post();
	}
};

class App : public wxApp {
	virtual bool OnInit();
	virtual int OnExit();
};
app.cpp

Code: Select all

#include "app.h"
#include <wx/scrolwin.h>

bool App::OnInit() {
	MyFrame1 *mainFrame = new MyFrame1();
	mainFrame->Show();
	SetTopWindow(mainFrame);

	return TRUE;
}

int App::OnExit() {
	return 0;
}

IMPLEMENT_APP(App)

MyFrame1::MyFrame1( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : 
	wxFrame( parent, id, title, pos, size, style )
{
	wxBoxSizer *siz = new wxBoxSizer(wxHORIZONTAL);

	wxScrolled<wxPanel> *scrPanel = new wxScrolled<wxPanel>(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL);
	scrPanel->Connect(wxEVT_SCROLLWIN_THUMBTRACK, wxScrollEventHandler(MyFrame1::onScroll));
	scrPanel->SetScrollbars(1, 0, 1500, 0, 0, 0, true);
	wxStaticText *text1 = new wxStaticText(scrPanel, wxID_ANY, wxT("asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf"));
	scrPanel->SetSizer(new wxBoxSizer(wxHORIZONTAL));
	scrPanel->GetSizer()->Add(text1, 0, wxEXPAND);

	siz->Add(scrPanel, 1, wxEXPAND);

	SetSizer(siz);

	thread = new threadClass(this);
	thread->Run();
	Connect(wxEVT_THREAD, wxThreadEventHandler(MyFrame1::onThreadEvent));
}

void *threadClass::Entry()
{
	while (!TestDestroy()) {
		sema->Wait();
		OutputDebugString(wxT("queuing thread event\n"));
		wxQueueEvent(myframe, new wxThreadEvent(wxEVT_THREAD, 5));
	}
	return nullptr;
}
background: in my program, there is a big wxImage painted in the scrolledWindow. i only paint the visible part, additional parts are added from the thread while scrolling. so at the moment these additional image parts are only added after scrolling has finished and not while scrolling.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: thread event while scrolling

Post by doublemax »

Platform, wxWidgets version?

If you're under Windows, it could be the same as this: https://trac.wxwidgets.org/ticket/17579
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: thread event while scrolling

Post by mael15 »

3.1.0 windows, looks like this could be the cause. I will try the solution first thing on Monday, thank you! :)
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: thread event while scrolling

Post by mael15 »

doublemax wrote:Platform, wxWidgets version?

If you're under Windows, it could be the same as this: https://trac.wxwidgets.org/ticket/17579
unfortunately, the fix included in this ticket is already implemented in my wxwidgets version 3.1.0.
any other idea?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: thread event while scrolling

Post by doublemax »

unfortunately, the fix included in this ticket is already implemented in my wxwidgets version 3.1.0
Are you sure? The 3.1.0 from http://wxwidgets.org/downloads/ still has the bad code.
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: thread event while scrolling

Post by mael15 »

you are right, i got confused what was the fix and what the reversion.
the problem is, now my sample program immediately closes after start.
maybe i have to fix something else?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: thread event while scrolling

Post by doublemax »

the problem is, now my sample program immediately closes after start.
maybe i have to fix something else?
The difference from 3.1.0 to current HEAD shouldn't be that big. Did you make a clean rebuild of everything? Do the samples run?
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: thread event while scrolling

Post by mael15 »

doublemax wrote:Did you make a clean rebuild of everything?
after a fresh download, applying the fix mentioned above and rebuilding everything: everything ok now. hope i did not lose changes from weeks before that i do not remember anymore...
thank you again!!
Post Reply