Hello everybody! Here's the situation: I'm developing a simple application with the following structure:
- FormMain (startup 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, everytime that I need to call a time consuming method located on ComplexFunctions I do the following:
// This is FormMain.cs
string strSomeParameter = "lala";
DoItInNewThread(new ParameterizedThreadStart(ComplexFunctions.DoSomething), strSomeParameter, ThreadPriority.Normal);
The other class, FormNotification, its a Form that display some information of the process to the user. This FormNotification could 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 closes the form. I'm not using formNotif.ShowDialog because I don't want to give focus to this Form. You could check this link to see what I'm doing in Notify.
Ok, here's the problem: When I call FormNotify from ComplexFunction which is called from another Thread in FormMain ... this FormNotify disappears after a few milliseconds. It's the same effect that when you do something like this:
using(FormSomething formSomething = new FormSomething)
{
formSomething.Show();
}
How can avoid this?
These are possible solutions that I don't 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 stay for 10 seconds, but they are irrelevant to see the problem).
Thanks for your time!!! And please, sorry my english.