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?
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.
I have a C# app that receives updates from a server, processes it, and updates the GUI. the updates come in constantly, say several times a second. I want the app to update the GUI at most once every 2 seconds. so if an update comes in at time T, I want all updates that come in from T through T+2sec to stay in a buffer, and at T+2sec do the processing and GUI update. I know in JS you can use setTimeout() to execute some code at some time in the future, so I want something like that.
what's an appropriate way to do this? I've heard that using threads to "schedule" a function call isn't a great idea, but I'm not sure of a better way to do this. would it be so bad to use a Timer with a two second interval, synchronized to the GUI thread, that does the processing/updating?
You can use a System.Windows.Forms.Timer. It doesn't run events in a separate thread, it's the GUI thread that runs them. That way you can update the GUI directly without having to use Invoke.
You should use one of the timer classes, as you posted in your question.
See this MSDN article comparing them - this should give you a good basis to make a decision.
I am new to the world of GUI programming and I am writing a little GUI app using IronPython and WinForms. It reads values from an external device and displays them in a ListView component (name, value). I want to periodically perform the reading and updating of the ListView component at a certain fixed rate.
I had the following ideas to accomplish this:
A timer, which periodically triggers the read/screen update directly in the OnTick handler
A timer, whose OnTick handler triggers a BackgroundWorker to perform the read/update
Since the first solution will block the GUI until the read/update loop is done, which, depending on the number of values being read from the device, could take some time, I think the BackgroundWorker might be a better solution. I might want to add some functionality to manipulate the ListView items later (add, remove values etc.) and a blocked GUI does not seem like a good idea.
Is there a certain design pattern or a better way to accomplish reading/updating screen data?
NOTE: Any code examples can be IronPython or C#. The conversion from C# to IronPython is pretty straight forward. Thanks!
Personally, I'd have one thread that's responsible for reading values out of the device and storing the data in a data structure, and then a System.Windows.Forms.Timer (there's 3 Timers in .NET, this is the only one that ticks in the thread that's safe to update your controls) to read values out of that data structure and update the UI. You may need to synchronise that data structure, you may not.
That way the device can take as long as it likes to return data, or it can push as many millions of rows per second at you, and the UI thread will still poll at your predetermined tick rate (probably every 100 msec?). Since your timer ticks are just reading data out of memory, the UI won't block for IO.
The BackgroundWorker is prefered when you have lot of work to do in the background.
Use a Timer to trigger a function that will do the necessary work in a second thread.
It won't block the UI. (don't forget to update the controls on the UI thread).
System.Threading.Thread newThread;
newThread = new System.Threading.Thread(anObject.AMethod);
http://msdn.microsoft.com/fr-fr/library/ms173178(VS.80).aspx
Another option rather than getting the Timer and the Background worker thread working would be to use the System.Threading.Timer, this will execute your method on a thread on a regular interval and once you have the data you can use Control.Invoke or Control.BeginInvoke to update the control in the UI thread.
I have application where I read/receive data all the time (text) and I need to display this data into datagridview, what is the best way to do that in real time, so the data will be changed all the time.
I thought about multi threading, if this is a good idea can you guide me with link to explain how to implement it.
Thanks
You can use a BackgroundWorker to execute a thread in the background to receive data. Then you need to use Control.Invoke to marshall calls to the UI thread to update data.
This link provides some example code you can use as a starting point.
On a regular basis I find myself writing little utility programs that use some loop which takes a while to process. Yet while the loop is going the form no longer refreshes so if you were to move the form, or move another window over it and off, the form would be blank until the loop finishes.
Now I know the correct way to deal with this is to use a background working to do the time consuming task on a separate thread. But I haven't quite got my head around the multi threaded stuff just yet.
So If I'm only going to use one thread what is the best thing to do on each loop to keep the contents of the form up to date. For example there may be a progress bar in the form.
I've seen and used various combonations of Form.Refresh(), Form.Update(), and Application.DoEvents() but was wondering what is the best way to deal with this?
You can fake it with Application.DoEvents() at different locations in the code, but you really should put the work in another thread.
Take it as an opportunity to get into threading, you will have to sooner or later.
The Bgw makes it easy: The DoWork event runs on another thread but ProgressChange and Completed are synchronized on the main thread.
Find a few examples and be careful when you use shared data inside DoWork.