Himo Posted December 22, 2004 Posted December 22, 2004 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... Quote For questions about VS .net extensibility, please fire at me! :) For readability, please use the [ CS][/CS ] tags
Wile Posted December 23, 2004 Posted December 23, 2004 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. Quote Nothing is as illusive as 'the last bug'.
Himo Posted December 23, 2004 Author Posted December 23, 2004 But I think this doesn't solve the problem of the thread being stopped after one execution. Quote For questions about VS .net extensibility, please fire at me! :) For readability, please use the [ CS][/CS ] tags
Administrators PlausiblyDamp Posted December 23, 2004 Administrators Posted December 23, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Himo Posted December 23, 2004 Author Posted December 23, 2004 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?) Quote For questions about VS .net extensibility, please fire at me! :) For readability, please use the [ CS][/CS ] tags
Administrators PlausiblyDamp Posted December 23, 2004 Administrators Posted December 23, 2004 I would rarely recomend explicitly managing garbage collection, far too often it will cause more problems than it solves. It may be easier if you tell us what you are trying to acheive, there may be an alternate solution to the problem. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Himo Posted December 23, 2004 Author Posted December 23, 2004 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 Quote For questions about VS .net extensibility, please fire at me! :) For readability, please use the [ CS][/CS ] tags
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.