I need to make an application in .NET CF with different/single forms with a lot of drawing/animation on each forms.I would prefer to have a single update[my own for state management and so on] function so that i can manage the different states, so that my [J2ME Gaming Code] will work without much changes.I have came to some possible scenarios. Which of the one will be perfect?
Have a single form and add/delete the controls manually , then use any of the gamelooping tricks.
Create different forms with controls and call update and application.doEvents() in the main thread.[ while(isAppRunning){ UPDATE() Application.DoEvents() }
Create a update - paint loop on each of the form as required.
Any other ideas.
Please give me suggestion regarding this
If its a game then i'd drop most of the forms and work with the bare essentials, work off a bitmap if possible and render that by either overriding the main form's paint method or a control that resides within it (perhaps a panel). That will give you better performance.
The main issue is that the compact framework isn't really designed for a lot of UI fun you don't get double-buffering for free like in full framework, proper transparency is a bitch to do with WinForm controls and if you hold onto to the UI thread for a little too long you'll get serious rendering glitches. Hell you might even get those if you do too much on background threads! :O
You're never going to get optimal performance from explicitly calling Application.DoEvents, my rule of thumb is to only use that when trouble-shooting or writing little hacks in the UI.
It might be worth sticking the game on a background thread and then calling .Invoke on the control to marshal back to the main UI thread to update your display leaving the UI with plenty of time to respond while also handling user input.
User input is another reason I avoid normal winform controls, as mobile devices generally don't have many keys it's very useful to be able to remap them so I generally avoid things like TextBoxes that have preset key events/responses.
I'd also avoid using different forms as showing a new form can provide a subtle pause, I generally swap out controls to a main form to avoid this issue when writing business software.
At the end of the day it's probably worth experimenting with various techniques to see what works out for the best. Also see if you can get any tips from people who develop games on CF as I generally only do business software.
HTH!
Related
I have a C#/WPF/.NET 4.5 app that users will use to open certain files. The app will then go through a lot of motions - read the file, pass it trough a number of plugins and parsers. The files are potentially fairly big (>100MB) so this could take a while. I want to keep the user informed about what is going on in the UI, so in my viewmodel I have some stuff for showing current status and a progress bar.
Now I want to be cool and modern and do this using ReactiveUIs Async command support, which is completely new to me.
For the feedback messages from the load/parse process, what is the best approach? The status message and progress bar val/max values need to be set on the UI thread, pretty basic stuff. Should I implement my data loader as an IObservable, or is it somehow better to do this using for instance the MessageBus component?
Keep in mind that a user might be loading several of these huge files into the app at once, and I want the UI to remain as responsive as possible while the loading is going on.
Examples of how to go about implementing this properly would be hugely appreciated!
I checked this with The Master himself (Paul C. Betts) elsewhere, and he told me that the way to handle this would be for the app that displays the progress information to create a Subject<SomeProgressInfo> and pass this to the guys doing the actual loading/parsing. These can then push new information to the host using .OnNext().
This also helps with unit testing, where one can just provide a dummy Subject<T> that won't be subscribed to.
i have a c# application and there are a lot of forms with controls.
And every time when i run my application, forms which have many controls open slow.
So is there any way to make it open faster?
As you can see from the comments, there isn't one universal "make things faster" technique. You need to find the bottleneck and fix it. Here are some pointers:
Are all your controls on all your forms added statically in the designer, or added dynamically at runtime based on environment/user details/loaded dynamically via reflection? These can significantly slow down UI load time.
Do you have hundreds of controls on a single form? If so, consider splitting your forms to smaller chunks.
Do you have complex logic or data access during your Form_Load events? This can also slow down UI responsiveness. Consider starting the application "clean", and then loading the data asynchronously.
Use a profiler! Find a good, simple profiler to see where, exactly, you're spending your time. You'll often be surprised at what actually takes time. Use a trial version of a good, established profiler like dotTrace or Ants, and consider buying it for the future.
Instead of making it load faster, I would recommend you to give better user experience to your user.
First, load a Splash View (with loading progress) first. On next thread running in background, load your View with a lot of controls. When your view loaded completely, hide the Splash View and show your View.
In case you do something timeconsuming code with the controls in form constructor or while loading form you can write this.SuspendLayout(); before that code and this.ResumeLayout(); after that code. That prevents several time-killing layout operations.
Considering the information in question provided I wolud say:
Devide controls in possible groups (It's practically impossible that user needs all controls at the same time) and put them on separate Tabs of tab control.
Automatically (statially) load only controls on fisrt Tab, for others load them dinamically.
In this case, if the user doesn't need certain functonality will never switch to the Tab that contains it, so the controls of that functionality that user doesn't need will not be loaded, which will provided overall faster load time.
If there is a data that should be pushed on the controls, load it in async way where it's possible,
Hope this helps.
I have had a similar problem, but with many more controls than yours.
Considering that you have a relatively small set of controls, I think the culprit is the data access layer slowing down everything. (The loading of that grids and combos for example)
A simple action that can tell you many things is to move your data access to the form_shown event.
In Visual Studios (under the Solution Explorer), you can right click your solution/solution name and select properties at the bottom of the list. If you select "Configuration Properties," then go under configuration, you can select "Release" instead of "Debug." This will shave time off loading significantly.
Also, it is wise to avoid using "Thread.Sleep" in your code. This method is often used and mostly for poor code writers. About 10% of it is viable for particular circumstances, but I would avoid it at all cost.
Best way is to separate your solution to many layers
YourAppUI contains forms
YourApp.Controls contains controls
YourApp.BLL contains business logic
YourApp.DAL contains data access layer
I am having an application which is for securities trading in stock exchange. It has real time market feed from one of the vendors. We are processing market data on worker thread(s) and while updating main GUI we are marshaling this data to GUI thread.
Let me make this more clear, there is an ultragrid (third party gridview) having 80 columns, we have around 40 rows with same security that must be updated as and when there is fluctuation in market data. We are able to handle market frequency of 10 messages/sec/security. But beyond that we are unable to handle. GUI becomes non-responsive when frequency increases to 25-30 msgs/sec/security. We have followed best practices while designing and implementation but still we can not handle high freq. We are performing all non-GUI specific work on worker/back-ground threads, but still facing GUI hang. Please help in suggesting me any out of the box solution to tackle this problem. Here I can not put code snippet due to security reasons.
Switch your grid component to something that can double buffer and multithread (I expect most data will remain static around updates), or roll your own. Rolling your own sounds unavoidable: this kind of heavy update activity isn't the most common use case for grid controls. I'd start looking for grid controls with open source code you can modify, at e.g. http://www.codeproject.com or http://www.codeplex.com. What to pick depends on your other requirements, alas.
The key thing here is to virtualise everything - the only interaction between your data model and the grid should be when the relevant cells need to be painted. In order to do this you will need to use a grid that supports this kind of virtualisation (I've used a Syncfusion grid to do this before, see example here).
You will also want to batch the updates to your data model so that you don't block the GUI thread too frequently. Have a look at this question and answer.
I'm writing an application in native code and wish to use WPF for a user-interface library. Now, I've got the interoperation down relatively easily, but ultimately, I'm trying to write a native app, with WPF as a library, not a WPF app with a few native extensions. That said, I'm looking for an interoperation solution with certain qualities that I just can't seem to find in any examples.
Firstly, I definitely want to write my own main loop. I might perform arbitrary actions in native code and I want the ability to poll, for example, in each iteration. My application is definitely not a "sleep until you click something" application.
Secondly, I want the ability to control WPF. I want to say when WPF processes input. I want to say when it renders. I want it to make me a sammich, when I say so. This is one of the bigger problems that I have. Surely these methods must exist inside WPF, they're just not exposed anywhere that I can find them.
I've been looking into CLR hosting, but it doesn't seem to have a kind of, "Go until finished" thing that I'm looking for. Even if I cracked open a critical section or something, even from managed code there's no way to make WPF do what you want.
If I understand your question correctly, it sounds like you want to write your own main loop/message pump for WPF. I remember reading about this in a message thread a while back. It's not a full answer, but I wonder if it might point you in the right direction.
Subject: Game loops in WPF?
wondering how easy it would be to write a custom "game loop" within the WPF framework. By game loop, I mean pumping my own events on a custom Dispatcher so I can control the framerate of my app.
Answer:
You can write your own message pump. Just don't call Dispatcher.Run. You need to pump messages, and you need to call ComponentDispatcher.RaiseThreadMessage as appropriate. Use reflector to see what Dispatcher.PushFrame does.
Of course, this still may not give you what you want for a "game loop"...
Source: http://blogs.msdn.com/b/jaimer/archive/2009/06/29/wpf-discussions-090626.aspx
I think you want to look at HwndSource. It hosts a WPF GUI as a win32 control. Its all a bit tricky though and you're probably better off writing a plain WPF .net GUI app and using it to drive your native code using some form of interop. MS really didn't think of this kind of migration approach (reusing your existing code and slapping a new GUI on top) preferring to make us all rewrite all our code.
There are plenty of examples around the web. Good luck.
PS.
of course you could try the current 'gold standard' in native GUIs - Qt.
I am working on a large scale project where a custom (pretty good and robust) framework has been provided and we have to use that for showing up forms and views.
There is abstract class StrategyEditor (derived from some class in framework) which is instantiated whenever a new StrategyForm is opened.
StrategyForm (a customized window frame) contains StrategyEditor.
StrategyEditor contains StrategyTab.
StrategyTab contains StrategyCanvas.
This is a small portion of the big classes to clarify that there are many objects that will be created if one StrategyForm object is allocated in memory at run-time. My component owns all these classes mentioned above except StrategyForm whose code is not in my control.
Now, at run-time, user opens up many strategy objects (which trigger creation of new StrategyForm object.) After creating approx. 44 strategy objects, we see that the USER OBJECT HANDLES (I'll use UOH from here onwards) created by the application reaches to about 20k+, while in registry the default amount for handles is 10k. Read more about User Objects here. Testing on different machines made it clear that the number of strategy objects opened is different for message to pop-up - on one m/c if it is 44, then it can be 40 on another.
When we see the message pop-up, it means that the application is going to respond slowly. It gets worse with few more objects and then creation of window frames and subsequent objects fail.
We first thought that it was not-enough-memory issue. But then reading more about new in C# helped in understanding that an exception would be thrown if app ran out of memory. This is not a memory issue then, I feel (task manager also showed 1.5GB+ available memory.)
M/C specs
Core 2 Duo 2GHz+
4GB RAM
80GB+ free disk space for page file
Virtual Memory set: 4000 - 6000
My questions
Q1. Does this look like a memory issue and I am wrong that it is not?
Q2. Does this point to exhaustion of free UOHs (as I'm thinking) and which is resulting in failure of creation of window handles?
Q3. How can we avoid loading up of an StrategyEditor object (beyond a threshold, keeping an eye on the current usage of UOHs)? (we already know how to fetch number of UOHs in use, so don't go there.) Keep in mind that the call to new StrategyForm() is outside the control of my component.
Q4. I am bit confused - what are Handles to user objects exactly? Is MSDN talking about any object that we create or only some specific objects like window handles, cursor handles, icon handles?
Q5. What exactly causes to use up a UOH? (almost same as Q4)
I would be really thankful to anyone who can give me some knowledgeable answers. Thanks much! :)
[Update]
Based on Stakx answer, please note that the windows that are being opened, will be closed by the user only. This is kind of MDI app situation where way too many children windows are opened. So, Dispose can not be called whenever we want.
Q1
Sounds like you're trying to create far too many UI controls at the same time. Even if there's memory left, you're running out of handles. See below for a brief, but fairly technical explanation.
Q4
I understand a user object to be any object that is part of the GUI. At least until Windows XP, the Windows UI API resided in USER.DLL (one of the core DLLs making up Windows). Basically, the UI is made up of "windows". All controls, such as buttons, textboxes, checkboxes, are internally the same thing, namely "windows". To create them, you'd call the Win32 API function CreateWindow. That function would then return a handle to the created "window" (UI element, or "user object").
So I assume that a user object handle is a handle as returned by this function. (Winforms is based on the old Win32 API and would therefore use the CreateWindow function.)
Q2
Indeed you cannot create as many UI controls as you want. All those handles retrieved through CreateWindow must at some point be freed. In Winforms, the easiest and safest way to do this is through the use of the using block or by calling Dispose:
using (MyForm form = new MyForm())
{
if (form.ShowDialog() == DialogResult.OK) ...
}
Basically, all System.Windows.Forms.Control can be Disposed, and should be disposed. Sometimes, that's done for you automatically, but you shouldn't rely on it. Always Dispose your UI controls when you no longer need them.
Note on Dispose for modal & modeless forms:
Modal forms (shown with ShowDialog) are not automatically disposed. You have to do that yourself, as demonstrated in the code example above.
Modeless forms (shown with Show) are automatically disposed for you, since you have no control over when it will be closed by the user. No need to explicitly call Dispose!
Q5
Everytime you create a UI object, Winforms internally makes calls to CreateWindow. That's how handles are allocated. And they're not freed until a corresponding call to DestroyWindow is made. In Winforms, that call is triggered through the Dispose method of any System.Windows.Forms.Control. (Note: While I'm farily certain about this, I'm actually guessing a little. I may not be 100% correct. Having a look at Winforms internals using Reflector would reveal the truth.)
Q3
Assuming that your StrategyEditor creates a massive bunch of UI controls, I don't think you can do a lot. If you can't simplify that control (with respect to the number of child controls it creates), then it seems you're stuck in the situation where you are. You simply can't create infinitely many UI controls.
You could, however, keep track of how many StrategyEditors are opened at any one time (increase a counter whenever one is instantiated, and decrease it whenever one is closed -- you can track the latter using the FormClosing/FormClosed event of a form, or in the Dispose method of a control). Then you could limit the number of simultaneously opened StrategyEditors to a fixed number, say 5. If the limit is exceeded, you could throw an exception in the constructor, so that no more instances are created. Of course I can't say whether StrategyForm is going to handle an exception from your StrategyEditor constructor well...
public class StrategyEditor : ...
{
public StrategyEditor()
{
InitializeComponent();
if (numberOfLiveInstances >= maximumAllowedLiveInstances)
throw ...;
// not a nice solution IMHO, but if you've no other choice...
}
}
In either case, limiting the number of instantiated StrategyEditors seems like a temporary fix to me and won't solve the real problem.