|
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.
|
|
|
|
|
Hello,
I have created a Process that runs a command from cmd and handles it's output using event Handler.
I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done.
I tried the following, but didn't work :
processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
sb = new StringBuilder();
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.CreateNoWindow = true;
public int ConnectToServer()
{
processInfo.RedirectStandardInput = true;
process = Process.Start(processInfo);
process.StandardInput.WriteLine("username");
process.StandardInput.Flush();
process.StandardInput.WriteLine("myPswd$");
process.StandardInput.Flush();
process.StandardInput.Close();
process.BeginOutputReadLine();
process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
string d = e.Data;
if (!string.IsNullOrEmpty(d))
{
if (d.indexOf("Completed")) {
connected = true;
}
}
}
It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet.
Any help is highly appreciated.
Thanks & Regards,
|
|
|
|
|
how about adding username and password as command arguments
eg.
cmd.exe "username" "password"
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Thanks Dear,
But no, can't add like that. Can either pass a txt file or input on screen. As I don't want to have txt file with readable username & password, so thought of this option.
Thanks & Regards,
|
|
|
|