473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

analyze a thread

Hi, could somebody analyze a class and suggest what to do to avoid
hanging the class (thread) when the application calling it exits? (Other
than using background threads). This class is something similar to a
splash screen. Thanks
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.IO;

namespace CF.GUI {

/// <summary>
/// Displays a "connecting" window with animation of signal sent
between two computers.
/// The window is shown only if a second has passed since it was called.
/// </summary>
/// <remarks>
/// Warning! Disables the parent form, if Hide() isn't called the
parent form will be frozen!
/// Always use try-finally block.</remarks>
/// <example>
/// ConnectionForm f = null;
/// try {
/// f = new ConnectionForm(this);
/// f.Show();
/// // do something in the main thread
/// } finally {
/// f.Hide();
/// f.Dispose();
/// f = null;
/// }
/// </example>
public class ConnectionForm : Form, IDisposable {

private Size size;
private Point computer1;
private Point computer2;
private Point computer1Attach;
private Point computer2Attach;
private Point textLocation;
private Rectangle invRect;

private bool modal;
/// <summary>
/// position of "signal" in the network
/// </summary>
private int signalPosition;
/// <summary>
/// length of the "signal"
/// </summary>
private const int signalLength = 10;
/// <summary>
/// in which direction does the "signal" travel
/// </summary>
private bool signalDirectionRight = true;
private Form owner = null;
private Icon icon;
private Thread threadShow;
private Thread threadAnimate;

private string caption;

public ConnectionForm(Form owner) {

try {

this.owner = owner;
this.owner = null;

this.size = new Size(200, 60);
this.computer1 = new Point(8, 15);
this.computer1Attach = new Point(28, 30);
this.computer2 = new Point(160, 15);
this.computer2Attach = new Point(180, 30);
this.textLocation = new Point(70, 4);
this.invRect = new Rectangle(computer1Attach.X, computer1Attach.Y -
1, computer2Attach.X, computer2Attach.Y + 1);

this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.White;
this.Cursor = Cursors.WaitCursor;
this.ControlBox = false;
this.HelpButton = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowInTaskbar = false;
this.Size = size;
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "Scania CF System";
this.TopLevel = true;
this.TopMost = true;
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer |
ControlStyles.Opaque |
ControlStyles.UserPaint,
true);

caption = "Connecting...";
ICFModuleClient cf = CF.CFModuleSingleton.CFClient;
if (cf != null)
caption = cf.Captions.get("CF.GUI.ConnectingForm:Caption");

Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream resourceStream =
executingAssembly.GetManifestResourceStream( "CF.GUI.images.computer.ico" );
icon = new Icon(resourceStream);
} catch {}
}
~ConnectionForm() {
if (owner != null) {
try {
owner.Enabled = true;
} catch { }
owner = null;
}
}
// TODO: override or what?
public void Dispose() {
if (owner != null) {
try {
owner.Enabled = true;
} catch { }
owner = null;
}
base.Dispose(true);
}
/// <summary>
/// Displays the form. Starts new thread, so doesn't block
/// </summary>
public new void Show() {
Show(true);
}
/// <summary>
/// Displays the form. Starts new thread, so doesn't block
/// </summary>
public void Show(bool modal) {
this.modal = modal;
if (modal) owner = null;

if (owner != null) {
try {
owner.Enabled = false;
} catch { }
}

try {
threadShow = new Thread(new ThreadStart(threadShowRun));
threadShow.Start();

threadAnimate = new Thread(new ThreadStart(threadAnimateRun));
threadAnimate.Start();
} catch { }
}
/// <summary>
/// Closes the form
/// </summary>
public new void Hide() {
// TODO: invoke Hide()? It's called from an other thread I suppose?
Or not?
if (owner != null) {
try {
owner.Enabled = true;
} catch { }
owner = null;
}

int trials = 50;
bool success = false;
while (!false && (trials-- > 0) ) {
try {
this.Close();
success = true;
} catch {
Console.WriteLine("ERROR1");
}
if (!success) {
Thread.Sleep(20);
}
}
try {
threadAnimate.Abort();
threadShow.Abort();
this.Dispose();
} catch (Exception e) {
Console.WriteLine ("ERROR2" + e.Message);
}
}

private void threadShowRun() {
try {
// do not show window if time is less than one second
Thread.Sleep(1000);
if (modal)
this.ShowDialog();
else
this.Show();
} catch { }
}
private void threadAnimateRun() {
try {
signalPosition = computer1Attach.X;
signalDirectionRight = true;
while (true) {
if (signalDirectionRight) {

// send "signal"
if (signalPosition < computer2Attach.X) {
signalPosition += 6;
} else {
signalDirectionRight = false;
}

} else {

// send "signal"
if (signalPosition > computer1Attach.X) {
signalPosition -= 6;
} else {
signalDirectionRight = true;
}

}

Invoke(new MethodInvoker(Invalidate), new object[] {invRect});
Thread.Sleep(100);
}
} catch { }
}
private Bitmap backBuffer;

protected override void OnPaint(PaintEventArgs e) {
try {
if (backBuffer == null) {
backBuffer = new Bitmap(size.Width, size.Height);
}
Graphics g = Graphics.FromImage(backBuffer);

g.Clear(Color.White);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

// draw border
Pen p = new Pen(Color.Black, 1);
g.DrawRectangle(p, 0, 0, size.Width - 1, size.Height - 1);

// draw text
g.DrawString(caption, new Font("Verdana", 7), Brushes.Black,
textLocation);

// draw animation
Pen p1 = new Pen(Color.Black, 3);
Pen p2 = new Pen(Color.White, 1);
g.DrawLine(p1, computer1Attach, computer2Attach);
int y = computer1Attach.Y;
g.DrawLine(p2, signalPosition, y, signalPosition - signalLength, y);
g.DrawIcon(icon, computer1.X, computer1.Y);
g.DrawIcon(icon, computer2.X, computer2.Y);

g.Dispose();
e.Graphics.DrawImageUnscaled(backBuffer, 0, 0);
} catch { }
}
protected override void OnPaintBackground(PaintEventArgs e) {
// don't allow the background to paint
}
}
}
Feb 9 '06 #1
0 1058

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Holger Marzen | last post by:
Hi all, the docs are not clear for me. If I want (in version 7.1.x, 7.2.x) to help the analyzer AND free unused space do I have to do a vacuum vacuum analyze or is a
3
by: Harry Broomhall | last post by:
I asked earlier about ways of doing an UPDATE involving a left outer join and got some very useful feedback. This has thrown up a (to me) strange anomaly about the speed of such an update. ...
3
by: Joseph Shraibman | last post by:
Trying this: VACUUM VERBOSE ANALYZE; on a 7.4.1 database only does a vacuum, not the analyze. I've tried this on two seperate databases. Is this a known bug? I haven't seen anything about...
3
by: user_5701 | last post by:
Hello, I have an Access 2000 database that I need to export certain queries to Excel 2000. The problem is that I have to take the toolbars away from the users for security purposes, but still let...
5
by: Sweety | last post by:
Plz i need full explanation ? #include<stdio.h> int main() { for(;0;) printf("hello") ; return 0 ; }
0
by: Rajesh Kumar Mallah | last post by:
Greeting, Will it be an useful feature to be able to vacumm / analyze all tables in a given schema. eg VACUUM schema.* ; at least for me it will be a good feature.
5
by: Jon Lapham | last post by:
I have been using the EXPLAIN ANALYZE command to debug some performance bottlenecks in my database. In doing so, I have found an oddity (to me anyway). The "19ms" total runtime reported below...
16
by: Ed L. | last post by:
I'm getting a slew of these repeatable errors when running ANALYZE and/or VACUUM ANALYZE (from an autovacuum process) against a 7.3.4 cluster on HP-UX B.11.00: 2004-09-29 18:14:53.621 ERROR:...
6
by: dspfun | last post by:
I would like to analyze my running c-program. What I would like to know for example is the range of the entire address space of my running c-program (memory reserved for/by the running program),...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.