|
Do not use a Sleep command. This is the coding equivalent of summoning up a demon, and should be avoided at all costs. You are trying to run something at a periodic interval, so a timer should be your first port of call. If, however, your call is to happen 30 seconds AFTER the completion of the last call, then you may want to look at an alternate mechanism (although stopping and starting the timer may suffice for you here).
|
|
|
|
|
Out of curiosity, Pete, what's wrong with using Sleep() on a background thread? Obviously you NEVER want to sleep the GUI (As that would make the application non-responsive), but I don't see any downside to sleeping a background thread.
|
|
|
|
|
What happens if you need to tell the background thread to finish, and it's just started a Sleep(30000000) ? A better mechanism is to do something like:
lock (SyncLock)
{
Monitor.Wait(SyncLock, 30000000);
} I shudder whenever I see Thread.Sleep in code because I just know that the pitfalls haven't been considered.
|
|
|
|
|
Never thought of doing it that way... *adds that to the mental arsenal*
Generally I use Thread.Sleep for background threads that are intended to go for the entire life of the application, so terminating the thread isn't much of an issue... Yeah, it drops with an exception when the system is shutting down, but by that point, it's irrelevant as long as I make sure there isn't anything that can't be randomly interrupted.
But yeah, never thought of using a pulse as a thread-killer...
if (Monitor.Wait(ThreadAbortLock, 30000)) return;
Time to take a look through my system... I know a couple spots where this method would be preferable.
|
|
|
|
|
I always use a System.Timers.Timer for my Windows Services.
|
|
|
|
|
your first concern should be: do I want those operations on the GUI thread, or anywhere but the GUI thread? Having it in a separate thread right now points to the latter. So a Windows.Forms.Timer would be wrong as that one ticks on the GUI thread, all other timers could be fine.
Of course, the operations not running on the GUI thread also means you need InvokeRequired/Invoke if your updated data needs to become visible on the GUI, and having multiple threads (or non-GUI timers) requires attention to synchronization.
A secondary concern could be how much control you need on the actual thread the operations will be running. A timer would tick on a ThreadPool thread, and you would have no say in its priority, nor would you be able to abort it, if you were so inclined; ditto for a BackgroundWorker. A regular thread (an instance of the Thread class) would offer you full control.
One final remark: a timer tends to set a target for periodicity, it is the time span between the beginning of two consecutive events; a Thread.Sleep() sets a gap, typically from the end of one action to the beginning of another. This difference most often is irrelevant, it may cause the actual period to be slightly larger.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
What's the different between 2 code below?
1-Form2 obj1=new Form2();
obj1.show();
2-Form2 obj2=new Form2();
obj2.showDialog();

|
|
|
|
|
One shows a dialog (i.e. a modal window), the other shows a modeless window.
|
|
|
|
|
Well, showdialog() causes the form to be show as a "dialog" (hence the name). That means that it will retain focus in your application: you will notice that you can't place the focus on the calling form while the dialog remains active/visible.
Make sense?
|
|
|
|
|
so you mean that with showdialog I can access the component in form2 like textbox?
what can I do with dialog?
|
|
|
|
|
You need to make an accessor to expose the items you need from the form. Here is an example: I created a project with two forms. Form1 (parent/main) contains a textbox and a button. Form2 (dialog) has a textbox and two buttons (OK and Cancel). In the properties of the buttons, I set the "DialogResult" to "OK" and "Cancel" respectively, then in Form2's properties, set the AcceptButton to the OK button, and the CancelButton to the Cancel button. From there, the code is simple:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
if (frm.ShowDialog() == DialogResult.Cancel) return;
textBox1.Text = frm.Value;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string Value
{
get { return textBox1.Text; }
}
}
|
|
|
|
|
Thanks you realy help me. 
|
|
|
|
|
In obj2.show you can move form everywhere but in another case it can move in the parent form area. I guess.. 
|
|
|
|
|
freshonlineMax wrote: n obj2.show you can move form everywhere but in another case it can move in the parent form area. I guess..
You guess wrong.
The second example just shows a dialog box. There's no constraint in where it can be moved - the constraint is in how you can interact with the hosting application. As it's modal, processing on the primary thread halts in the calling code until it is dismissed.
|
|
|
|
|
As an addition to what others have told you, the Form object must be disposed later if you show it with ShowDialog method, so in this case it is much better to do this:
using (Form2 obj2 = new Form2())
{
obj2.ShowDialog();
}
|
|
|
|
|
An additional addition to the previous good answers...
ShowDialog returns a value of type DialogResult that enables you to determine the result of whatever that form was shown for - useful for custom message box type dialogs etc.
|
|
|
|
|
Yes Both are using for showing Form.
But Firt one is used for showing form as a normal form.
But Second One Is Used for Showing Form as a Dialog. You can not access Normally from Mouse or Keyboard.
First Case shows Non Immediate Value Required.
Second Shows Must Value Required.
Best Regard
If you can think then I Can.
|
|
|
|
|
Hi,
I've tab control in my application and the shape looks like below.
_____________
tab1 |tab2 |
-----------------------------------------------
|
|
|
|
-----------------------------------------------
tab headers are in rectangle shape
But I would like design the tabs as below (as we have in visual studio)
_____ ______
/tab1 |/tab2 |
-----------------------------------------------
|
|
|
|
----------------------------------------------- Please guide me to change the shape of tab header
|
|
|
|
|
|
|
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hello Everyone
I have created a Client/Server application on C# on WPF IDE and I have a little problem on my UserClient property class...
Within this class I have a method called:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
if (temp != null)
{
return (userId == temp.userId);
}
if (temp != null)
{
return (userPass == temp.userPass);
}
return false;
}
The problem I'm having is:
The class UserClient is highlighted with a green line under-neath where when I place MouseOver it sas:
UserClient overrides Object.Equals but does not override Object.GetHashCode
Here is the entire UserClient class code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace DtposClient
{
public class UserClient
{
private string UserFullName;
private string UserAccessLevel;
private int UserId;
private int UserPassword;
public string fullName
{
get { return UserFullName; }
set { UserFullName = value; }
}
public string accLevel
{
get { return UserAccessLevel; }
set { UserAccessLevel = value; }
}
public int userId
{
get { return UserId; }
set { UserId = value; }
}
public int userPass
{
get { return UserPassword; }
set { UserPassword = value; }
}
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
if (temp != null)
{
return (userId == temp.userId);
}
if (temp != null)
{
return (userPass == temp.userPass);
}
return false;
}
public UserClient(string fullNam, string aLevel, int usId, int pass)
{
fullName = fullNam;
accLevel = aLevel;
userId = usId;
userPass = pass;
}
}
}
Could someone please help me crack this problem....
thanks in advance
kind regards
lapeci
|
|
|
|
|
It's a warning that you are not implementing best practices. You can read the rule description here to understand why
"You get that on the big jobs."
|
|
|
|
|
Moreover, this code fragment :
if (temp != null)
{
return (userPass == temp.userPass);
}
will never be executed (if temp != null, the method will still have returned a value).
The error you get is because when you override the Equals() method, compiler is expecting you also override the GetHashCode() one :
public override int GetHashCode()
{
return UserId;
}
You can also have a more complex hashcode generation, for example :
public override int GetHashCode()
{
return UserId ^ UserPassword ^ UserFullName.GetHashCode() ^ UserAccessLevel.GetHashCode();
}
Here it's up to you to decide which elements of your class will be taken for the hashcode generation.
|
|
|
|
|
Let me help you with this method:
public override bool Equals(object obj)
{
UserClient temp = obj as UserClient;
return temp != null && userId == temp.userId && userPass == temp.userPass;
}
I guess this is what you wanted it to do. For the warning you mention, I think someone has already told you to make a customized GetHashCode for your UserClient class.
|
|
|
|