I want to set my wpf application to navigate to another page after 3 second using c#.
Is this possible? If yes then how?
You could either use a Timer and set the Tick to 3000 which would a fire a method, changing the page. Or you could start a new Thread, put a thread.sleep(3000) then call the page change. If you do the second method, you need to use Dispatcher.Invoke as you are not on the main UI thread.
Related
here is my thread in asp.net web page (code behind) :
Thread my_thread = new Thread(delegate()
{
my_thread_method(params);
});
Timer1.Enabled = true;
my_thread.Start();
Now I want to access Timer1 in my_thread() and work with that timer.
How can i do that?
I also need to update some labels text inside thread, but I don't have access to them.
What is the solution?
my_thread_method needs to be defined in the same class as the controls in order for you to access them.
You will then need to read this sort of thing to call UI classes correctly from the non-UI thread:
How to update the GUI from another thread in C#?
EDIT
this answer is not related to asp.net web forms.
so we can not accept this.
For update controls in Thread, you have to use invoke method.
Go to : C# Threading using invoke, freezing the form
In a WPF 4 app, I have a very big user control full of controls which takes something like 4s to initialize on a fast machine. During this time the application is of course not responsive at all.
Is there a way to show an animation in the main window while this control is initialized?
I understand that I cannot create it on another thread. But is there maybe a way to create it with a lower priority from the Dispatcher so that I could show a spinning wheel or so on the main window which would still spin?
(The only solution I can think of right now would be to break the user control into further pieces and load them only when needed. But this will take a lot of development time to change.)
Update1
To be more clear: It is a simple WPF window using tab pages. When a new tab page is opened I'm initializing the user control which holds the controls for this tab page. One of these user controls is so full of controls that it takes 4s until the new tab page will be shown.
So I thought showing a spinning wheel would be better than having a blocked application.
I think you are going to have to break this user control up into pieces. What you can do is use a BackgroundWorker to coordinate the 'building' of this user control. Each time the DoWork event is fired, use Dispatcher.BeginInvoke to create an add the next control to your UI. This technique is described in the following blog post:
http://loekvandenouweland.com/index.php/2010/12/wp7-add-user-controls-graphics-in-background-thread/
This will allow you to show an animation during loading.
Why can't you initialize it on another thread? I see two scenarios:
Initialization is slow for non-WPF reasons that be preloaded/precomputed on another thread before entering the core WPF initialization.
WPF itself is consuming 4 seconds of CPU time (though, that's really WTF levels of CPU time...). If so, you can start another STA thread with it's own message pump that can display an independant UI (e.g: a spinning wheel) until the primary thread is done loading.
You can create "dialogs" which implicitly create a new Dispatcher and run on a background thread, or you can explicitly create your own Dispatcher (=message pump).
I use the following method to do so:
public static Dispatcher StartNewDispatcher(ThreadPriority cpuPriority = ThreadPriority.Normal) {
using (var sem = new SemaphoreSlim(0)) {
Dispatcher retval = null;
var winThread = new Thread(() => {
retval = Dispatcher.CurrentDispatcher;
sem.Release();
Dispatcher.Run();
}) { IsBackground = true, Priority = cpuPriority };
winThread.SetApartmentState(ApartmentState.STA);
winThread.Start();
sem.Wait();
return retval;
}
}
This gives you real multi-threaded UI; but it also means you can't databind or in any other way directly communicate between the two UI's: after all, WPF objects have thread-affinity.
Before you go this route, verify there aren't any slow components you can preload using a profiler: option 1 (preload heavy stuff before WPF init) is simpler and cleaner.
A solution could be to move the slow part (loading data? that doesn't belong IN the control) to another thread as you mentioned.
Or use a VirtualizingStackPanel to lazy load what is needed.
Could you elaborate on the reason for the delay?
I have a very simple StopWatch application in Silverlight. I have the following private properties in my MainPage class: _StopPressed (bool), _TimeStart, _Elapsed (string). I also have a "Start" and "Stop" button.
The "Start" button calls a method called UpdateTime that constantly updates _ElapsedTime until _StopPressed is true. When I originally wrote it, UpdateTime would block the UI so I couldn't press the Stop button, so I updated my code to use System.Threading.ThreadPool.QueueUserWorkItem with my UpdateTime method so that it updates _Elapsed on a background thread. That works great at updating the value.
However, if I try to set the .Text value of my TextBlock from within UpdateTime(), I get an UnauthorizedAccessException that has to do with one thread accessing the data in another thread.
What do I need to do to avoid getting this exception and appropriately update the UI without blocking it?
Use a DispatcherTimer instead of a timer, works nearly exactly the same but is used for updating UI elements.
I am needing to create something like a lock timer(a little thing that just updates a lock time in a database). I thought the Timer control would suite my needs, but whenever the Timer control causes a partial post back, recently typed text in a textbox can disappear(inbetween the post back begin and post back end) and it loses focus.
Because this is only a lock timer, I do not need to refresh any part of the screen, I basically just need to tell the server "hey, don't free my lock, I'm still on this page". So is a Timer control even necessary? Is there an easier way to do this is pure javascript? The only thing I need to know is an ID, which could be kept as a hidden field(and therefore accessible from javascript by DOM)
anyone have any input on how to tune the timer control or a quick javascript way to do it?
edit:
also, I have the updatepanel that contains the timer control outside of the update panel containing the textbox control
If I understand it correctly you need a method which will update the datetime in the database at periodic intervals.
For that you can simply use Ajax. The window.setInterval is a JavaScript function which will fire a piece of code at regular intervals.
window.setInterval(foo,5000);
The above code fires the foo method every 5 seconds.
The only thing you need to lookup is how to call the database. Since, you are already using MS Ajax I suggest you check out ScriptManager control which contains a section for services. Check out the following post which consists of a simple example of how to call WebService methods using MS Ajax:
http://azamsharp.com/Posts/83_Using_FireBug_Profiler_to_Dig_Deep_into_MS_AJAX_and_JQuery_API.aspx
Did some searches here & on the 'net and haven't found a good answer yet. What I'm trying to do is call a button twice within the same class in C#.
Here's my scenario -
I have a form with a button that says "Go". When I click it the 1st time, it runs through some 'for' loops (non-stop) to display a color range. At the same time I set the button1.Text properties to "Stop". I would like to be able to click the button a 2nd time and when that happens I would like the program to stop. Basically a stop-and-go button. I know how to do it with 2 button events, but would like to utilize 1 button.
Right now the only way to end the program is the X button on the form.
I've tried different things and haven't had much luck so far so wanted to ask the gurus here how to do it.
BTW, this is a modification of a Head First Labs C# book exercise.
Thanks!
~Allen
You would need to use Multithreading (launch the process intensive code asynchronously in a separate thread), for instance, using the BackgroundWorker object in .NET 2+. This would be necessary because your UI will not respond to the user's click until the loop running in the Start method is completed. It is quite irrelevant if you use the same button or another one to toggle the process, because the processor is busy processing the loop.
The BackgroundWorker has a property called WorkerSupportsCancellation which needs to be true in this scenario. When the user clicks Stop you would invoke the CancelAsync method of the BackgroundWorker.
See MSDN for a good example. Also DreamInCode has a good tutorial which seems quite similar to your requirement.
Why not create two buttons, hide one when the other is visible? That should be a lot of easier to handle.
Or you can add a bool field to indicate which operation branch to execute.
One simple solution would be to add a boolean member to your form that is, e.g., true when the button says "Go" and false when the button says "Stop".
Then, in your button's event handler, check that boolean value. If the value is true, then start your operation and set the value to false when you change the button's text to say "stop". Vice-versa for the other case. :)
There are other techniques that I might prefer if this were production code, perhaps including considering the design of the form more carefully, but as this is clearly a learning exercise I believe that a simple boolean flag indicating the current state of the form is just what you're looking for.
Note that I would strongly discourage you from checking the value of the button text to determine what state the object is in. Whenever possible, as a general rule of good design, you want your visual state to be "decoupled" from your underlying object's state. That is to say, your visual widgets can depend on your underlying objects, but your underlying objects should not depend on your visual widgets. If you tested the text of the button, your underlying logic would depend on your visual state and that would violate this general rule.
If your problem is related to the fact that you can't cancel the operation while it's being performed, you'll want to look into using a BackgroundWorker to perform your long-running activity.
Another option would be to check the current text on your button to determine what to do:
void btnStartStop_Click(Object sender, EventArgs e)
{
if (btnStartStop.Text == "Go")
{
btnStartStop.Text = "Stop";
// Go code here
}
else
{
btnStartStop.Text = "Go";
// Stop code here
}
}
Are you getting your second button click event? Put a breakpoint in your click handler and run your code. When you click the second time, do you ever hit your breakpoint?
If your loop is running continuously, and it is in your button click handler, then your loop is running in the UI thread. You probably don't get to "see" the second button click until after the loop is completed. In addition to the branch code that you see above, try either inserting a DoEvents in your loop processing (this is a place where your loop will temporarly give up control so that messages can be processed). Or, (better) have a look at the backgroundworker class -- do most of your processing in a different thread, so that you UI can remain responsive to button clicks.
Cerebrus is right about using the Background Worker thread. However if you are doing a WPF app then it won't be able to update the UI directly. To get around this you can call Dispatcher.BeginInvoke on the main control/window.
Given code like:
Private Delegate Sub UpdateUIDelegate(<arguments>)
Private Sub CallUpdateUI(<arguments>)
control.Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Background, New UpdateUIDelegate(AddressOf UpdateUI), <arguments>)
End Sub
Private Sub UpdateUI(<arguments>)
'update the UI
End Sub
You can call CallUpdateUI from the Background Worker thread and it will get the main thread to perform UpdateUI.
You could set the Tag property on the button to a boolean indicating whether the next action should be "Stop" or "Go", and reset it each time you click the button. It's an Object property, though, so you'll have to cast it to bool when you read it.