|
hi friend,
i have two thread which share the job(thread1,thread2). how do i terminate the thread1 from thread2. is this posible? and how to return the value form the thread
Thanks in advanced
Nishar A.
|
|
|
|
|
|
|
Hi All,
Im hoping someone can give me an idea on what I may be doing wrong (if anything). In my project I have a Combo Box which I populate from a SQL Database (using a Dataset and a for statement looping through each record). I have done it on form load and it works well however I decided to run it from a background thread so I become familiar with threads and for a few other reasons.
Anyway, i have done it using a Thread but I seem to have taken a very large performance hit. Without a thread, populating the combo box (on Form_Load) it takes about 1 second. With the Thread it takes about 6 seconds.
Im assuming since Im new with threads there is something obvious in my code that is wrong. Here is the relevant code in question:-
//Create Delegates for the Threads to Access The Forms UI
private delegate void UpdateComboBox(ComboBox cboName,string strItemToAdd);
Code in the Form_load Event
//Hide the Status Label
lblStatus.Visible = false;
//Start by Enabling the Loading Circle
//This is an indicator that the Background Thread is still running
//The Background thread is connecting to the database and populating the Combo Boxes
lblLoadingFormData.Text = "Loading Data";
lblLoadingFormData.Visible = true;
lcFormData.Active = true;
//Create the Thread which will Update the Combo Boxes
Thread thrPopulateComboBoxes = new Thread(this.PopulateComboBoxes);
thrPopulateComboBoxes.Start();
Code to Populate the Combo Box
private void PopulateComboBoxes()
{
//This Method will Populate the ComboBoxes on the Form
//Customers
ArrayList al = new ArrayList();
//Customers Combo Box
al = myDatabaseFunctions.PopulateComboBox("SELECT Name FROM tblCustomers ORDER BY Name ASC", "Name", "Customers");
foreach (string s in al)
{
this.Invoke(new UpdateComboBox(this.AddItemToComboBox),new object[] {cboCustomer,s.ToString()});
}
}
I know the issue is not with the Arraylist as this is the same sort of code I am using in a non thread and the performance is fine
Finally the code to add to Combo Box
private void AddItemToComboBox(ComboBox cboName, string strItem)
{
cboName.Items.Add(strItem);
}
Can any1 offer some thoughts as to why Im taking such a performance hit? Am I doing something wrong with the threads?
Thanks in advance,
Daniel.
|
|
|
|
|
When Invoke is called from a worker thread, it sends a windows message to the UI thread which then runs the delegate. You are doing this for each string in your results, which will be slow.
You could run the db call on a worker thread and then call Invoke once with the entire ArrayList to populate the ComboBox .
Also, you should probably use a thread pool thread for this, either directly or using a BackgroundWorker .
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Hi,
I didn't look at your code as it was unformatted, lacking PRE tags. However I think you should read this[^].
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Hi all, I have some issues on Registering event, If you have any idea on my implementation please help me …
private SHDocVw.InternetExplorer browser;
HTMLDocument document;
HTMLDocumentEvents2_Event DocEvents;
This Event Fires well, when ever I click on any link the event is fired no issue on that. But when I click on the Text box the focus losts.
browser = (SHDocVw.InternetExplorer)pDisp;
DocEvents = (mshtml.HTMLDocumentEvents2_Event)document;
DocEvents.onclick += new HTMLDocumentEvents2_onclickEventHandler(DocEvents_onclick);
To over come this issues I have just implemented from one of the articles like bellow
public delegate void DHTMLEvent(IHTMLEventObj e);
[ComVisible(false)]
public class DHTMLEventHandler
{
public DHTMLEvent Handler;
mshtml.HTMLDocument Document;
public DHTMLEventHandler(mshtml. HTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
browser = (SHDocVw.InternetExplorer)pDisp;
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)browser.Document;
DHTMLEventHandler Handler = new DHTMLEventHandler(doc);
Handler.Handler += new DHTMLEvent(this.myCallback);
doc.onclick = Handler;
but it shows the bello error.
Ambiguity between 'mshtml.DispHTMLDocument.onclick' and 'mshtml.HTMLDocumentEvents_Event.onclick
So I have changed from mshtml.HTMLDocument to mshtml.DispHTMLDocument
Now there is no compilation error but while excecuting it shows the bellow error
System.NotImplementedException , NotImplemented “\r\n”
Somewhat I got the information that we have to use mshtml. IHTMLDocument2 So I have used as bellow
public delegate void DHTMLEvent(mshtml.IHTMLEventObj e);
[ComVisible(true)]
public class DHTMLEventHandler
{
public DHTMLEvent Handler;
mshtml.IHTMLDocument2 Document;
public DHTMLEventHandler(mshtml.IHTMLDocument2 doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
DispHTMLDocument dispDoc = (mshtml.DispHTMLDocument)browser.Document;
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)browser.Document;
DHTMLEventHandler Handler = new DHTMLEventHandler(doc);
Handler.Handler += new DHTMLEvent(this.myCallback);
dispDoc.onclick = Handler;
public void myCallback(IHTMLEventObj evo)
{
}
Now this runs good, but no event is fired while I click on the browser
If some one has any observation on that please guide me how to go with this. I am able to use IHTMLElementCollection and add events for each and every IHTMLElement But I am not interest in loop through all the elements and register events for each and every elements, rather want to regisert an event for a Document and from that I want to identify which element is clicked.
There are type of approaches.W3C Document Object Event Model.and Microsoft Event Object Model.
I think we can achieve the issues using Event Register and Bubbling
I know little bit on concept wise but not able to implement
If some one helps me … then it will be helpful for many…
Plese free to post your comments on that
Thanks for every one (those who involves in it)
|
|
|
|
|
I use AxSHDocVw.AxWebBrowser on a form. It work fine.
|
|
|
|
|
|
Hello,
I've been coding projects in Java for some time now and decided to move to C#. I have some difficulty using the notifyall equivalent of Java in C#. Here's how I use it:
In Java when I make a multithreaded application I use the Wait() command to make the thread sleep, and at some point I use the notifyAll command which makes all threads in the wait state continue operation (from then on it's usually a race over which gets the resources first and the others become blocked again.
Now the closest thing I have found to this in C# is the AutoRaiseEvent and ManualRaiseEvent classes.
How my program works is: There are usually 4-8 threads running asking a question from the class by putting the question in the input queue, the class (called Handler) gets the next question and puts the answer in the outputqueue along with the code that identifies the original question.
Up to now I've been using something like this:
While {!myHandler.hasAnswer(myQuestionCode))
{
Application.DoEvents();
}
unfortunatelly this uses up a lot of the CPU so it has to change.
I tried the following:
While {!myHandler.hasAnswer(myQuestionCode))
{
RaiseEventObject.WaitOne();
}
and when the Handler produces the answer it calls RaiseEventObject.Set();
I tried using this with AutoRaiseEvent but then only one of the Threads triggers and usually it's the wrong one so it goes back to WaitOne() and nothing happens.
I am now testing the ManualRaiseEvent and altering my code to this:
While {!myHandler.hasAnswer(myQuestionCode))
{
RaiseEventObject.Reset();
RaiseEventObject.WaitOne();
}
but still I see a problem, what if two threads pass the WaitOne stage and then before the others can become unblocked (among them the one whose answer has been given) the event is Reset , can that happen? or when the Set() command is sent ALL threads unblock?
If the above happens isn't there a chance that the handler (the calculations it does don't take much time) will trigger the Set while one of the threads is in part of the loop other than WaitOne ? if that happens and the Reset triggers, will the Set be lost?
Can anyone suggest corrections to my code or some sort of alternative?
I'm sorry if I did not explain it correctly, English is not my primary language, I tried explaining it as best as I can.
Thanks in advance for any help
|
|
|
|
|
|
Thank you for the reply I tried coding all options mentioned in the article but it does not seem to work for me.
I've tried many solutions using the following code to test them:
private void button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(DoWork);
Thread t2 = new Thread(DoWork);
Thread t3 = new Thread(DoWork);
Thread t4 = new Thread(DoWork);
t1.Start("1");
t2.Start("2");
t3.Start("3");
t4.Start("4");
}
public static void DoWork(object a)
{
while (true)
{
lock (key) {
while (block)
{
Monitor.Wait(key);
block = true;
}
}
Trace.Write(a.ToString() + "|");
}
}
private void button2_Click(object sender, EventArgs e)
{
for (int i = 1; i < 2000; i++)
{
Trace.WriteLine("-");
lock (key)
{
block = false;
Monitor.PulseAll(key);
}
}
}
(this is the implementation from the document that was linked)
what it shows is something along the lines of :
1234-
-
-
12-
123-
1234-
what I want it to show is this:
1234-
1234-
1234-
1234-
1234-
meaning that when the notify is sent ( the - in the printout) all threads wake up
did I just understand the article wrong?
|
|
|
|
|
That's nearly there, but this won't work at all:
Cruces wrote:
while (block)
{
Monitor.Wait(key);
block = true;
}
One of the advantages of Wait/Pulse is you can have different conditions for each waiting thread:
lock( key )
while( !myHandler.hasAnswer(myQuestionCode) )
Monitor.Wait( key );
If you also need your UI thread to wait for the worker threads to run, then you're going to have to synchronize that separately.
Normally, you just want to put a loop around the condition so the worker thread keeps going when there is work to do and only Wait 's when there is nothing ready to do.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thanks a lot mate I hadn't thought of trying that, I'll try implementing that over the weekend and testing it with as many clients as possible to see how it goes, again thank you very much for the help
|
|
|
|
|
Import data in excel with OPENROWSET
but i want set formats of cell like date(dd-MMM-yy),number
and text but all should be done by sql server
or in predefine excel sheet
but not through excel objects.
|
|
|
|
|
Hello,
is there a possibility to draw controls like in a graphic-program in layers? My problem is, that i want to have for example a panel with a transparent-png-file as background drawed over a button, but the button should be visible if there's a transparency in the png file. - and maybe for every control inside my form?
Is there any way to draw them without having the restrictions of the Panels and Controls and so on?
Many thanks
|
|
|
|
|
Hallo
I'm developing a C# .Net2 application built around a DGV.
Is it possible to have the DGV data refreshed automatically as the underlying data change?
I mean, is there any way to automatically PUSH changes from database to the grid (or the BindingSource)?
Thanks in advance
|
|
|
|
|
I have a no idea what a DGV is. You can create triggers in your DB, and I believe in newer versions of SQL Server, they can run .NET code.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
I think you ment DataGridView Control by dgv??
If you are using dataadapter object you can easily reflect changes to db. All you need is a timer control, and calling update method of this adapter in Tick event of timer.
|
|
|
|
|
Well, that's not very efficient, I assume that's what he's trying to avoid.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
By looking the question that he asked i think this method is adequate for this question.
I agree that this is not the best way and not very efficient but anything not mentioned about performance, or other ways that already tried.
|
|
|
|
|
hallo and thanks for the replies.
By DGV I mean the DataGridView control, but it could be any control showing data.
Actually I was trying to avoid a timer, but the underlying database is a SQL2000 thus having no possibility of CLR into T-SQL.
As far as I understand, the logic flow would be:
data changes > trigger that broadcasts/sends a message > client application "listening" > performs a grid refresh
|
|
|
|
|
Sorry I thought you were trying to send data to database. Now i understood you are trying to refresh grid from database.
data changes > trigger that broadcasts/sends a message > client application "listening" > performs a grid refresh is the best way i think
|
|
|
|
|
Yes, your outdated database probably means you have no choice but to use a timer. Unless, you write some sort of external app that broadcasts to all users somehow, when data is changed. It depends on how many users you have, how often data changes, etc.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
I have maximum 15 concurrent users (on that particular table in the db) with less than 10 changes per minute: hence, I think I'll go for a timer.
Only, I thought that newer .NET brought something better on the argument...
Thanks anyway
|
|
|
|