Thread not starting

Z

Zippy

I've got a problem starting a thread in c#.

Two scenarios.

1) got a form, which creates an instance of a class. in the
constructor of the class is a call to a method which starts a thread.

public void StartThread ()
{
new Thread (new ThreadStart (this.WaitExecute)).start();
Console.WriteLine ("thread started")
}

private void WaitExecute()
{
Console.WriteLine ("thread sleep");
Thread.Sleep (30000);
SignOn();
this.Invoke (new EventHandler (this.Restart));
Console.WriteLine ("thread done");
}

this works.

scenario 2
send 2 msmq messages (send 1 and its fine) and the thread doesnt start
ie all i get on the console is the "thread started" message and not
the "thread sleep"

the msmq call was originally in the same class as the thread above,
but i've since move it ot its own class.

i have also now moved the call to the startThread onto a button on the
main form

i can send one message and the click on the button and it starts fine
send two messages and the thread wont start.

any ideas would be appreciated

Andy
 
Z

Zippy

The rest of the code is on my laptop, so i'll try and post a short but
complete bit of code as soon as possible

in the meantime..

when i call the StartThread method - it starts the thread (the restart
callback simply checks for a terminate flag, then calls the
StartThread, which gives me a 30 second timer.

all works fine until i send my two msmq messages (cant post this code
as theres pages of it)

What i'm really after is whether anyone can think of any generic
reasons why a thread would not start.

for example if the hand held has run out of free processes.

the signOn method creates a socket and sends a packet to a server.
Colud the msmq and the socket be clashing with each other ?

Andy
 
A

Alex Feinman [MVP]

Not enough infomation to answer your question, but it looks like what you
are doing can be better achieved by using System.Threading.Timer class
 
Z

Zippy

not sure this is the same problem or not
this sample basically creates a form with two labels.

The form creates an instance of the TempTest class and registers a
callback method.

The TempTest class creates a timer (using code pulled from Andy
Wigley's .Net Compact Framework book) and increments a counter, the
value of which is then passed back to the form.

this works for as long as you like until you minimise and restore the
form when the timer stops firing.

any ideas?

Andy

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace TempTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private TimerTest tt;

public Form1()
{
InitializeComponent();

}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 24);
this.label1.Size = new System.Drawing.Size(40, 16);
this.label1.Text = "Count";
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 24);
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.Text = "0";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(634, 195);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

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

private void Form1_Load(object sender, System.EventArgs e)
{
tt = new TimerTest();
tt.RegisterCallBack (new EventHandler (UpdateCount));
}

public void UpdateCount (object sender, System.EventArgs e)
{
label2.Text = ((TimerTest.CountArgs)e).count.ToString();
}

}
}

using System;
using System.Threading;

namespace TempTest
{
/// <summary>
/// Summary description for TimerTest.
/// </summary>
public class TimerTest
{
private EventHandler CallBack;
private int Count = 0;


public TimerTest()
{
Timer timer = new Timer(new TimerCallback (CallBackFunction), new
TimerState (0),
3000,10000);
}

public void RegisterCallBack (EventHandler eh)
{
CallBack = eh;
}

void CallBackFunction (Object state)
{
Count++;
if (CallBack != null)
CallBack (this, new CountArgs (Count));
}

public class TimerState
{
public int Value;
public TimerState (int v)
{
Value = v;
}
}

public class CountArgs : EventArgs
{
public readonly int count;

public CountArgs (int val)
{
this.count = val;
}
}
}
}
 
J

Jon Skeet [C# MVP]

Zippy said:
not sure this is the same problem or not
this sample basically creates a form with two labels.

The form creates an instance of the TempTest class and registers a
callback method.

The TempTest class creates a timer (using code pulled from Andy
Wigley's .Net Compact Framework book) and increments a counter, the
value of which is then passed back to the form.

this works for as long as you like until you minimise and restore the
form when the timer stops firing.

any ideas?

Well it looks to me like it's not using Control.Invoke when it should
be - it's trying to update the label from the threadpool thread that
the timer's firing on. That's a big no-no.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top