|
Thanks .. I wonder if this is the cause of the problems I was having. Thanks for your input.
- Malhar
|
|
|
|
|
Hi,
I posted this message earlier and some one posted a link to an article. It helped, but still I am confused how to do it in my scenario.
I have a HomeForm (main form which has main()). In one of its meathod I have
<br />
PrimeInfoThread pTh = new PrimeInfoThread(this.pictureBox1,this.panel4,this.button1,this.button3,this.SLabel);<br />
Thread t = new Thread(new ThreadStart(pTh.ThreadProc));<br />
t.Start();
PrimeInfoThread is another class as follows
public class PrimeInfoThread<br />
{<br />
private System.Windows.Forms.PictureBox hPic ;<br />
private System.Windows.Forms.Panel hPan ;<br />
private System.Windows.Forms.Button but1 ;<br />
private System.Windows.Forms.Button but2 ;<br />
private System.Windows.Forms.Label slab ;<br />
<br />
public PrimeInfoThread(System.Windows.Forms.PictureBox hP,<br />
System.Windows.Forms.Panel hpanel,<br />
System.Windows.Forms.Button b1,<br />
System.Windows.Forms.Button b2,<br />
System.Windows.Forms.Label Sl) <br />
{<br />
hPic = hP;<br />
hPan = hpanel;<br />
but1 = b1;<br />
but2 = b2;<br />
slab = Sl;<br />
}<br />
<br />
public void ThreadProc() <br />
{<br />
EnumGetEDID InitEnumObj = new EnumGetEDID();<br />
InitEnumObj.AppInitEnum(slab); <br />
hPic.Hide();<br />
hPan.Hide();<br />
but1.Show();<br />
but2.Show();<br />
<br />
}<br />
<br />
}
In threadProc I call another class EnumGetEDID and call AppInitEnum(slab) where I pass the label. Inside AppInitEnum() I do 'slab.Text' to set the text values.
Now how do I make this Thread safe. Can anyone show it in my code.
Thanks a lot.
|
|
|
|
|
It seems to me that you're making this more complicated than it needs to be with the use of an entirely seperate class (PrimeInfoThread ) to run your other thread. Why don't you just have a method in your HomeForm class called ThreadProc() and then reference the UI components (labels, buttons, etc...) without passing them to another object. You can use the ParameterizedThreadStart delegate to pass the label 'slab ' - although it might make more sense to just pass the string value of its Text property if thats the only reason you need it... If you do that, then the following code should make it thread safe...
This goes in the routine that starts the thread:
Thread t = new Thread(new ParameterizedThreadStart(ThreadProc));
t.Start(this.SLabel); Then this in HomeForm :
private void ThreadProc(object data)
{
EnumGetEDID InitEnumObj = new EnumGetEDID();
InitEnumObj.AppInitEnum((Label)data);
UpdateUI();
}
private delegate void UpdateUIDelegate();
private void UpdateUI()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new UpdateUIDelegate(UpdateUI));
return;
}
pictureBox1.Hide();
panel4.Hide();
button1.Show();
button3.Show();
} Hope this helps
Oh... forgot to say that this uses .Net Framework v2, since the ParameterizedThreadStart delegate isn't available in v1.1
Cheers,
Will H
-- modified at 15:41 Thursday 2nd February, 2006
|
|
|
|
|
If you use the 2.0, try to use the BackgroundWorker, it gives the abilty to update without the cross-thread exception.
|
|
|
|
|
Thanks. I've only recently started using 2.0 in the last few days. I'll definitely look into that one.
Cheers,
Will H
|
|
|
|
|
I just gave the BackgroundWorker class a whirl, and I don't think it does allow you to update the UI from a seperate thread. Maybe I miss-read your meaning, but I took the C# code from the example in the MSDN library (here[^]), then I added the line this.Text = percentComplete + "%"; just after worker.ReportProgress(percentComplete); in the ComputeFibonacci method (the one that's running on the seperate thread), and this caused a System.InvalidOperationException - a cross thread exception
Cheers,
Will H
|
|
|
|
|
You can only update UI controls on the thread that created them. Property updates and method calls on those controls must be marshalled back onto the UI thread. Lookup Control.Invoke for more information.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I know that. In this[^] thread I tried to explain that, but was told by eligazit that the BackgroundWorker class could let you get around this.
Cheers,
Will H
|
|
|
|
|
It doesn't "get around it". It just fire an event on the correct thread. It insulates you from the details of getting code that modifies a UI control to run on the correct thread.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
To update GUI with the BackgroundWorker, you need to do the following steps:
1. When creating the wroker:
public void InitWorker()
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
}
Note that I've creating new event handler to update GUI, when ever I want to set something on the GUI, I should call the ReportProgress(...) method of the worker:
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// Some long operation
// .....
Thread.Sleep(2000);
// Update GUI
worker.ReportProgress(30);
// Some long operation
// .....
Thread.Sleep(2000);
// Update GUI
worker.ReportProgress(60);
// Some long operation
// .....
Thread.Sleep(2000);
// Update GUI
worker.ReportProgress(90);
}
And, on the ReportProgress handler, I can update the GUI as I want, this is beacuse the worker handles moving the OS the the right thread so it is not my problem...
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Label label;
label.Text = e.ProgressPercentage + "%";
}
Note that on the ReportProgress you can send an object that will be set on the ProgressChangedEventArgs (State), in this property you can set all the values you want to update on the GUI (other than the precentage).
|
|
|
|
|
Ah... i see. Thanks
Cheers,
Will H
|
|
|
|
|
I have a complete API all in vb.net.
I need to view all code and run all code in visual studio express edition c#
How do i do this?
please tell me this is possible (it has got to be)
|
|
|
|
|
|
|
|
Hi,
Easy question here. I found this online, but (I'm still new to programming) I'm not sure how to use it.
protected override CreateParams CreateParams<br />
{<br />
get<br />
{<br />
CreateParams cp = base.CreateParams;<br />
cp.ExStyle |= 0x00000020;
return cp;<br />
}<br />
}
Do I create a new class for this code? Either way, how and where do I say that I want a label (not all labels) to be this way.
Thanks so much!!!
Mel
|
|
|
|
|
Hi again,
Ok, I created a class and put the code in it.
public class TransparentControl : Label<br />
{<br />
protected override CreateParams CreateParams<br />
{<br />
get<br />
{<br />
CreateParams cp = base.CreateParams;<br />
cp.ExStyle |= 0x00000020;
return cp;<br />
}<br />
}<br />
}
Back in the normal form, I made the label private TransparentControl label7; and changed the appropriate line in InitializeComponent() this.label7 = new HCAUT.TransparentControl(); . I also made the backcolor of the label Transparent. But when I run everything, the label's backcolor still isn't transparent. Can anyone see what I'm doing wrong?
Thanks again,
Mel
|
|
|
|
|
hi melanieab!
i think if you inherited the Label, it already supports transparency.
try to remove the override property you've added.
if still doesn't allow transparency, add this line of code on the constructor of your control.
SetStyle(ControlStyles.SupportsTransparentBackColor, true)
hope that helps!
microsoc
|
|
|
|
|
hi all, Im using Microsoft Data Application Block for this.
Im trying to execute a sql procedure that inserts a row. I need
to get the identity field (eventID) returned back to me.
Ive tried several ways but it wont work. Anyone have a complete
example written in c# 1.0 for sql server 8.0, with c# code and
stored procedure?
Really appreciate all the help.
thanks
Moazzam
|
|
|
|
|
Why don't you show us your code and we can show you where to put it right?
ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
|
|
|
|
|
Assuming your table contains an Identity column ..
create procedure EmployeeCreate (@name varchar(20), @dept varchar(20), @employeeid numeric out)<br />
as<br />
begin<br />
insert into Emplyee (Name, Department) values (@name, @dept)<br />
select @employeeid = @@identity<br />
end
So when you execute your SQL command from .net and pass parameters, pass a parameter for "@employeeid". You wont need to set a value for this SqlParameter, but you'll have to set the correct Direction.
- Malhar
|
|
|
|
|
Hey guys, could anyone please tell me from where i can get the windows default bitmaps for standard toolbar buttons like 'cut', 'copy', 'print', 'save',etc..... in my C# forms
|
|
|
|
|
c:\program files\vs.net\common7\graphics\...
|
|
|
|
|
If you make a toolstrip and in its options choose to embedd standard items, then the cut copy save etc appear with icons
You could search for the strings they use to get the icons in the designer file
|
|
|
|
|
Our main application is written in vb 6.0. The latest module for this product is written in .net. To accomplish this our .net module expose a COM interface to start and stop the .net module from the vb 6.0 code base.
Furthermore the module needs to receive data from the vb application during the session. Otherwise the connection between both is useless. At the moment the .net module is a Inproc COM thing which runs in the same process as the vb app does.
At first glance the problem seemed just to be that the Tab key behaved strange. A few tests later we saw that the .net module opens a MDI Form but didn't create a message loop (Application.Run). A few coffees later we fixed that part and the .net module main window was finally running inside a Application.Run(new MdiMainForm()) call. Guess what, the tab problem was fixed...
I assume that the vb application message loop wasn't suitable for the .net part of the process I guess it's very strange to have such a constelation inside one process, right?
After seeking codeproject I know that the best solution would be to let the module run as an exe in his very own process, OutOfProcess as a COM Server or use one of the IPC solutions (Named Pipe, Remoting WM Messages etc..)
This seems all to be very compilcated, all I want is to start/control/stop our .net module by the vb application, any ideas?
myMsg.BehindDaKeys = "Jerry Maguire";
|
|
|
|