|
Yep I know I'm having a dumb blonde moment!
To be honest I know about accesing controls from other classes etc. But my difficulty with this one is I am dealing with threading, I have a UI that runs on one thread, when a button is pressed, it creates a new thread and goes of and executes another (labour intensive task), within the running task I want to make callbacks to the UI to update a listbox with messages.
I have setup the delegates fine but my trouble is in accessing the listbox control to call the begininvoke method e.g.
lstLogger.BeginInvoke(delegate, object)
To call this from the other class I would need to create a new instance of the Logger class which contains the listbox lstLogger, but I don't want to do this, I want to invoke a method on a seperate thread that is already running.
It's driving me nuts, all the examples I can find do it from within the same class, which is easy to set up as I have access to the lstLogger control.
Hope that explains a bit more and doens't make me sound so dumb!
Regards
Paul
|
|
|
|
|
oceanexplorer wrote: But my difficulty with this one is I am dealing with threading
Slow down and relax. The threading issue is completely separate from the object access issue. You actually seem to be aware of that but perhaps your frustration is not allowing you to realize it.
oceanexplorer wrote: To be honest I know about accesing controls from other classes etc.
Then start there. Don't think about the thread issue and solve the access problem first. Once you have access to the object then go back to solving the thread issue.
#1 Get access to the object I need.
#2 Now I am in another thread so I need to Invoke the object rather than just call it's method.
Also take a look at this[^] and see if it helps.
|
|
|
|
|
Thanks Mike,
It's friday, I'm bored of thinking and want to go home!!
In order to gain access to the object, I tired making it public rather than private but I still cannot see it, public static allows me to see it but then that causes hassle in construction and it won't build.
I would normally create public property accessors but I need access to the object in order to invoke methods.
Any ideas?
Thanks again
Paul
|
|
|
|
|
oceanexplorer wrote: It's friday, I'm bored of thinking and want to go home!!
oceanexplorer wrote: Any ideas?
Sounds like you need to go home and come back to this with a fresh mind on Monday. If you put any efforts in over the weekend I would suggest limiting that to reading some design material, perhaps patterns or something. Nothing directly related to your project.
|
|
|
|
|
Does anyone know how to get to the HTTP headers when executing a web service method? Specifically I'm interested in checking the IP Address of the client requesting a web page (REMOTE_ADDR).
Thanks
Jerry
|
|
|
|
|
Found it thanks. For the record:
string uha = Context.Request.UserHostAddress.ToString();
|
|
|
|
|
Hello,
I'm trying to execute a script from a file and I noticed I can only do it using a FbBatchExecution. The problem is I can't find how to add a transaction to the batch execution. Can i use the FbBatchExecution with a command and specify a transaction to that command? Can you please point me in some directions? I'm confused
Thank you!
drinking beer is fun
|
|
|
|
|
Ok inseatd of adding sql statements to a FbBatchExecution I just executed each statement using a command it looks ugly, but it works
<br />
foreach (string cmd in script.Results)<br />
{<br />
fbCommand.CommandText = cmd;<br />
fbCommand.ExecuteNonQuery();<br />
}<br />
drinking beer is fun
|
|
|
|
|
Hi,
I have created a windows form the contains a button and a list box. When the button is clicked it fires the following code,
<br />
Migration.Begin();<br />
thread = new Thread(new ThreadStart(Migration.Begin));<br />
thread.Start();<br />
Migration is a seperate class that is executed on a new thread as it will be a labour intensive task and I want the UI to remain usable.
However within the migration module I want to be able to call back to the thread that created the UI and have it append a message to the listbox.
So intially in the UI class I added a delgate which called a seperate function to add a line to the listbox:
<br />
public static LoggerListDelegate loggerListDelegate; <br />
<br />
public Logger()<br />
{<br />
InitializeComponent();<br />
loggerListDelegate = new LoggerListDelegate(AddLine);<br />
}<br />
And the function addline was as follows:
<br />
public void AddLine(string line)<br />
{<br />
lstLogger.Items.Add(DateTime.Now.ToLongTimeString() + ": " + line); <br />
}<br />
I then called this from the Migration using the following:
<br />
Logger.loggerListDelegate.BeginInvoke("******Migration has begun******",null,null);<br />
This however is giving me an issue that it is not thread safe as I am trying to invoke on different thread the list was created on.
So my understanding is that I would really need to call the Listbox invoke method, e.g.
lstLogger.Invoke(delegate, objects)
If that's correct how do I access the lstLogger from my Migration class in order to invoke it?
If this is not right how do I go about making it threadsafe?
Many many many thanks
Paul
|
|
|
|
|
There is a huge difference between Control.BeginInvoke and Delegate.BeginInvoke: Delegate.BeginInvoke will call the method on a thread from the ThreadPool, but Control.BeginInvoke will call the method on the thread that runs the message loop for that control (which usually is the main thread). Since the main thread created the form and the controls on it, it is the only thread allowed to access the controls. So you have to use Control.BeginInvoke (lstLogger.BeginInvoke or this.BeginInvoke if "this" is a form).
|
|
|
|
|
Hey Daniel,
thanks for a great response, that makes it a lot clearer for me, I was having major mental block!
I have just one further question, what is the best way for me to access the lstLogger control from a seperate class? I tried changing the control to public static but the obviously is incorrect.
I could in the migration class create a new instance of the Logger class and call it-
Logger log = new Logger();
log.lstLogger.BeginInvoke(delegate, objects)
But that won't work properly, what are you thoughts?
Thanks again
Paul
|
|
|
|
|
Can anyone still answer this?
|
|
|
|
|
Hi
I would do the following:
let's say you have 2 classes the worker class Worker and the Main Form frmMain
public class Worker
{
public event EventHandler MyInterThreadEvent;
public Worker()
{
}
protected virtual void onMyInterThreadEvent(EventArgs e)
{
if (MyInterThreadEvent != null)
{
MyInterThreadEvent(this, e);
}
}
public void ThreadMethod()
{
do
{
onMyInterThreadEvent(EventArgs.Empty);
}while(something);
}
}
public class frmMain:Form
{
private Worker MyPersonalSlave;
public frmMain()
{
InitializeComponent();
MyPersonalSlave = new Worker();
MyPersonalSlave.MyInterThreadEvent += new EventHandler(MyPersonalSlave_MyInterThreadEvent);
}
private void MyPersonalSlave_MyInterThreadEvent(object sender, EventArgs e)
{
if (!InvokeRequired)
{
}
else
{
Invoke(new EventHandler(MyPersonalSlave_MyInterThreadEvent),new object[]{sender,e});
}
}
}
Greets
m@u
|
|
|
|
|
HI,
l like to know how can i use the xp icons
from where do i get them
THANKS
Have Fun
Never forget it
|
|
|
|
|
You can get the Microsoft Common Icons here[^].
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
THANKS
but is'nt there a way to get out of the the system (XP)?
i cant find them with SEARCH
-- modified at 11:11 Friday 2nd November, 2007
Like getting out of shell32.dll???
Have Fun
Never forget it
|
|
|
|
|
I have a treeview where I add nodes. Before I add a new node to the treeview I want to check if a node with the same name allready exist, how can I do that in an easy way ?
KRR
|
|
|
|
|
<br />
if (!treeview1.Nodes.Contains(nodetoadd))<br />
{<br />
}<br />
else<br />
{<br />
}<br />
|
|
|
|
|
That would not work, consider
<br />
TreeNode tn1 = new TreeNode("item1");<br />
TreeNode tn2 = new TreeNode("item1");<br />
treeView1.Nodes.Add(tn1);<br />
if(!treeView1.Nodes.Contains(tn2)){<br />
treeView1.Nodes.Add(tn2);<br />
}else{<br />
}<br />
treenode tn2 has the text as tn1, but it is not THE same treenode.
but you could use the Name property and ContainsKey method
<br />
TreeNode tn1 = new TreeNode("item1");<br />
tn1.Name = "item1";<br />
<br />
TreeNode tn2 = new TreeNode("item1");<br />
tn2.Name ="item1";<br />
treeView1.Nodes.Add(tn1);<br />
<br />
if(!treeView1.Nodes.ContainsKey(tn2.Name )){<br />
treeView1.Nodes.Add(tn2);<br />
}else{<br />
MessageBox.Show("node exists"); <br />
}<br />
<br />
If it' stuck, DO NOT pull harder!
|
|
|
|
|
Thanks.
The last example was just the one I was looking for. It works great !
KRR
|
|
|
|
|
Is it possible to use two or more kinds of format for String.Format()?
For example, If I want to display the 1.452677e-12 as 1.45e-12, how to do?
thanks
|
|
|
|
|
Sure, why not... There are many ways to do that.
simplest way...
string result = String.Format();
result = String.Format();
depending on the formatting you are doing, you could also do
.ToString().ToString() and use the standard IFormatProviders.
Hogan
|
|
|
|
|
Hi,
can Any one tell me how to convert mm/dd/yy to dd/mm/yy format.....
Arif patel
|
|
|
|
|
Arif patel wrote: can Any one tell me how to convert mm/dd/yy to dd/mm/yy format.....
Sure, use .ToString("dd/mm/yy"); .
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi pete,
I have wrote like this
Label2.Text=DateTime.Now.ToString("dd/mm/yy");
But i am getting like this 02/27/07 which is wrong...plz reply me..
Arif patel
|
|
|
|