|
Thanks Saksida. As per your comment I noticed I have not provided any method which subscribes to the event. Now after I have written a "MyUserControlOne_DataChanged" method and its working nicely. Thank you very much for poiting me the mistake.
Now this is working nicely !
public class DataChangedEventArgs : EventArgs
{
private String _strArgVal = "";
public DataChangedEventArgs(String Value)
{
_strArgVal = Value;
}
}
public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e);
public class MyUserControlOne
{
String DataString = String.Empty;
private bool isEventWorking = false;
public event DataChangedEventHandler DataChanged;
public MyUserControlOne()
{
DataString = "";
this.DataChanged += new DataChangedEventHandler(MyUserControlOne_DataChanged);
}
protected void MyUserControlOne_DataChanged(object sender, DataChangedEventArgs e)
{
isEventWorking = true;
}
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
DataChanged(this, e);
}
public String SetData
{
set
{
if (value != DataString)
{
OnDataChanged(new DataChangedEventArgs("Event Fired !!!"));
} DataString = value;
}
}
public bool EventWorking
{
get { return isEventWorking; }
}
}
calling code:
static void Main(string[] args)
{
MyUserControlOne o = new MyUserControlOne();
o.SetData = "ABC";
o.SetData = "ABCD";
o.SetData = "ABCD";
Console.WriteLine(o.EventWorking);
}
Thanks,
Arindam D Tewary
|
|
|
|
|
It is worth changing your code slightly to cope with the case when you have no handler - if you think about it not handling an event is not an error, it is perfectly normal. As such, it should not throw an exception.
Arindam Tewary wrote:
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
DataChanged(this, e);
}
If you change your code to:
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
EventHandler eh = DataChanged;
if (eh != null)
{
eh(this, e);
}
}
Then a potential source of run time exception is removed. (The load of "eh" is to handle the admittedly rare case when the handler is removed between the test and the execution)You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Hi OriginalGriff,
You are very much correct. I would be changing my code for sure. Thanks for your comment. Highly appreciate it. Thanks,
Arindam D Tewary
|
|
|
|
|
I would use:
protected virtual void OnDataChanged(DataChangedEventArgs e)
{
if (DataChanged != null)
DataChanged(this, e);
}
I think this way it is easier to read and not declaring new variable
|
|
|
|
|
No, what Griff gave you is the correct way to do things; there is a (probably small) probability that another thread could remove the content of the event between your code checking it for null, and your code using it. A local copy fixes that.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
tham. Now i see the problem. It was same example in a book i was reading. Maybe it is time to burn that book
|
|
|
|
|
Arindam Tewary wrote: if (value != DataString)
{
OnDataChanged(new DataChangedEventArgs("Event Fired !!!"));
} DataString = value;
furthermore it is wrong to throw the event BEFORE the data change has occurred. A delegate is likely to read SetData to find out what the new data value is, it should not get the old value instead.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi Luc,
Thank you very much for your valuable input. Its great to get some input from MVPs
Thats surely bad design. I would change that immediately. As I was digged into that "NullException", so I overlooked that. Please if you want to provide any other input also, although origianl problem is solved, please provide.Thanks,
Arindam D Tewary
|
|
|
|
|
There are many articles and blogs that deal with events but most are a little overwhelming initialy.
I wrote this[^] article to provide a gentle tutorial which you may find useful.
|
|
|
|
|
Hi,
I'm trying to end 3 threads in a save way. I can get 2 threads to stop, but the last one doesn't seem to abort.
My try:
private volatile bool stopRequested = false;
this.stopRequested = true;
this.thdPageContent1.Join();
this.thdPageContent2.Join();
this.thdFileComparer.Join();
After the thdFileComparer thread, i got a messagebox that 'tells' me the threads are stopped:
MessageBox.Show("Threads stopped successfully");
Any idea what I might be doing wrong?
Thanks in advance!
|
|
|
|
|
The join method only waits for a thread to end... it does not abort the thread.
So, if your thread is in an infinite loop (or infinite wait), your join will be waiting infinitelly.
I must say I need to know the thread code to know what's wrong... but, if you are using some event (ManualResetEvent, for example) in the 3rd thread, you must set it. So, the thread can execute (and check the stopRequested variable).
|
|
|
|
|
Hi,
What do I need to do to end the thread after the Join?
The 3rd thread has a while loop and in side that while loop i got a nested for statement.
One for statement in another one.
I check for the volatile variable in my while loop. And once more inside the deepest for statement.
That should be enough right?
|
|
|
|
|
If you check the variable, it should end.
Are you sure there is nothing more happening?
Can't you put a break point in the last join and, instead of executing it, as all threads are stopped, check what's happening in the other thread? Maybe put some breakpoints at that moment and then allow the Join to go... you must see exactly what the other thread is doing.
|
|
|
|
|
Yes I'm sure. The variable gets checked alright. But it doesn't seem to stop it.
Here is that thread i'm talking about with the code:
private void FileComparerThread()
{
while (this.stopRequested != true)
{
for (int i = 0; i < Directory.GetFiles(this.dest).Length; i++)
{
string[] filePaths = Directory.GetFiles(this.dest, "*.txt");
for (int j = 0; j < filePaths.Length; j++)
{
if (this.stopRequested != true)
{
if (filePaths[i].ToString() != filePaths[j].ToString())
{
bool comparedFiles = this.FileCompare(filePaths[i].ToString(), filePaths[j].ToString());
if (comparedFiles)
Directory.Move(filePaths[j], Path.Combine(this.dest2, Path.GetFileName(filePaths[j])));
}
}
}
if(filePaths.Length > 3)
File.Move(filePaths[i], Path.Combine(this.dest, Path.GetFileName(filePaths[i])));
Thread.Sleep(5000);
}
}
}
And then I got a this.FileCompare() function that checks 2 files for equality thats it.
I've done some big time debugging. Its just looping those for statements.
Even though the stopRequested = true.
It's not comming out that for statement.
|
|
|
|
|
I think I know what's the problem.
You didn't return in the internal if.
So, stopRequested is true. Your method does not compare the files, but continue in it's loop, waits for 5 seconds and so on.
So, I think instead of:
if (this.stopRequested != true)
{
... some code...
}
You should use:
if (stopRequested)
return;
|
|
|
|
|
Hi,
You're right, About an hour ago i got an else statement after that if-statement with a return.
I'll use your suggestion. Saves me some line of codes :P
Thanks!
|
|
|
|
|
Yustme wrote: the last one doesn't seem to abort
Aborting a thread is a bad idea as it leaves things in an unknown state. But then maybe you're not really calling Thread.Abort() at all?
Yustme wrote: this.stopRequested = true;
This doesn't do anything, unless your thread is periodically checking that variable. But then you did not show such code, making me doubt you understand how your code is working.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi,
I didn't abort the thread. That was just 'speaking'.
I know, i check its state on different places periodically.
The code has grown too big to post it. I've reduced it now.
I didn't see your post yesterday!
|
|
|
|
|
Please i would appreciate any help i can get on getting my project started,cannot integrate the different classes on my server.
|
|
|
|
|
You might need to read through some chapters on .Net remoting in a C# book. They will provide some samples that will start you up.Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
I would like to be able to click a button (which is always located in the same area) of a external/downloaded html page?
Any help would be appreciated.
Language C# visual Studio.
|
|
|
|
|
Of specific HTML or all possible pages, including www.codeproject.com, wwww.google.com and others?
Or are you refering to Button submit within Form html tag?
|
|
|
|
|
Of all or any page.
For example... I download or view a page (lets say http://www.codeproject.com/Forums/1649/Csharp.aspx) and via code I click the "Search" button.
|
|
|
|
|
<form method="get" action="/info/search.aspx" name="Search" style="margin:0;">
<b>Search </b>
<input class="small-text" name="artkw" style="width:200px;" />
<select class="small-text offblack" style="font-weight:bold;" name="sbo">
<option value="kw">Articles / Quick Answers</option>
<option value="fm" selected="true">Messages</option>
<option value="s">Jobs</option>
<option value="ctlk">Product Catalog</option>
</select>
<input type="submit" class="button small-text" value=" Go! " />
</form>
As you can see, this is HTML for a submit. aka. Search. This has a GET method. it uses ? inside url. you can easly get what to send and then send it to url. in this case: /info/search.aspx
|
|
|
|
|
Unfortunately this is not what I'm looking for.
What I need is via c# I download a page onto my drive (let's. say http://www.writtenonthecity.com/display.php?image=2301&loc=8&type= ).
Now once the page is loaded I would do a search (in visual studio) in the page for the "Submit" button and via C# code i want to click it as if I was actually sitting there and using my mouse to click the button but via code.
I don't want to generate any buttons or edit box's or change the page's html, I just want to know how to click a button via code as if I was actually sitting there and clicking it with my mouse with a unknown downloaded page. Not a aspx page that I've created with a onclick event.
|
|
|
|