I've an application witch update execute commands on a database. I've a function witch execute that commands and in the in this function I display some messages to the user.
My problem is that the texts written into the textEdit are displayed at the end of the function work.
I've tried to use QThread but it does'nt resolve the problem
this->moveToThread(&threadText);
connect(&threadText, SIGNAL(started()), this, SLOT(writeTexte()));
threadText.start();
Does anyone have an idea how to proceed to write into textEdit and not block the UI ?
In Qt you never need threads in standard UI applications! That is because Qt uses something called event loops that process events smoothly without ever blocking. If you experience blocking, that is a sign that you have a bug somewhere in your code. For any beginner there are some pitfalls. The official documentation on the subject is quite good.
It is hard to tell what your exact bug is because there is littel code to look at. I suggest you post more of your code, especially the naive approach that did not work before you added threads to the mix.
On a side note, using threads in Qt is of course possible and has it's usages, however it can be un-intuitive. I have found this article to be a really good starting point to understand how threads is used best in Qt.
Related
I am in need of a solution, and I am not quite sure I have enough knowledge to properly ask the question, so please bear with me. I am working with a CAD application with it's own API which supports the .NET Framework 4.5. I wanted to develop some customized functionality for the application using VB.NET, but because of work restrictions I am not allowed to install custom programs or run custom executables. I am however allowed to utilize the CAD applications scripting environment (which also supports the .NET Framework). I am limited in what I can achieve with scripting because as far I know I can't listen for an event in a script, because the scripts run time ends so quickly. Is there a way to extend the run time of a script until certain events occur? If anybody is curious, the CAD application I am using is called Siemens NX. Any Ideas?
I do not know how far this answer help you.
When you do not have events to listen, try to depend on return variables and using assert statements (or at least if statements).
I mean, if something happens then only go to next, which is a very traditional way.
Also, if you want to prolong the run time of script you can use some thing like "sleep" or "delay" statements (may be milli seconds as input).
I mean if some thing is happening (control by a variable), "sleep" till that action is complete.
Or check for the action complete status in a infinite while loop and exit, when it is done.
In simple terms, traditional way of doing things helps in your situation.
I'm planning on writing a program but I am stuck in a conundrum. I don't want to start writing something and then have to rewrite it all when I find out that my program is not extensible. The other problem, is I do not have enough programming knowledge to know where to begin designing my program so that it is extensible.
I have done some reading on DLLs (or delayed loading for unix), dynamic loading, run-time library loading/deloading, etc.. but I still cannot quite comprehend what I need to do. I will give a sample program example, and if someone could lead me in the right direction for what to learn so my bigger program can begin, I will be extremely grateful!
Let's say I create a console program 'iAmDog' where you can type in commands, and the dog will respond accordingly with output to the console. Now let's assume when I create this program, the dog only has 1 command, 'bark' which produces the output 'roof roof!'. How would I go about writing this program so that while the user is still running the program, I can edit a library, or code somewhere else, to add a 'sit' command to the dogs repertoire.
Again, Ideally my plan is to have no downtime or as minimal downtime as possible, all while being able to code updates to functionality of the always running program.
Thank you for reading!
Russell aka SgtPooki
Are you using .NET? Then MEF, Managed Extensibility Framework, is probably suited well for you.
There is a podcast on Hanselminutes about MEF. You find an mp3 as well as a PDF transcript, that lets you search the show. They even discuss the possibility to continuously watch a folder for updates, to achieve what you descripbe, although they discourage to do this.
Firstly, I've never used threads, but have found lots of examples on the internet about their use but nothing that obviously answers my question.
I have a class that loads and manipulates a file(s). It is fairly CPU intensive so I intend to put it in its own thread so that the GUI remains responsive. However, I would also like to use a progress bar to indicate the current status of the file operations.
The question is, what is the best way of approaching this, i.e. How do I get my file class to tell the app where it's up to? Do I have to add thread specific code to my class? Or is there an interface I can implement? Or, am I approaching this all wrong. Additionally (sorry, another stupid question) I assume I need an indicator in my file class to tell the thread when it's finished?
I'm using VS2010 and intend to build the app with WPF (if that's relevant)
Thanks for any advice,
Bob
Since you are intending to use WPF, use a Dispatcher:
Build More Responsive Apps With The Dispatcher
(Otherwise, for Winforms use a BackgroundWorker)
I was struggling with getting some asynchronous activity to work under PyGTK, when someone suggested that I look at using Twisted.
I know that Twisted started as a networking framework, but that it can be used for other things. However, every single example I've ever seen involves a whole lot of network-based code. I would like to see an example of using Twisted for a simple PyGTK desktop app, without the needing to expend the extra mental effort of understanding the network aspect of things.
So: Is there a clean, simple tutorial for or example of using Twisted to create a GTK (PyGTK) app and perform asynchronous tasks?
(Yes, I've seen pbgtk2.py. It's uncommented, network-centric and completely baffling to a newcomer.)
Updated: I had listed various gripes with glib.idle_add/gtk.gdk.lock and friends not working properly under Windows. This was all reasoned out on the pygtk list - there's some trickery that is needed with PyGTK to get asynchronous behaviour working under Windows.
However, my point still stands that any time I mention doing asynchronous activity in PyGTK, someone says "don't use threads, use Twisted!" I want to know why and how.
Twisted to perform is asynchronous tasks in pygtk simply uses functions such as gobject.io_add_watch/glib.io_add_watch and gobject.timeout_add/glib.timeout_add (plus some others, you find them in the gobject and glib module), so there's not much difference in using raw pygtk functions or twisted if you don't need networking.
As an addition twisted has the same problems as pygtk with asynchronous tasks, twisted use the same loop as of pygtk and so it gets blocked if you perform some blocking task!
The best thing to do is to use one of the glib functions that are intended basically for handle such situations.
I've tested in an application the correct behaviour under windows of twisted+pygtk but I avoided to do blocking stuff (max reading from a large file, chunk per chunk basically using glib.idle_add or glib.io_add_watch, in the sense that twisted uses something like that).
For example I'm not sure that spawning process and processing stdout with glib.io_add_watch seems to not work. I've written an article on my blog that handle the performing of asynchronous processes in pygtk, not very sure that works on windows though it may depend on the version.
I have a college assignment due quite soon, and I need to be able to call a C++ dll that takes a long time (possibly infinte, it relies on user input)to execute. Im calling this through VB. My VB GUI freezes up when this happens, and I would like to keep the GUI responsive, so that the user can stop this possibly infinte loop.
Can anyone suggest the best/fastest way of doing this?
A bit of background, the C++ is trying to keep score on a snooker table using a webcam, and while the VB scoreboard updates easily, I would like to script it so that the analysis is almost continuous, while still allowing the user to interact. Currently the project requires the user to press a button to start the shot analysis, but it would be preferable if the program scripted itself. I have only realised this problem now and the deadline is very soon.
Update: Our lecturer suggested an option to solve the problem, but it would appear that most options here and the one he suggested will not work for us as the processing time required for the webcam image capture is too great to handle due to hardware constraints. Thanks for taking the time to help, it was much appreciated!
The best way to handle threading in VB.NET is via the System.Threading namespace.
You might also look into Application.DoEvents()
Try the system.Threading as Mark said, also try looking at Background Worker Process which is a bit simpler in VB.NET. Readup here
I would definitely use the Background Worker process. You can drag it onto your form and use the DoWork sub routine to actually do the work that is freezing your GUI thread. You can also use the ReportProgress event to actually provide progress back to your form.
As for your question regarding two separate threads, If both steps take a long time to complete I would run them in the same thread one after the other.
The one thing that could bite you with using the thread is cross-threading. In the context of your problem this means not having your second thread update form controls.
A really good resource for how to implement this code along with dealing with cross-threading is this PDF.
I also should point out that if you are using .net 1.0/1.1 you can still do the multi-threading, but don't have the luxary of having a background worker control. You'd just have to create a new thread from the System.Threading Namespace.
Just as an alternative, you could have your C++ actually processing in the background all the time. When called from VB, it would just be retrieving data from it or sending it a command (start, quit, ???) all of which would return instantly.
This could also give you more reliability since C++ would never miss video frames while VB was collecting the garbage or doing the dishes or whatever VB does in the background--C++ is going to let you be closer to a real time system.
RE: the comment about how.
What I'd probably do is have my VB programs send "Messages" to the C++ (As I said). A message is just a way to think of a function/method call--but generally they return quickly.
The "Start" message would tell the C++ code to start it's thread running and return. This is a Linux C++ thread howto, I'm not sure if you need to do something different in windows (I'd hope not, but I haven't used C++ as my main dev. language in decades).
If that doesn't work, just google "C++ Threads"
Sending a "Stop" message would stop the thread (and probably free resources).
A "Get Data" call would go to the location that the C++ thread used to store data, grab it and return.
Sorry to be so general, I'm pretty heavily Java these days.
I'm surprised nobody has suggested using a BackgroundWorker yet.