Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

NOTE: This is a repost of Xtreme VB Talk, so that's why there are two posts

 

Well, hello, I come by to post some C# sharp, so I'll just start of :)

 

POST2:

I'm declaring a form-wide thread, that I want to activate when there's a keystroke. It works perfectly the first time, but when the moving.Resume(); hits the second time, the MessageBox doesn't show up anymore. I think I'm making a fundamental error in my threading logic, but could someone point it out?

 

Thread main;

 

Private void Form1_Load(Object sender, System.EventArgs e)

{

moving = New Thread(New ThreadStart(Run));

moving.Start();

moving.Suspend();

}

 

Private void Form1_KeyDown(Object sender, System.Windows.Forms.KeyEventArgs e)

{

Thread.Sleep(0);

If(moving.ThreadState==ThreadState.Suspended)

{

moving.Resume();

}

 

e.Handled = true;

}

 

Public void Run()

{

MessageBox.Show("Still running");

moving.Suspend();

}

 

 

POST2:

 

I found out that my thread was just stopped after execution

But my question remains: How can I keep it running?

I now have this solution:

 

 

If(moving.ThreadState==ThreadState.Stopped)

{

moving = New Thread(New ThreadStart(Run));

moving.Start();

}

 

But I guess that not too healthy for my overhead, while I'm waiting for the Garbage Collector to come by...

For questions about VS .net extensibility, please fire at me! :)

For readability, please use the [ CS][/CS ] tags

Posted

What I think happens (didn't try running the code though, but you can easily check it in the debugger).

 

first time the thread is fired: it shows the message box, and then suspends itself.

 

The second time the thread is fired, it continues from the suspend, and has to do nothing -> stopped. It doesnt 'perform' the Run method all over again, it continues from where it was suspended.

You can easily check this by doing a messagebox.show after the suspend with a different message to indicate it is the 2nd messagebox.

 

The usual way around this is to put the 'worker' method (your Run method) of a thread in a loop:

I'll try it out of my head :

while (keepTheThreadRunning)
{
 messagebox.show(myMessage);
 thread.suspend()
}

 

Where 'keepTheThreadRunning' is a boolean at the class level that can be set to false when you want the loop to exit, e.g. when your application is closing.

 

There is also an exception you can catch that is thrown when the thread is stopped (using abort) from another thread. That could give you something in the Run method like:

try
{
 //loop till infinity, we'll get an exception when the thread is aborted
 while (true)  // should be equal to old C++ for(;;) construction
 {
   //show the message box
   messagebox.show(myMessage)
   //wait till we have to show it again.
   thread.suspend()
 }
}
catch(ThreadAbortException e) 
{
 //thread has been stopped from the outside, clean up the any remaing resources 
}

I suggest looking into the thread.abort method and the threadabortexception class for this last solution.

Nothing is as illusive as 'the last bug'.
Posted
But I think this doesn't solve the problem of the thread being stopped after one execution.

For questions about VS .net extensibility, please fire at me! :)

For readability, please use the [ CS][/CS ] tags

  • Administrators
Posted

The thread function is just like any other function it starts, runs to completion and then stops. Your code

public void Run()
{
MessageBox.Show("Still running");
moving.Suspend();
}

displays a messagebox and then suspends the thread (I'm assuming moving is declared at the form level). If you want the thread to be executed multiple times then you will need to either make it loop or spawn multiple copies.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
I want it to loop, because the thread is gonna be up for a long time(few hours) and with active use the thread would be spawned every 5 seconds at least, increasing it's memory use by 12K each 5 seconds, leaving a hole of 8,5 mb of memory use. This may not seem to much, but I maybe want to make more threads, quickly making this a real issue...(or should I explicitly collect garbage?)

For questions about VS .net extensibility, please fire at me! :)

For readability, please use the [ CS][/CS ] tags

Posted

It's for a little game(more like a overrated threading test, it's better then having customers and provider as in the MSDN sample) and it uses the moving thread for the "hero" to move around while you can still check on data(like mouseclicks that reset certain things)

 

[CS]

PictureBox main = new PictureBox();

ArrayList objects = new ArrayList();

 

Thread moving;

Keys keyholder;

 

private void Form1_Load(object sender, System.EventArgs e)

{

this.Height = 600;

this.Width = 800;

main.Top = 50;

main.Left = 50;

Image test;

String path = @"C:\Documents and Settings\stephan.van.hugten\My Documents\My Pictures\";

 

test = Image.FromFile(path + "happy.gif");

main.Image = test;

main.Size = test.Size;

this.Controls.Add(main);

 

objects.Add(new PictureBox());

((PictureBox)objects[0]).Top = 250;

((PictureBox)objects[0]).Left = 250;

test = Image.FromFile(path + "House.jpg");

((PictureBox)objects[0]).Image = test;

((PictureBox)objects[0]).Size = test.Size;

this.Controls.Add((PictureBox)objects[0]);

 

moving = new Thread(new ThreadStart(Run));

moving.Start();

}

 

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

{

keyholder = e.KeyCode;

Thread.Sleep(0);

if(moving.ThreadState==ThreadState.Stopped)

{

moving = new Thread(new ThreadStart(Run));

moving.Start();

}

e.Handled = true;

}

 

public void Run()

{

//Move me lad, move!

}

[/CS]

 

(BTW: How do you do the C# code tags?)

EDIT: Nvm

For questions about VS .net extensibility, please fire at me! :)

For readability, please use the [ CS][/CS ] tags

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...