|
You can't extend Thread because it is a sealed class.
Skoder wrote: Then i could just override the Run() method, and i could create a new thread by instanciating my class like MyThreadClass tc = new MyThreadClass();
and call tc.Run() to make the thread start.
I'm not sure 100% what kind of effect you are going for here. You could have a class that implements the equivalent to Runnable. Then override the Run method to spawn a thread that calls a method in that class.
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
Hey,
thanks for the answer.
Yes i found out that it was a sealed class according to msdn.
But isnt there some way to make a class behave like a Thread ?
Or is the only way to create instances of a Thread and pass a delegate ?
|
|
|
|
|
Skoder wrote: But isnt there some way to make a class behave like a Thread ?
What type of behavior are you wanting? Can you give us a code sample and describe what you want to happen?
Skoder wrote: Or is the only way to create instances of a Thread and pass a delegate ?
That's the best way (IMHO).
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
Actually, it's recommended you use the thread pool instead of creating threads yourself. You can use the ThreadPool to run background tasks via ThreadPool.QueueUserWorkItem(...).
Optionally, if you're trying to do work on a background thread which needs to return data to the UI thread, use System.ComponentModel.BackgroundWorker (.NET 2.0), which takes care of getting the calls on the correct thread for you.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
That's not true. Creating a thread yourself is just fine and provides a different set of features that you can't get through the ThreadPool. For example you can't Join, Abort or check the state of items in the ThreadPool.
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
I didn't say it was disallowed or discouraged. Microsoft simply recommends using the thread pool over creating your own thread for performance and efficiency reasons. See this article[^] for more information.
|
|
|
|
|
Your original statement was more a blanket statement. I figured you didn't mean it to be all encompasing but I wanted to be explicit for the people who are just starting out with threads.
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
Hey again,
i just want to make a class which acts like a thread.
Soo i can have private varibles, lists, methods and other stuff which is only used in that single Thread. (thats how it is done in Java).
Soo i can just do something like
myThread t = new myThread(var1, var2);
t.Run();
it is just for because i want to seperate it.
But i guess i can just do the following on the class i want to be a thread:
class myThread {
public myThread(var1, var2) {
Thread t = new Thread(new ThreadStart(Run));
}
public void Run() {
bla bla bla
}
}
It will basically work the same way.
|
|
|
|
|
You can use
int var1, string var2;
ThreadPool.QueueUserWorkItem(methodToCall, new object[] {var1, var2});
void MethodToCall(object arguments)
{
object[] args = (object[])arguments;
int var1 = (int)args[0];
int var2 = (string)args[1];
}
Note that if you're using .NET 2.0, you can use anonymous methods (basically blocks of code inside a function) to make this a lot easier and more typesafe. For example,
int var1, string var2;
ThreadStart methodToExecuteWithVariables = delegate
{
};
new Thread(methodToExecuteWithVariables).Start();
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
Hi again,
I have a textbox where the text is longer than the textbox width. When I type text in and leave, the beginning of the string shows, and focus goes on to the next box. But if I don't change the text (just tab through or whatever), the beginning of the string is cut off, leaving the end of the string visible. I do use textbox.SelectionStart = 0; , but it it only works if text is changed.
Any suggestions???
Thanks again,
Mel
|
|
|
|
|
Hm..
Maybe try to add [code]textbox.SelectionStart = 0;[/code] to the LostFocus event?
|
|
|
|
|
That's the weird part, I have the FocusLeave event.
private void txtBoxLeave(object sender, System.EventArgs e)
{
textbox.SelectionStart = 0; ...
}
Again, it works when the text is altered (even if it ends up being the exact same text), but not if nothing at all is done.
Mel
|
|
|
|
|
Maybe you can cheat a little bit?:
private void txtBoxLeave(object sender, System.EventArgs e) {
if (textbox.Text.Length > 0) {
textbox.SelectionStart = 1;
}
textbox.SelectionStart = 0;
}
Dunno if this works though...
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
It just refuses to work. I even put a messagebox in before selectionstart = 1, and the messagebox showed up ok. But no beginning of the string. (If it helps anyone get any more ideas, pressing only the left arrow before leaving the textbox is enough to get the beginning part. And I did try adding a SendKeys.Send("{Left}") to the KeyDown event, but that only sent a left to the next textbox (even though it was before the command to focus on the next textbox), which I'm not sure I understand.)
Mel
|
|
|
|
|
Ok, let me try again:
private void txtBoxLeave(object sender, System.EventArgs e) {
textbox.Text = textbox.Text + " "
textbox.Text = textbox.Text.SubString(0, textbox.Text.Length - 1)
textbox.SelectionStart = 0;
}
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
Hey,
Thanks a lot for looking again. I did try adding stuff to the text, but it was still refusing to work for me. I just posted what I found to do the trick before seeing your message.
Anyway, thanks again, and happy holidays!
Mel
|
|
|
|
|
I'm happy you got it working. You have happy holidays too!
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
Ok, got it going. It's as simple as textbox.Select(0,0);
|
|
|
|
|
how i can see the Browser Activity Monitor
ex: i open http://www.hotmail.com
and write email
and password
i need to read it by this program
-----------
Palestine
|
|
|
|
|
Hi,
How do get the screen coordinates of the mouse cursor to appear on the WinForm next to the cursor and follow the cursor around as it is dragged
across the Form?
thanks a lot.
|
|
|
|
|
You can build a really simple solution using the MouseMove eventhandler:
if(e.Button == MouseButtons.Left) {<br />
Point coordinate = PointToScreen(new Point(e.X,e.Y));<br />
<br />
}
WM.
What about weapons of mass-construction?
|
|
|
|
|
I have a project that had a Mainscreen that calls several forms via dll's. Visual inheritance if you will. There can only be one instance of each form running at a time. Therefore, I have the following class to pass each form through:
using System;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
namespace FormManager
{
public class FormLoader
{
private static HybridDictionary m_InitializedForms = new HybridDictionary();
public static void LoadFormType(Type formType)
{
Form frm = (Form)Activator.CreateInstance(formType);
if (IsAlreadyLoaded(formType))
{
frm.BringToFront();//this does not work.
return;
}
FlagAsLoaded(formType);
frm.Closed += new EventHandler(FormClosed);
frm.Show();
}
private static void FlagAsLoaded(Type formType)
{
m_InitializedForms[formType.Name] = true;
}
private static void FlagAsNotLoaded(Type formType)
{
m_InitializedForms[formType.Name] = false;
}
private static bool IsAlreadyLoaded(Type formType)
{
return ((m_InitializedForms[formType.Name] != null) &&
(bool)m_InitializedForms[formType.Name] == true);
}
private static void FormClosed(object sender, EventArgs e)
{
Form closingForm = (Form)sender;
closingForm.Closed -= new EventHandler(FormClosed);
FlagAsNotLoaded(sender.GetType());
}
}
}
So, on the mainscreen I would simply call the Form2 from a button_click event like so:
FormLoader.LoadFormType(typeof(Form2));
This works great. However, when I click on the button again, it does not bringtofront or have focus. I have done something very similar to this and it worked fine. I tried using the WIN32 API to bring it to front as well. That would instantiate another form. Still, no luck. Any suggestions would be great. Thanks in advance.
-- modified at 10:29 Wednesday 14th December, 2005
|
|
|
|
|
Instead of storign true or false as the dictionary values, you could try storing the form instances themselves. Then you could do something like
Form f = (Form)m_InitializedForms[Form2];
f.Show();
You could simply compare the value to null to decide whether to instantiate or show an existing one.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Here's your problem with the FormLoader . You check to see if the form type has been loaded and then try to BringToFront a new instance of the form. Here's a rework of your class that does what you want.
public class FormLoader
{
private static HybridDictionary m_InitializedForms = new HybridDictionary();
public static void LoadFormType(Type formType, Form parentForm)
{
lock( typeof( FormLoader ) )
{
if( eh == null )
{
eh = new EventHandler( FormClosed );
}
}
if( IsAlreadyLoaded( formType ) )
{
Form f = GetForm( formType );
f.Focus();
f.BringToFront();
return;
}
Form frm = (Form)Activator.CreateInstance(formType);
FlagAsLoaded( formType, frm );
frm.Closed += eh;
frm.Show();
}
private static void FlagAsLoaded( Type formType, Form f )
{
m_InitializedForms[formType.Name] = f;
}
private static void FlagAsNotLoaded( Type formType )
{
m_InitializedForms.Remove( formType );
}
private static bool IsAlreadyLoaded( Type formType )
{
return ( ( m_InitializedForms[ formType.Name ] as Form ) != null );
}
private static Form GetForm( Type formType )
{
return ( m_InitializedForms[ formType.Name ] as Form );
}
private static void FormClosed( object sender, EventArgs e )
{
Form closingForm = sender as Form;
if( closingForm != null )
{
closingForm.Closed -= eh;
FlagAsNotLoaded( sender.GetType() );
}
}
private static EventHandler eh;
} Hope the helps.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Curtis,
You are the man. Thanks for all of your help. I actually have to change it a bit to also control how MANY instances can be called for each form. However, I will have to take a stab at that later. When I do, do you mind if I hit you up? Thanks.
|
|
|
|