I have a Button on Page 1 and on Button_click Doing some operations with a background worker and showing the progress and once the operation is finished , application
loads a new page like this
Uri ViewPageUri = new System.Uri("View.xaml", UriKind.Relative);
private void bookOpeningWorker_WorkFinished(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
{
NavigationService.Navigate(ViewPageUri);
}
}
Inside View Page Loaded event doing some other time consuming operations in a Background worker and showing the waiitng progress.
The second page contains few user controls and some other browser controls , but all the operations are performing on Load event of the page
All things are working fine , but the only issue is application is taking some time to finish execution between the lines NavigationService.Navigate and Loaded event of the Page
So i am getting a delay of 3-4 seconds betweeen this time and no progress or waiting messages are visible to end user and also i can see the pointer in a waiting mode .
Is there any specific reason for this effect?or any better way to improve the code?
Related
I'm creating a Excel VSTO using c#. My operation is easy, just right-click on the cell and click on "Update" and a winform that shows progress status will prompt out and launch the controls on the form is tied to a User Process Controller.
The problem is now that the process has launched and executed before the form is fully load, is there a way that I can block the user process controller from executing before all the control on the progress status form is fully shown and loaded? The image below depict my condition.
I have tried to put my User Process Controller call in Form Activated, Shown, Loaded, and nothing works.
This is the first stage the form loaded. Note : The two line of text has shown that the user control process has been executed.
This is the second stage the form loaded.
This is third stage
And finally it is fully loaded.
I have discovered a "hack" to overcome this issue. I add in a backgroundworker and done the follow code on the Form Constructor.
public SummaryStatus()
{
InitializeComponent();
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
backgroundWorker.RunWorkerAsync();
}
And on the DoWork event of BackgroundWorker
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(2000);
}
and finally, I added the following code in Run Worker Completed
void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
UpdateUPC upc = new UpdateUPC();
upc.txtUpdateSummary = txtUpdateProgress;
upc.updateProgressBar = UpdateProgressBar;
upc.UpdateStatusOnItem();
}
I understand the above might not be a acceptable solution but it might just provide a workaround for the issue. However, if anyone who has a better solution, do feel free to drop in your suggestion.
I have an issue very similar to DataGridView refresh causes "Not Responding" application (but only when not in Visual Studio)
While loading my data to my Winforms app form, I temporary disable a panel filling the form and containing all the controls, and I enable it back when done.
As in the example, I work with BackgroundWorker to load data and use thread-safe bgWorkerSend_ProgressChanged or bgWorkerSend_RunWorkerCompleted to interact with the UI.
I also have a Windows Forms timer that refreshes in the same way the form after 5 minutes of idle time.
What is weird, is that at the end of the init load, the panel is enabled back correctly, while in the latter refresh, the application enters "Not Responding" mode, and remains there forever. Furthermore, this only happens when I execute directly the application. If executed from Visual Studio, it works correctly both at initial load and at refresh.
private void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage == 100)
{
// re-enable panel
panel1.Enabled = true;
// the below is never reached !!?
AnyInstruction();
}
}
Narrowing down the issue, it showed that the problem was because the code inside BackgroundWorker thread was filling a DataSet that has been instantiated earlier in the main thread.
By moving the instantiation into the BackgroundWorker thread instead, resolved the issue, and panel1.Enabled = true; is now executed correctly in _bw_ProgressChanged.
I have a web application that generates enlarged images of smaller images uploaded by other team and stops if no image is left in uploaded folder.
A button needs to be clicked to start generating image and restart every time it finishes.
Can this click event be fired automatically at regular interval say at 15 minutes or better still just as the application stops.
A similar question was asked on earlier thread How to write C# Scheduler .
private void OnTimerTick()
{
if (DateTime.Now.Minutes % 15 == 0)
{
// Do something
}
}
Where should I place timer to call below code ?
protected void btnImgGenerate_click()
{
// Application code
}
Is there a way to check whether a web application in asp.net has stopped executing and then restart it automatically?
Using timer, I can schedule application to start and stop at specific time of day and keep application running throughout specified duration of day using second method.
You can add code to initialize timer from the System.Threading namespace in Global.asax file and configure it to execute some functionality with desired periodicy. But if your application crashes you'll get following issue:
Is there a way to check whether a web application in asp.net has
stopped executing and then restart it automatically ?
Using timer , I can schedule application to start and stop at specific
time of day and keep application running throughout specified duration
of day using second method .
Nope. You can't start web appliccation from itself if it stopped by some reasons and no new requests comes. So in my opinion for your purpose better suited a windows service.
A jQuery implementation for clicking a button after 15 minutes (which will cause a postback and trigger your event) is:
$(document).ready(function(){
setTimeout(function(){$('btnImgGenerate').click();},900000);
});
Have a Button click event that starts the timer-
protected void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
and in the timer Tick event, you can do something like this-
protected void timer1_Tick(object sender, EventArgs e)
{
timer1.enabled=false;
//do your coding
timer1.enabled= true;
}
So once you click the button the timer starts, does all the operations and at the end restarts automatically, after you set timer1.enabled to true.
If you are looking for automating this without the click event, you can use the javascript setInterval() Method-
See the example here-
http://www.w3schools.com/jsref/met_win_setinterval.asp
I am working on a WPF Browser Application. The problem is that I have to load some heavy services in the beginig, this causes to see a white page for some seconds before the page components get loaded. Is there a way to avoid this somehow by loading the page without waiting for the heavy processes to finish?
Here is the thing I am trying now, and it didn't work:
public Page1()
{
InitializeComponent();
}
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
// Initialize and configure kinect
// This takes some seconds
}
I was thinking the page_loaded event happens after all the components of the page have finished loading. But still it waits for the processes to finish and I get some seconds of a white page in the begining...
You can use BackgroundWorkers to handle the long-running operations. This will keep the UI responsive while the task(s) are running. Go here to learn more: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Could use BackGroundWorker and could call it in either event.
You cannot build or load a UI control in the background.
Build the data and (non UI) objects in the background and then bind on callback.
BackgroundWorker Class
I'm writing a forms application. I'm adding a piece that allows you to double click on a row of a datagridview control to open a new form with more details. These additional details are pulled from a database, which takes a bit of time to finish.
If I run the DB query from the form's load event, the form doesn't show up until everything in the load event has completed, which can take several seconds.
I want the form to show up instantly when you double click, and all of the fields to be populated once the data is ready.
Is there an event I should be using other than Load?
The standard way to accomplish this is to use a background worker thread and disable the button until the worker thread completes. There is a complete event you can subscribe to on the background worker.
You should use threading. Kick off a thread to do the data retrieval in the form's load event. Introduction to threading
You should use a BackgroundWorker to load the data in a background thread without freezing the UI.
If you really want to load on the UI thread, you should handle the Shown event.
This is an c# example using BackgroundWorker as the other posts metioned that loads unit definitions from .xml an file and changes the status label when it finishes. I stuck in the form intializer, but maybe it is better to start it in an OnLoad() override.
public MainForm()
{
InitializeComponent();
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = false;
bw.WorkerSupportsCancellation = false;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
unitsToolStripLabel.Text = "Loading Units";
bw.RunWorkerAsync();
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
...
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
unitsToolStripLabel.Text = string.Format("{0} Units Loaded", Units.UnitLibrary.WorkingSet.Count);
unitsToolStripLabel.LinkBehavior = LinkBehavior.HoverUnderline;
unitsToolStripLabel.Click += new EventHandler(unitsToolStripLabel_Click);
}
Please explain a little more on why you do not want to use threading/backgroundworker?
Whilst the correct way to do this is the BackgroundWorker thread, a quick and dirty method is to start a timer on the Load event and get the data when the timer expires. Say 10ms is enough for the form to be painted, then you can disable the controls and set the cursor busy while you get the data. But this is still going to lock up the UI thread while the database is busy leading to repainting artifacts eg if part of the window is covered, and doesn't allow you to display progress using a progress bar.
You can let the load event finish, then start another method to pull data from your database. The initialization of the UI can be done after the form has completed loading, but make sure your UI controls are disabled while you're initializing them.