Here's the situation: I am developing a simple application with the following structure:
- FormMain (launch point)
- FormNotification
- CompleFunctions
Right?
Well, in FormMain I have the following function:
private void DoItInNewThread(ParameterizedThreadStart pParameterizedThreadStart, object pParameters, ThreadPriority pThreadPriority) { Thread oThread = new Thread(pParameterizedThreadStart); oThread.CurrentUICulture = Settings.Instance.Language; oThread.IsBackground = true; oThread.Priority = pThreadPriority; oThread.Name = "μRemote: Background operation"; oThread.Start(pParameters); }
So, every time I need to call a time method located on ComplexFunctions , I do the following:
// This is FormMain.cs string strSomeParameter = "lala"; DoItInNewThread(new ParameterizedThreadStart(ComplexFunctions.DoSomething), strSomeParameter, ThreadPriority.Normal);
Another class, FormNotification, is its Form, which displays some process information for the user. This FormNotification can be called from FormMain or ComplexFunctions. Example:
// This is ComplexFunctions.cs public void DoSomething(string pSomeParameter) { // Imagine some time consuming task FormNotification formNotif = new FormNotification(); formNotif.Notify(); }
FormNotify has a timer, so after 10 seconds it closes the form. I do not use formNotif.ShowDialog because I do not want to focus on this form. You can check this link to find out what I'm doing in Notify.
Ok, here is the problem: When I call FormNotify from ComplexFunction , which is called from another thread in FormMain ... this FormNotify disappears after a few milliseconds. This is the same as when you do something like this:
using(FormSomething formSomething = new FormSomething) { formSomething.Show(); }
How to avoid this?
These are possible solutions that I do not want to use:
- Using Thread.Sleep (10000) in FormNotify
- Using FormNotif.ShowDialog ()
This is a simplified scenario (FormNotify does some other fancy stuff that just stays on for 10 seconds, but they are not related to the problem).
Thank you for your time!!! And please excuse my English.