enableVisualstyles -> Crash

  • Thread starter Thread starter mphanke
  • Start date Start date
M

mphanke

Hi,

everytime I enable the Visualstyles on in my app it keeps crashing at a
point, where I close my progress window with progressdialog.close(). It
runs in a seperate thread and contains a timer and a progressbar- if
this is of interest.

Best regards,

Martin
 
mphanke said:
Hi,

everytime I enable the Visualstyles on in my app it keeps crashing at a
point, where I close my progress window with progressdialog.close(). It
runs in a seperate thread and contains a timer and a progressbar- if this
is of interest.

After you enable it do: Application.DoEvents();

FB
 
Hi,

Thanks for the hint, but didn't help!

So my Main() looks like :

static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new Form1());
}

The func which starts the thread and displays the progress bar looks like:

Thread g_Thread;

private void ShowProgressBar()
{
DataProgress dp = new DataProgress(true); // true->start the timer
immediately
try
{
dp.ShowDialog();
dp.Activate();
}
catch(ThreadAbortException abortException)
{
dp.StopTimer(); // stop the timer
dp.SetTitle((string)abortException.ExceptionState); // set this to be
the final message
dp.Close(); // close -> and crash if visualstyles enabled ->
WindowsFormsParkingWindow
}
}

private void StartStopThread(bool b, string str)
{
if(b)
{
g_Thread = new Thread(new ThreadStart(ShowProgressBar));
g_Thread.Start();
Thread.Sleep(250);
}
else
{
if(g_Thread.IsAlive)
{
g_Thread.Abort(str);
Thread.Sleep(250);
}
}
}

Hope this makes things clearer now.

Martin
 
You did not provide a complete code sample so it's hard to tell what's
really going on.

I'd be surprised if the thread that the function ShowProgressBar executes on
behaves as you think it does. Just because you catch a ThreadAbortException
does not mean that you've handled it completely. After the catch block
completes the runtime will rethrow the exception again unless you call
ResetAbort. If you do not call ResetAbort then eventually that entire thread
of execution will terminate and you will see an unhandled exception - if
this was the main thread of execution then your app crashes.

Another potential problem is that you may be updating controls on threads
other then the thread on which the control was created.

In general, don't abort a thread unless you know what all the side effects
are. There are other and better ways to cause a thread to terminate in a
graceful manner. Also, controls should usually be updated on the same thread
that created them. You can use the Control.InvokeRequired field to test for
this.
 
Hi,

thanks, this was the advice I needed. The problem was I called
Application.DoEvents() inside the Tick-func of the Timer. This actually
caused the crash on the Close()-call.

Martin
 
Hi,

Thanks for the hint, but didn't help!

So my Main() looks like :

static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new Form1());
}

The func which starts the thread and displays the progress bar
looks like:

Thread g_Thread;

private void ShowProgressBar()
{
DataProgress dp = new DataProgress(true); // true->start
the timer
immediately
try
{
dp.ShowDialog();
dp.Activate();
}
catch(ThreadAbortException abortException)
{
dp.StopTimer(); // stop the timer
dp.SetTitle((string)abortException.ExceptionState); //
set this to be
the final message
dp.Close(); // close -> and crash if visualstyles
enabled ->
WindowsFormsParkingWindow
}
}

private void StartStopThread(bool b, string str)
{
if(b)
{
g_Thread = new Thread(new
ThreadStart(ShowProgressBar)); g_Thread.Start();
Thread.Sleep(250);
}
else
{
if(g_Thread.IsAlive)
{
g_Thread.Abort(str);
Thread.Sleep(250);
}
}
}

Martin,

Threading and WinForms can get tricky. You may need to spawn the new
thread with your form's BeginInvoke method rather than using the
Thread class. See this article for more info (it's the first in a
series of three):

http://msdn.microsoft.com/library/en-
us/dnforms/html/winforms06112002.asp

or

http://tinyurl.com/29sd
 
Back
Top