I have two function in my viewModel, one of which brings data (say from Database which may take time depends on data and network ) and second method generate UI dynamically according to data.
I want to show Busy Indicator on first function call , but i don't want it to be Async .
Is there any way to show RadBusyIndicator without Async Call to the function,
i tried to put the first function in thread and then put the main thread on waiting for the working thread thread, but it doesn't work for me .
Thanks in Advance
If you don't want the long running Data fetch operation to happen on a different thread than the UI one. Then only way I can think of is to have a separate Window with waiting/busy indicator. And create this window instance and show it from a separate thread.
Close the window & thread when the Data Fetch completes. So you are going to need quiet some multi-thread sync & communication.
Related
I am building two applications that work on the same database. My problem is that i want to create an automatic update.
Details :
I have a farmacy and on the main form i am displaying some data (list of medicines for example).
I have another application that allows me to add/edit/delete drugs.
I want the first application to update from time to time (1-2 seconds).
I tried creating another thread in the main application and call it with a 2 seconds sleep, but it frezees the application and i don't want that. I want the applications (both of them) to be functional and do a background update somehow.
Ideas ?
A BackgroundWorker would allow you to do this.
If you use an instance of System.Threading.Timer to update the display every X seconds, you can rely on the tick callback to be on a background thread. This will keep the display from freezing.
Remember, you can only access controls on the thread they were updated on. If you need to access a control on a worker thread, you can use the InvokeRequired/Invoke pattern (in WinForms).
Take a look at the Async CTP. It will help you do stuffs in background and update your views (Forms) later.
This one is a tough one...
I've got a C# app that runs an HTTP server in a BackgroundWorker, it allows users to input data, that should display in real time onto a main form. The HTTP server basically runs in a do/while(true) loop, that always waits for the HTTP response. So it has to run in a BackgroundWorker so that the program does not stop responding while waiting for the web user. However, when the user enters data, I want the background worker to update the data on the main form.
I've tried making the functions of the main form public, but this gives a run time exception because the thread accessing the forms controls is not the thread that created them.
I've thought about just using the ProgressChanged event of the BW, but I've got to pass a lot of data, and it would be nicer to pass more than just a string. Can I override this method, and if I can, how/where would I do it??
Thanks.
Go ahead and use ProgressChanged as-is. It doesn't really matter how big the object you're passing as the userState parameter is, as long as:
It's a reference type (that is, not a struct).
The background process doesn't modify its contents after passing back to the main thread, to avoid a race condition.
Here's a simplified example of what I'm trying to do:
I have 2 controls MyControl c and Panel p. p is created in the main GUI thread as normal, but I want c to be created in a background thread, because it takes awhile and I don't want to freeze the GUI. How can I add c to p.Controls? If I do it in this.Invoke it throws an exception for c, and if I do it from the background thread it throws an exception for p.
Or is it that I really shouldn't be creating GUI elements outside of the main GUI thread?
Or is it that I really shouldn't be creating GUI elements outside of the main GUI thread?
Yes, this is basically the problem. Most controls have thread affinity, and must be created within the user interface thread.
Typically, the way to handle this is to move the "work" that is causing the control creation to be slow onto a background thread, but still create the control on the UI thread. You'd then use Control.Invoke or Control.BeginInvoke to update the information in the UI once the slow work was completed.
For example, if you're loading a set of data from some external source, you can create the control, and start a background thread to load the data. Once the data is loaded, you can then update the control to reflect it. This will cause it to stay responsive the entire time.
It may be that your control does not take a long time to create; rather it takes a long time to get its data. In that case, consider creating a control that can display it UI while it is being populated. Then you can create the control in your main thread, populate it in a background thread, and give your users a better experience.
Use background thread to do what it has to do, and then signal somehow (bool _backgroundCompleted as a trivial example) that c is ready to be created.
Create c from Timer that will fire periodically and do nothing until _backgroundCompleted is set, then create c, then kill the timer.
It might be 'ugly', but it will work and will remain simple.
Forms.Timer, if you are asking ;)
And yeah, NEVER ever mess with multiple threads on the same form. You can have several forms on several threads, but to do that, you have to first RUN a new thread, then create a form ON it. It will have its own message loop, and will do fine.
I have datagridview and need to update it on separate thread. Or maybe even on same thread but only when data ready. I have 90% of data loading right away and would like to show it to user. Then I have 2 more columns that can load 20+ seconds sometimes and would like to update them as soon as data ready. What is the best way to accomplish this?
In WinForms, your best bet is BackgroundWorker, it's a nice class for doing work on a different thread and updating a WinForms based UI.
Having not seen any code, I would suggest creating your own event. Once the data is ready, you throw a new event with the new data as the argument, and then load that. It'll happen on the same UI thread though.
Not sure if you already have a way to poll for that being completed. Making a thread to run in the background and just poll for that wouldn't be too hard.
Where is the remaining 10% of your data coming from? What are you binding to? Is this a scenario where you're making one hit to your data source for the "90%" and then making a totally separate hit to go get the rest of your data?
I'm doing a windows form and would like an audit task to happen every 30 seconds. This audit is essentially checking a series of services on remote computers and reporting back into a richtextbox the status.
Current I have this running in an endless background thread and using an invoker to update the richtextbox in the main form.
Is this best practice? If I made an endless loop in my main form that would prevent any of my buttons from working, correct?
I'm just curious if every time I want to create a periodic audit check I have to create a new thread which checks the status or file or what have you?
Edit: I looked further into the Timer class and decided to go with System Timer as it proved to be better with a longer function. Thanks for pointing me in the right direction.
You should look into the Windows Forms Timer class. You don't want a loop in your main form. It's better to use the timer to fire events which can be processed asynchronously on another thread.
(I assume this is a winform application)
Invoking on the main thread is the way to go. But what about using a timer instead of an endless loop? It gives you more control. And a the timer function would execute on it's own thread.
It's good practice to let long going work execute on a background thread, so that the main thread can work with the UI.