ui-thread

No error “Only the original thread that created a view hierarchy can touch its views” when the view is updated without delay

瘦欲@ 提交于 2019-11-28 07:26:54
问题 I was faced with an interesting problem. If you write the following code in the onCreate/onStart/onResume method of activity: final Button myButton = (Button)findViewById(R.id.myButton); final TextView myTextView = (TextView)findViewById(R.id.myTextView); final Thread thread = new Thread(new Runnable() { @Override public void run() { myTextView.setText("Hello text"); } }); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { thread.start(); } }); or:

Run code on UI thread without control object present

戏子无情 提交于 2019-11-28 07:01:40
I currently trying to write a component where some parts of it should run on the UI thread (explanation would be to long). So the easiest way would be to pass a control to it, and use InvokeRequired/Invoke on it. But I don't think that it is a good design to pass a control reference to a "data/background"-component, so I'm searching for a way to run code on the UI thread without the need of having a control available. Something like Application.Dispatcher.Invoke in WPF... any ideas, thx Martin Judah Gabriel Himango There's a better, more abstract way to do this that works on both WinForms and

WPF/C# Don't block the UI

ぐ巨炮叔叔 提交于 2019-11-28 05:40:41
问题 I've an existing WPF application, which has several sections. Every section is a UserControl, that implements an interface. The interface specify two methods: void LoadData([...]) and bool UnloadData() . Those method are called by the UI thread, so we need to do our work in backgroundworker if it's time consuming. No problems with LoadData since we can update the UI asynchronously. The problem is with UnloadData(). This should return if we can really leave the current view. This is computed

Interacting with UI threads in Java/J2ME

醉酒当歌 提交于 2019-11-28 01:33:00
问题 I'm writing a J2ME application. One of the pieces is something that polls the contents of a directory periodically, and, if there are any new things, paints them on the screen. I've done this by having the UI form launch a polling thread with a pointer back to itself, and when the polling thread finds something it calls back to the form and calls a syncrhonized method to update it's display. This seems to work fine. The question I have is this. In C#/.NET I know it is not nice to have non-UI

What happens to an AsyncTask when the launching activity is stopped/destroyed while it is still running?

ぐ巨炮叔叔 提交于 2019-11-27 21:47:50
问题 I've seen few questions nearly identical to mine, but I couldn't find a complete answer that satisfies all my doubts.. so here I am.. Suppose that you have an activity with an inner class that extends the AsyncTask class like this: public class MyActivity extends Activity { private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { protected Bitmap doInBackground(String... urls) { return DownloadImage(urls[0]); } protected void onPostExecute(Bitmap result) { ImageView img =

Why is a single threaded model used to update the UI as main thread?

房东的猫 提交于 2019-11-27 20:58:43
问题 The Qt doc says, As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. The Android doc says, Like activities and the other components, services run in the main thread of the application process And iOS, It is strongly recommended not to update UI controls etc from a background thread (e.g. a timer, comms etc). This can be the cause of crashes which are

Android ViewPager automatically change page

僤鯓⒐⒋嵵緔 提交于 2019-11-27 18:34:44
I want to schedule an action to change automatically my ViewPager pages. I've tried: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... swipeTimer = new Timer(); swipeTimer.schedule(new TimerTask() { @Override public void run() { if (currentPage == NUM_PAGES) { currentPage = 0; } featureViewPager.setCurrentItem(currentPage++, true); } }, 100, 500); but I'm always getting: E/AndroidRuntime(5381): FATAL EXCEPTION: Timer-0 E/AndroidRuntime(5381): java.lang.IllegalStateException: Must be called from main thread of process I'm already in main thread

Difference between Handler.post(Runnable r) and Activity.runOnUiThread(Runnable r) [duplicate]

被刻印的时光 ゝ 提交于 2019-11-27 14:56:21
This question already has an answer here: What's the difference between Activity.runOnUiThread(runnable action) and Handler.post()? 2 answers Is there a difference between new Handler.post(Runnable r); and activity.runOnUiThread(Runnable r) runOnUiThread is basically suited to show a progress dialog or do some UI manipulations before an AsyncTask call. If you want to update the UI in the middle of a thread execution, then the best approach is to create a Handler which will update your UI, and let the thread continue running, for example, updating a text view periodically after a few sec, say

Is it possible to initialize WPF UserControls in different threads?

自古美人都是妖i 提交于 2019-11-27 12:09:21
We are developing a WPF application which will open a number of reports at the same time (just like a typical MDI application such as Excel or Visual Studio). Although it is possible to have the data context for those reports run in multiple worker threads, we still find that if the number of opened reports is really big, even the rendering of those reports (basically UserControl hosted either in a MDI environment or just in a grid area in the main view) will still make the application less responsive. So, my idea is to at least have several areas in the main UI, each of whom will have its

Is it possible to use AsyncTask in a Service class?

孤街醉人 提交于 2019-11-27 08:03:57
Everything is in the title. On the official documentations it is stated that Note that services, like other application objects, run in the main thread of their hosting process and AsyncTask only works if it is executed in the UIThread. So is it possible to use AsyncTask in a Service class? I am trying to do so but I'm always getting the same error 05-01 18:09:25.487: ERROR/JavaBinder(270): java.lang.ExceptionInInitializerError ... 05-01 18:09:25.487: ERROR/JavaBinder(270): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() Am I doing