Thread won't start...

  • Thread starter Thread starter taylorjonl
  • Start date Start date
T

taylorjonl

I am completely baffled. I am writting a daemon application for my
work to save me some time. The application works fine at my home but
won't work right here at work. Basically I have a MainForm what has a
Start/Stop button that starts and stops the processing thread.

private void StartButton_Click(object sender, System.EventArgs e)
{
if( bStopSignal )
{
// disable controls that aren't valid when running
StartButton.Enabled = false;
Issue12CheckBox.Enabled = false;
Issue28CheckBox.Enabled = false;
Issue29CheckBox.Enabled = false;

// enable controls that are valid when running
StopButton.Enabled = true;

// start the processing thread
bStopSignal = false;
MessageBox.Show( "Starting Processing Thread" );
(pProcessingThread = new Thread( new ThreadStart( ThreadProc )
)).Start();
}
}


private void StopButton_Click(object sender, System.EventArgs e)
{
// enable controls that are valid when stopped
StartButton.Enabled = true;
Issue12CheckBox.Enabled = true;
Issue28CheckBox.Enabled = true;
Issue29CheckBox.Enabled = true;

// disable controls that aren't valid when stopped
StopButton.Enabled = false;

// stop the processing thread
bStopSignal = true;
if( pProcessingThread != null )
{
MessageBox.Show( "Stopping Processing Thread" );
pProcessingThread.Join();
pProcessingThread = null;
}
}



private void ThreadProc()
{
MessageBox.Show( "Please browse to the issue viewer base URL to
start" );

// temporary code because I am lazy
pWebBrowser.Show(); pWebBrowser.BringToFront();
pWebBrowser.DocumentComplete.Reset();
pWebBrowser.DocumentComplete.WaitOne();
string starturl = pWebBrowser.Document.url;

string member = null;
while( !bStopSignal )
{
}
}

The above are the 3 important functions, the ThreadProc has been
trimmed of the logic in the while block, otherwise no difference from
my code.

When I click the start button I get the message box stating it is
starting the thread but don't get the message from the ThreadProc
function. Stop gives a message that it is stopping. It isn't starting
the thread like it should.

I am wondering if anyone can give me any feedback on possible cause. I
am wondering if I don't have access rights on my computer to start a
thread? Is this possible, if so would the program execute without
informing me it can't start the thread?

Keep in mind I can't debug it here at work because I don't have a
debugger and can't install a debugger.
 
I am completely baffled. I am writting a daemon application for my
work to save me some time. The application works fine at my home but
won't work right here at work. Basically I have a MainForm what has a
Start/Stop button that starts and stops the processing thread.

Could you post a short but complete program that demonstrates the
problem?
You shouldn't need much to show it. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Note that your bStopSignal variable *must* be volatile (or a
synchronized property) for your code to be thread-safe.
See http://www.pobox.com/~skeet/csharp/threads/volatility.shtml for
more on that.

Also note that you shouldn't use UI components created in one thread on
another thread. See
http://www.pobox.com/~skeet/csharp/threads/winforms.shtml for more than
that. (My guess is that your web browser is a UI component.) That
probably isn't the problem here (if you aren't seeing the message box
from the start of the other thread) but it may well bite you as soon as
you get past this initial problem.

Jon
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Threading;

namespace Issue
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label MemberLabel;
internal System.Windows.Forms.MainMenu MainMenu;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.TextBox MemberTextBox;
private System.Windows.Forms.Button AddButton;
private System.Windows.Forms.ListBox QueuedListBox;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.NotifyIcon NotifyIcon;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button StartButton;
private System.Windows.Forms.Button StopButton;
private System.Windows.Forms.GroupBox IssueCloseGroup;
private System.Windows.Forms.CheckBox Issue12CheckBox;
private System.Windows.Forms.CheckBox Issue28CheckBox;
private System.Windows.Forms.CheckBox Issue29CheckBox;
private System.Windows.Forms.MenuItem ImportMenuItem;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem ExitMenuItem;
private System.Windows.Forms.OpenFileDialog ImportFileDialog;

// member processing variables
private Thread pProcessingThread;
private bool bStopSignal;
private Queue pMemberQueue;

public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

// setup member processing variables
bStopSignal = true;
pMemberQueue = new Queue();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


/// <summary>
/// </summary>
private void SyncLists()
{
QueuedListBox.Items.Clear();
object[] contents = new object[pMemberQueue.Count];
pMemberQueue.CopyTo( contents, 0 );
QueuedListBox.Items.AddRange( contents );
}


/// <summary>
/// </summary>
private void ThreadProc()
{
MessageBox.Show( "Please browse to the issue viewer base URL to
start" );

string member = null;
while( !bStopSignal )
{
// get the next member
lock( pMemberQueue )
{
if( pMemberQueue.Count < 1 )
{
Thread.Sleep( 100 );
continue;
}
member = (string)pMemberQueue.Dequeue();

// sync the QueuedListBox and pMemberQueue contencts
SyncLists();

///////////////////////////////////////////////////////////
// Busines logic was here
///////////////////////////////////////////////////////////
}
}
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(MainForm));
this.MemberLabel = new System.Windows.Forms.Label();
this.MainMenu = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.MemberTextBox = new System.Windows.Forms.TextBox();
this.AddButton = new System.Windows.Forms.Button();
this.QueuedListBox = new System.Windows.Forms.ListBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.NotifyIcon = new
System.Windows.Forms.NotifyIcon(this.components);
this.StartButton = new System.Windows.Forms.Button();
this.StopButton = new System.Windows.Forms.Button();
this.IssueCloseGroup = new System.Windows.Forms.GroupBox();
this.Issue29CheckBox = new System.Windows.Forms.CheckBox();
this.Issue28CheckBox = new System.Windows.Forms.CheckBox();
this.Issue12CheckBox = new System.Windows.Forms.CheckBox();
this.ImportMenuItem = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.ExitMenuItem = new System.Windows.Forms.MenuItem();
this.ImportFileDialog = new System.Windows.Forms.OpenFileDialog();
this.IssueCloseGroup.SuspendLayout();
this.SuspendLayout();
//
// MemberLabel
//
this.MemberLabel.Location = new System.Drawing.Point(8, 8);
this.MemberLabel.Name = "MemberLabel";
this.MemberLabel.Size = new System.Drawing.Size(48, 23);
this.MemberLabel.TabIndex = 0;
this.MemberLabel.Text = "Member";
//
// MainMenu
//
this.MainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{
this.menuItem1,
this.menuItem2,
this.menuItem3,
this.menuItem4});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.ImportMenuItem,
this.menuItem6,
this.ExitMenuItem});
this.menuItem1.Text = "&File";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "&Edit";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "&View";
//
// menuItem4
//
this.menuItem4.Index = 3;
this.menuItem4.Text = "&Help";
//
// MemberTextBox
//
this.MemberTextBox.Location = new System.Drawing.Point(56, 8);
this.MemberTextBox.Name = "MemberTextBox";
this.MemberTextBox.Size = new System.Drawing.Size(168, 20);
this.MemberTextBox.TabIndex = 1;
this.MemberTextBox.Text = "";
//
// AddButton
//
this.AddButton.Location = new System.Drawing.Point(232, 8);
this.AddButton.Name = "AddButton";
this.AddButton.Size = new System.Drawing.Size(50, 23);
this.AddButton.TabIndex = 2;
this.AddButton.Text = "Add";
this.AddButton.Click += new
System.EventHandler(this.AddButton_Click);
//
// QueuedListBox
//
this.QueuedListBox.Location = new System.Drawing.Point(8, 40);
this.QueuedListBox.Name = "QueuedListBox";
this.QueuedListBox.Size = new System.Drawing.Size(272, 108);
this.QueuedListBox.TabIndex = 3;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 245);
this.statusBar1.Name = "statusBar1";
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(288, 22);
this.statusBar1.TabIndex = 4;
this.statusBar1.Text = "statusBar1";
//
// NotifyIcon
//
// this.NotifyIcon.Icon =
((System.Drawing.Icon)(resources.GetObject("NotifyIcon.Icon")));
this.NotifyIcon.Text = "Issue Closing Daemon";
this.NotifyIcon.Visible = true;
this.NotifyIcon.DoubleClick += new
System.EventHandler(this.NotifyIcon_DoubleClick);
//
// StartButton
//
this.StartButton.Location = new System.Drawing.Point(120, 160);
this.StartButton.Name = "StartButton";
this.StartButton.Size = new System.Drawing.Size(160, 32);
this.StartButton.TabIndex = 5;
this.StartButton.Text = "Start";
this.StartButton.Click += new
System.EventHandler(this.StartButton_Click);
//
// StopButton
//
this.StopButton.Enabled = false;
this.StopButton.Location = new System.Drawing.Point(120, 208);
this.StopButton.Name = "StopButton";
this.StopButton.Size = new System.Drawing.Size(160, 32);
this.StopButton.TabIndex = 6;
this.StopButton.Text = "Stop";
this.StopButton.Click += new
System.EventHandler(this.StopButton_Click);
//
// IssueCloseGroup
//
this.IssueCloseGroup.Controls.Add(this.Issue29CheckBox);
this.IssueCloseGroup.Controls.Add(this.Issue28CheckBox);
this.IssueCloseGroup.Controls.Add(this.Issue12CheckBox);
this.IssueCloseGroup.Location = new System.Drawing.Point(8, 152);
this.IssueCloseGroup.Name = "IssueCloseGroup";
this.IssueCloseGroup.Size = new System.Drawing.Size(104, 88);
this.IssueCloseGroup.TabIndex = 7;
this.IssueCloseGroup.TabStop = false;
this.IssueCloseGroup.Text = "Issues to close";
//
// Issue29CheckBox
//
this.Issue29CheckBox.Checked = true;
this.Issue29CheckBox.CheckState =
System.Windows.Forms.CheckState.Checked;
this.Issue29CheckBox.Location = new System.Drawing.Point(8, 64);
this.Issue29CheckBox.Name = "Issue29CheckBox";
this.Issue29CheckBox.Size = new System.Drawing.Size(72, 15);
this.Issue29CheckBox.TabIndex = 2;
this.Issue29CheckBox.Text = "Issue 29";
//
// Issue28CheckBox
//
this.Issue28CheckBox.Checked = true;
this.Issue28CheckBox.CheckState =
System.Windows.Forms.CheckState.Checked;
this.Issue28CheckBox.Location = new System.Drawing.Point(8, 40);
this.Issue28CheckBox.Name = "Issue28CheckBox";
this.Issue28CheckBox.Size = new System.Drawing.Size(72, 15);
this.Issue28CheckBox.TabIndex = 1;
this.Issue28CheckBox.Text = "Issue 28";
//
// Issue12CheckBox
//
this.Issue12CheckBox.Location = new System.Drawing.Point(8, 16);
this.Issue12CheckBox.Name = "Issue12CheckBox";
this.Issue12CheckBox.Size = new System.Drawing.Size(72, 15);
this.Issue12CheckBox.TabIndex = 0;
this.Issue12CheckBox.Text = "Issue 12";
//
// ImportMenuItem
//
this.ImportMenuItem.Index = 0;
this.ImportMenuItem.Text = "&Import File";
this.ImportMenuItem.Click += new
System.EventHandler(this.ImportMenuItem_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "-";
//
// ExitMenuItem
//
this.ExitMenuItem.Index = 2;
this.ExitMenuItem.Text = "&Exit";
this.ExitMenuItem.Click += new
System.EventHandler(this.ExitMenuItem_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 267);
this.Controls.Add(this.IssueCloseGroup);
this.Controls.Add(this.StopButton);
this.Controls.Add(this.StartButton);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.QueuedListBox);
this.Controls.Add(this.AddButton);
this.Controls.Add(this.MemberTextBox);
this.Controls.Add(this.MemberLabel);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Menu = this.MainMenu;
this.Name = "MainForm";
this.Text = "Issue Closing Daemon";
this.Resize += new System.EventHandler(this.MainForm_Resize);
this.Closing += new
System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.IssueCloseGroup.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// start application
Application.Run( new MainForm() );
}

#region Form Event Handlers

/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddButton_Click(object sender, System.EventArgs e)
{
// add member to queue and clear text box
pMemberQueue.Enqueue( MemberTextBox.Text ); MemberTextBox.Clear();

// update the QueuedListBox
SyncLists();
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// stop the processing thread.
bStopSignal = true;
if( pProcessingThread != null )
pProcessingThread.Join();
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Resize(object sender, System.EventArgs e)
{
// hide the form if minimize requested
if( FormWindowState.Minimized == this.WindowState )
this.Hide();
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotifyIcon_DoubleClick(object sender, System.EventArgs
e)
{
// show MainForm and set to appropriate state
this.Show(); this.WindowState = FormWindowState.Normal;
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartButton_Click(object sender, System.EventArgs e)
{
if( bStopSignal )
{
// disable controls that aren't valid when running
StartButton.Enabled = false;
Issue12CheckBox.Enabled = false;
Issue28CheckBox.Enabled = false;
Issue29CheckBox.Enabled = false;

// enable controls that are valid when running
StopButton.Enabled = true;

// start the processing thread
bStopSignal = false;
MessageBox.Show( "Starting Processing Thread" );
(pProcessingThread = new Thread( new ThreadStart( ThreadProc )
)).Start();
}
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopButton_Click(object sender, System.EventArgs e)
{
// enable controls that are valid when stopped
StartButton.Enabled = true;
Issue12CheckBox.Enabled = true;
Issue28CheckBox.Enabled = true;
Issue29CheckBox.Enabled = true;

// disable controls that aren't valid when stopped
StopButton.Enabled = false;

// stop the processing thread
bStopSignal = true;
if( pProcessingThread != null )
{
MessageBox.Show( "Stopping Processing Thread" );
pProcessingThread.Join();
pProcessingThread = null;
}
}


private void ExitMenuItem_Click(object sender, System.EventArgs e)
{

}

private void ImportMenuItem_Click(object sender, System.EventArgs e)
{
if( ImportFileDialog.ShowDialog() == DialogResult.OK )
{
try
{
using( StreamReader reader = new StreamReader(
ImportFileDialog.FileName ) )
{
string line = null;
while( ( line = reader.ReadLine() ) != null )
{
// add member to queue and clear text box
pMemberQueue.Enqueue( line );
}

// update the QueuedListBox
SyncLists();
}
}
catch( Exception ex )
{
MessageBox.Show( "Exception: " + ex.ToString() );
}
}
}

#endregion Form Event Handlers
}
}

I commented the Icon setting line because I wasn't sure if it was
needed. I can't verify that compiles, it should, all I did was remove
the references to the other forms which aren't valid for this problem.

Thanks for any help, this is driving me nuts.
 
taylorjonl wrote:

<snip mammoth code>

That's really not short, is it?
From the page I referred you to: "Anything which is unnecessary should
be removed."

Do you really believe that there's nothing in the code you posted which
isn't relevant to the question? How about the checkboxes, the text box
etc? As it is, there's far too much code to look through. If you pare
down the code to the bare minimum, you may well find the problem
yourself.

You definitely *do* have a problem calling SyncLists() inside
ThreadProc() though, for the reasons I gave before.
I commented the Icon setting line because I wasn't sure if it was
needed. I can't verify that compiles, it should, all I did was remove
the references to the other forms which aren't valid for this problem.

Why can't you verify that it compiles? Just bring up a command line
with the appropriate variables set (there's a batch file to set them if
you want), cut and paste your code into a text file, and run csc.
That's what I'd be doing, so I don't see what's likely to stop you from
being able to do the same.

Jon
 
Sorry if I pasted too much code, didn't know how much you wanted.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Threading;

namespace Issue
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button StartButton;
private System.Windows.Forms.Button StopButton;

// member processing variables
private Thread pProcessingThread;
private bool bStopSignal;

public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

// setup member processing variables
bStopSignal = true;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


/// <summary>
/// </summary>
private void ThreadProc()
{
MessageBox.Show( "Please browse to the issue viewer base URL to
start" );

string member = null;
while( !bStopSignal )
{
Thread.Sleep( 100 );
}
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(MainForm));
this.StartButton = new System.Windows.Forms.Button();
this.StopButton = new System.Windows.Forms.Button();

//
// StartButton
//
this.StartButton.Location = new System.Drawing.Point(120, 160);
this.StartButton.Name = "StartButton";
this.StartButton.Size = new System.Drawing.Size(160, 32);
this.StartButton.TabIndex = 5;
this.StartButton.Text = "Start";
this.StartButton.Click += new
System.EventHandler(this.StartButton_Click);
//
// StopButton
//
this.StopButton.Enabled = false;
this.StopButton.Location = new System.Drawing.Point(120, 208);
this.StopButton.Name = "StopButton";
this.StopButton.Size = new System.Drawing.Size(160, 32);
this.StopButton.TabIndex = 6;
this.StopButton.Text = "Stop";
this.StopButton.Click += new
System.EventHandler(this.StopButton_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 267);
this.Controls.Add(this.StopButton);
this.Controls.Add(this.StartButton);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "MainForm";
this.Text = "Issue Closing Daemon";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// start application
Application.Run( new MainForm() );
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartButton_Click(object sender, System.EventArgs e)
{
if( bStopSignal )
{
// disable controls that aren't valid when running
StartButton.Enabled = false;

// enable controls that are valid when running
StopButton.Enabled = true;

// start the processing thread
bStopSignal = false;
MessageBox.Show( "Starting Processing Thread" );
(pProcessingThread = new Thread( new ThreadStart( ThreadProc )
)).Start();
}
}


/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopButton_Click(object sender, System.EventArgs e)
{
// enable controls that are valid when stopped
StartButton.Enabled = true;

// disable controls that aren't valid when stopped
StopButton.Enabled = false;

// stop the processing thread
bStopSignal = true;
if( pProcessingThread != null )
{
MessageBox.Show( "Stopping Processing Thread" );
pProcessingThread.Join();
pProcessingThread = null;
}
}
}
}

That is the bare minimum and I have compile it. It works as expected.
Something is very wrong in my code. I am going to do another test with
only the WebBrowser control removed as I would expect this to be the
cause.

Sorry about not trying to compile from command line before, I tried to
that from work a while ago and it crashed my system. I don't have any
batch file with the environmental variables but just added csc.exe to
the path and it compiles. If you have this batch file please post it
because if I can compile at work I can find the problem.

Thanks for the help, I will post once I have found a solution.
 
taylorjonl said:
Sorry if I pasted too much code, didn't know how much you wanted.

The hint was in the page I linked to :)

That is the bare minimum and I have compile it. It works as expected.
Something is very wrong in my code. I am going to do another test with
only the WebBrowser control removed as I would expect this to be the
cause.

Right. Now you've got a minimal base, you can gradually add bits until
it fails.
Sorry about not trying to compile from command line before, I tried to
that from work a while ago and it crashed my system. I don't have any
batch file with the environmental variables but just added csc.exe to
the path and it compiles. If you have this batch file please post it
because if I can compile at work I can find the problem.

There should be one that came with Visual Studio. Look under the Visual
Studio installation directory for vsvars32.bat (VS.NET 2003) or
vcvarsall.bat (VS 2005).
 
Back
Top