I use TIdHTTP component to load xml data from a bank in a seperate thread but my form is getting freezed during that time...
what could be the problem ?
I have a main form and thread class, in thread class i have a method called loadData and on thread::Execute i Synchronize(loadData);
when button gets clicked I created the instance of thread class like testThread *t=new testThread(false);
and that's all
when i click the button the main form freezes?
even seperate thread didn't help????
Please help!!!
Synchronize() is running your loadData() method in the context of the main thread, not in the context of your worker thread. That is why your main thread blocks while loadData() is busy. You are misusing Synchronize(), rendering your thread useless. You need to do the bulk of your thread work outside of Synchronize(), and then use Synchronize() only to perform small updates in the main thread when needed, like displaying status (even then, Synchronize() is not always the best choice for that).
Related
I want to have the code executed by the OnTimer event to be executed in a separate (non-Main) background thread. this code does not access or communicate with the main thread/GUI. Simple question, I get the timer (TJvThreadTimer) is executed in it's own background thread, but:
Does the code contained in TJvThreadTimer.OnTimer event get executed in that background thread as well?
It is unclear from the limited documentation.
Thanks......
If you look at the timer's source code for yourself, you will see that the OnTimer event handler is called inside of a class method that is Synchronize()'ed by the internal background thread, which means the event handler runs in the main UI thread.
I am working with CDialog in MFC.
In my dialog, I create a ui-thread:
CWinthread *threadCom = dynamic_cast<MyThreadClass*>(AfxBeginThread(RUNTIME_CLASS(MyThreadClass)));
Somewhere in my dialog class, I write:
threadCom->MyFunction();
My question is: "MyFuction()" is executed in main thread of dialog or ui-thread
Update after comment of AlexanderVX
For example, when user clicks on button on dialog
CMyDialog::OnBtnClicked() { threadCom->MyFunction(); }
"Calling a function" like calling a member function of a class is an action in your current thread.
If you want that Actions are executed asynchronously in the other thread you need some communication between the threads.
If the second thread has also a UI, it is possible to use messages (PostMessage) to inform the thread about an action. PostMessage is also a good way to inform the main thread about results from the worker thread.
If you need to transfer data too (more than a DWORD), you need a more complex data exchange that should be thread safe too.
I am trying to use MFC to create a tool. This tool main job is to sort data. Well, I found that when the tool is sorting, since there is only main thread; therefore, while it is doing sorting work, no dialog boxes can be moved or clicked. Hence, I created another thread to do sorting work and works fine.
But there is another problem after I used a thread. I don't how to make main thread to wait for the sorting thread. I want to do something after sorting thread is done, but right now, main thread just moves onto the next procedures without waiting for sorting thread to finish its work.
Here is snippet
AfxBeginThread(processfiles, tVals) // A thread do its work.
// below I want to do something with the result I got from the thread above.
//But main thread just do its work separately without waiting for the thread to finish its work.
.
please help thanks!!
So write code to do that. Pop up a dialog box. Indicate that the sorting is taking place. Do whatever you want. Have the other thread send your thread a signal when it's done, say by sending you a message with PostMessage.
I used this tutorial http://delphi.about.com/od/kbthread/a/thread-gui.htm to create a class that asynchronously downloads a file from the internet in another thread with a TDownLoadURL. I did this because I want to download a file without blocking the UI thread so the program doesn't become unresponsive during large downloads, the progress bar can update, etc.
I am having a problem because even though I have done the download in another thread (inheriting from TThread and doing the work in the Execute method) the GUI thread seems to be blocked and does not process messages until the download is finished. Here is the code for my class: http://codepad.org/nArfOPJK (it's just 99 lines, a simple class). I am executing it by this, in the event handler for a button click:
var
frame: TTProgressFrame;
dlt: TDownloadThread;
begin
dlt := TDownloadThread.Create(True);
dlt.SetFile('C:\ohayo.zip');
dlt.SetURL('http://download.thinkbroadband.com/512MB.zip');
dlt.SetFrame(frame);
dlt.SetApp(Application);
dlt.Start;
Note: The SetApp method was for when I was manually calling app.ProcessMessages from inside the UpdateDownloadProgress method of my class TDownloadThread. This would keep the GUI from being unresponsive, but it made the progress bar behave wierdly (the moving glowing light thing of aero's progress bar moving way too fast), so I removed it. I want to fix this properly, and if I have to call ProcessMessages there's really no point in multithreading it.
Can someone help me fix this problem? Thanks.
I now have the solution for you!
Calling TDownLoadURL.Execute (your call to dl.Execute in TDownloadThread) results in the action being transferred back into the main thread which is why your UI becomes unresponsive.
Instead you should call ExecuteTarget(nil) which performs no such machinations and works as you intend: the download runs on the worker thread.
Error Log says:
bool _WebTryThreadLock(bool), 0x3c689f0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
App structure:
worker threads are detached from the MainThread as new data is needed via user interaction, each worker thread feeds data into its own slot in an array. The problem arises only when I use the NavigationController to go "back" to the previous view WHILE a thread is still gathering data. I've tried to send a [NSThread exit] to each thread upon viewWillDisappear thats not going to work...
Any suggestions on thread clean-up upon poppin' the view controller?
So apparently i needed to build in checks to the runloop as well as the didRecieveData loop to watch for a global variable to indicate if the view has disappeared. This in turn needs to toggle the global var, causing all open threads to cancel the connection as well as exit the thread.