Hi all,
I am coding for displaying a splash screen until a big job is
finished.I am getting an exception:only during the first start of the
system.Rest all the time
it is working fine.
Only when i restart the PC my application will be launced by windows
services at startup and i get the exception saying:
"Object reference not set to an instance of an object" please see
Problem:- to see where i am struck
And the splash screen once stated displaying it will not close even
though the big process has completed.i have to stop and restart the
service and then the
splash screen works fine.
Algorithm:
----------
StartSplash(no interaction by the user only function calls)
//long process which may take from 5 secs upto 2 min upto that time
splash screen will be displayed
stopsplash(no interaction by the user only function calls)
//file splash.cs
namespace rag
{
class splash
{
//Variables and handles
Thread th = null;
Splashevt SplashOpr = null;
// events used to stop worker thread
ManualResetEvent m_EventThreadStopped;
//Class constructor
public splash()
{
// initialize events for thread
m_EventThreadStopped = new ManualResetEvent(false);
}
public void StartSplash(String str)
{
//start a new thread to start the splash
th = new Thread(new ThreadStart(DoSplash));
th.Priority = ThreadPriority.Lowest;
th.IsBackground=true;
//start the thread
th.Start();
}
/// <summary>
/// Thread function to display the splash screen
/// </summary>
private void DoSplash()
{
int start = 1;
//send the controller number
SplashOpr = new Splashevt();
//Start the splash screen
SplashOpr.Run(start);
}
/// <summary>
/// To Stop the Flash Screen
/// </summary>
public void StopSplash()
{
int Stop = 2;
while (th.IsAlive)
{
Trace.Write("Inside While\n");
m_EventThreadStopped.Set();
if (WaitHandle.WaitAll(
(new ManualResetEvent[]
{ m_EventThreadStopped }),
100,
true))
{
Trace.Write("Inside thread event\n");
SplashOpr.Run(Stop);
break;
}
//process the messages
Application.DoEvents();
}
}
}
}
//file splashevt.cs
namespace rag
{
delegate void CloseSplashScreenDelegate();
/// <summary>
/// To stop the thread call the function of class Longprocess
/// </summary>
public class Splashevt
{
#region Members
Splash sp = null;
#endregion
#region Functions
public Splashevt()
{
}
public void Run(int splashstate)
{
//Start the Splash Screen
if (1 == splashstate)
{
sp = new Splash();
//Display the splash screen
sp.ShowDialog();
}
//Stop the splash screen
else if (2 == splashstate)
{
try
{
if (sp != null)
{
sp.Invoke(new
CloseSplashScreenDelegate(CloseSplashScreen));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Errorinstopsplash",
MessageBoxButtons.OK);
//problem:- First time i start the PC since my
application will be launched as a windows service
i get an exception saying :- "object reference not
set to an instance of an object".It is because sp is null
But iam calling everytime startsplash first then
stopsplash then why is this sp not having any vaue??
Once i stop the service and restart it again.it works
fine.only during the start of the PC it is null.
//can anyone please help on this thnx in advance---
raghu
}
}
}
private void CloseSplashScreen()
{
sp.Close();
}
#endregion
}
}