Close modal dialog from external thread - C# - c#

I am struggling to find a way to create the Forms functionality that I want using C#.
Basically, I want to have a modal dialog box that has a specified timeout period. It seems like this should be easy to do, but I can't seem to get it to work.
Once I call this.ShowDialog(parent), the program flow stops, and I have no way of closing the dialog without the user first clicking a button.
I tried creating a new thread using the BackgroundWorker class, but I can't get it to close the dialog on a different thread.
Am I missing something obvious here?
Thanks for any insight you can provide.

You will need to call the Close method on the thread that created the form:
theDialogForm.BeginInvoke(new MethodInvoker(Close));

Use a System.Windows.Forms.Timer. Set its Interval property to be your timeout and its Tick event handler to close the dialog.
partial class TimedModalForm : Form
{
private Timer timer;
public TimedModalForm()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 3000;
timer.Tick += CloseForm;
timer.Start();
}
private void CloseForm(object sender, EventArgs e)
{
timer.Stop();
timer.Dispose();
this.DialogResult = DialogResult.OK;
}
}
The timer runs on the UI thread so it is safe to close the form from the tick event handler.

If you really just want a modal dialog then I found this to be the best solution by far: http://www.codeproject.com/KB/miscctrl/CsMsgBoxTimeOut.aspx (read the comments section for a small modification).
If you want to display your own form modally, then the solution from adrianbanks is best.

you can Invoke the close from your background thread

Related

How can I prevent duplicate items on view initalization for c# mvvm type application?

I'm making an application that consists of a blank main view and 2 user controls, main view is only used as a container to switch between the 2 user controls by using MVVM Light messenger to tell the main view to switch which user control is displayed.
On startup I have the UserControl1 displaying. I have a button that will show UserControl2. I set up a timer in the Xaml codebehind for UserControl2 that will be used as an inactivity timer to switch back to UserControl1 if inactive.
It is now evident that I am not cleaning up my data properly because after the timer_tick and moving back to UserControl1, I see my timer is still active and ticking. On top of that, when I navigate back to UserControl2, another timer is created and this keeps happening every time I move to this view.
I'm new to c# app development and mvvm, so I'm still trying to figure out how to clean and dispose of my data, so I'm not sure what a standard practice would be.
Could anybody suggest the proper way to ensure I'm not leaking memory for my views and viewmodels?
Edit:
Here is a section of my code behind that I am currently using
public partial class AddQualityRegisterView : UserControl
{
DispatcherTimer timer = new DispatcherTimer();
public AddQualityRegisterView()
{
InitializeComponent();
Debug.WriteLine("AddQualityRegisterView Initialized");
timer.Interval = TimeSpan.FromSeconds(15);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
Debug.WriteLine("Timer Ticked!!!!!!!!!!!!!!!");
var vm = (AddQualityRegisterViewModel)DataContext;
timer.Stop();
Debug.WriteLine("Timer Stopped");
vm.ExitCommand.Execute(null);
}
If you're using the System.Timers.Timer component, it implements IDisposable. In that case you'd want something like:
using(var timer = new System.Timers.Timer())
{
// Do your stuff here
}
Then whenever you exit that using block, the timer will be disposed of cleanly.
It looks like you are creating a new instance of UserControl2 view everytime you click the "button" to show it - this is not a best practice as far as MVVM is concerned (Though, this is a different story). Given your existing code, I believe you need to remove the handler to properly deal with the timer.
You can remove the handler by:
timer.Tick -= timer_Tick;

Blocking User Process Controller from executing before Control on Winform Fully Loaded and Shown

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.

c# chat window application problem with timer

I'm developing a chat application. For getting frequetly comming request,messages and zone request I'm using one timer and call all methods on timer.now. The problem is that when ever I click on any control in the application this gives me a late response due to the timer running. It first hangs until it completes the timer code then control click event is fire.
So, any help on how to handle this is appreciated, I also tried threading but this didn't help.
Please give me any idea if u have.
Thanks.
Use System.Timers.Timer or System.Threading.Timer instead of the Windows.Windows.Forms.Timer, and inside the Elapced event handler whenever you call methods or properties on UI controls use control.InvokeRequired and control.Invoke.
the problem with the form timer is that it perform the action on UI thread, From msdn:
Windows timer is designed for a single-threaded environment where UI
threads are used to perform processing
Edit: Here is example using System.Timers.Timer:
private System.Timers.Timer _chatTimer = new System.Timers.Timer();
public Form1()
{
InitializeComponents();
_chatTimer.Interval = 1000;//1 seconds
_chatTimer.Elapsed += OnChatTimerElapsed;
_chatTimer.AutoReset = true;
}
private void OnChatTimerElapsed(object sender, System.Timer.ElapsedEventArts e)
{
//code to perform when timer elapsed.
}
Edit2: Another thing to notice that depending on execution time on the elapsed event handler, if the time required to execute the code on it is larger than 1 second then I suggest you to set _chatTimer.AutoReset to false and only start the timer after the previous elapsed event is finished. for example check this.

VB.net forms application... what event to use when loading initial data

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.

Why is this C# timer code not working?

I need to be able to disable a button for 1.5 seconds at a time for an application I'm writing. An image is displayed, a user clicks a button, and then another image is displayed. I need to make sure that the user doesn't click the button again too quickly.
So, when the image is displayed, I call this function:
//when a new image is displayed, start the timer and disable the 'done' button
//for 1.5 seconds, to force people to stop pressing next so quickly
System.Timers.Timer mTimer;
void TimerStart() {
Done.IsEnabled = false;
mTimer = new System.Timers.Timer();
mTimer.Interval = 1500;
mTimer.Start();
mTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerEnd);
}
The TimerEnd code looks like:
void TimerEnd(object sender, EventArgs eArgs) {
if (sender == mTimer){
Done.IsEnabled = true;
mTimer.Stop();
}
}
The 'Done.IsEnabled' line gets hit, but the button is not reenabled and the timer doesn't stop firing. What am I doing wrong here? If it matters, this is a WPF app.
Use DispatcherTimer instead
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(someInterval);
timer.Tick += new EventHandler(someEventHandler);
timer.Start();
private void someEventHandler(Object sender, EventArgs args)
{
//some operations
//if you want this event handler executed for just once
// DispatcherTimer thisTimer = (DispatcherTimer)sender;
// thisTimer.Stop();
}
Basically you are trying to debounce the button, to prevent too quick clicks. Rather than use a timer save the previous click time in millis, if the button is clicked again within a short time ignore the next event.
The timer event is raised on a different thread. When working with the winforms controls, you need to make sure you Invoke them from the same thread where they were called.
When working with WPF there is no guarantee that updates made to UI controls on non-UI threads will work as expected. In many cases you will get an exception when you do this.
In your Timer elapsed handler you need to use the BeginInvoke/EndInvoke paradigm and put your button enabling logic in there to ensure that this code runs on the UI thread instead of Begin/End Invoke
There is a SynchnornizationContext available as well which can be accessed by calling SynchronizationContext.Current . You'll need to cache this before you make the timer call since SynchronizationContext.Current will be null in non-UI threads.
This link talks about this as well.

Categories