|
In .NET manual, AutoResetEvent.WaitOne() is
[C#]
public virtual bool WaitOne();
My question are :
1.In what case will "false" be returned?
2.Is it necessary to look at the return value of WaitOne?
Thnak you!
|
|
|
|
|
bool WaitOne() is true if the current instance receives a signal. It will never be false since it won't return if it is unsignaled.
|
|
|
|
|
Thank you for your reply.
I have also checked a lot these days, and come to the same conclusion
Would tell me if there are some difference between using AutoResetEvent' WaitOne/Set in forground-thread (started by Thread.Start() )and background thread(started by BeginInvoke()).
I have a problem that WaitOne() returned before it is Set() at another thread. As it happens not so frequently, I still don't know the reason.
I my app, except for the main thread, all threads are started by BeginInvoke.
Thnak you!
-- modified at 2:07 Thursday 30th March, 2006
|
|
|
|
|
Bob_Sun wrote: Would tell me if there are some difference between using AutoResetEvent' WaitOne/Set in forground-thread (started by Thread.Start() )and background thread(started by BeginInvoke()).
I believe there is no difference.
Anyway, you need to initialize the AutoResetEvent as unsignaled before using it:
AutoResetEvent are = new AutoResetEvent(false);
It also needs to be in the same scope as the thread waiting for the signal.
|
|
|
|
|
I am trying to access a dynamically created control ( a tabpage ) with the following code:
((ListView)this.tabControl1.TabPages[1].Controls[1]).Items.Add("HELLO");
I am getting an error:
Specified cast is not valid.
What am I doing wrong?
Thanks,
Jon
|
|
|
|
|
I guess that control is not a listview. You should really iterate through the controls collection for hte tab page and find the list view you want rather than relying on indexes that can change.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I have made a custom TabControl derived from Panel, custom TabPage derived form Panel, and TabPageCollection dervived from CollectionBase class. The CollectionEditor will successfully serialize the custom TabPages that are added. However when the custom TabControl is removed/deleted from a form the custom TabPage code that was added by the CollectionEditor is not removed from the forms InitalizeComponent().
When the CollectionEditor Remove button is used the serialized code is removed from InitializeCompnent() so I don't understand why it would not be removed when the custom TabControl is deleted.
I also found out that if the custom TabPage does not derive from anything then the code will be removed when the control is deleted.
So this is my situation, I am very confused and have asked this question on several forums with not effective responses.
Do I need to implement some type of TypeConverter or override some method that is derived from a WindowsForm control?
Any help or lead way will be greatly appreciated! I have been having no luck for the past week. Thank you very much for any help.
|
|
|
|
|
hi
i want to create a rectangle and move it from left to right ( x++ ) and rotate around it's center . how to do that ?
my code is here but i don't get true result :
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
Pen m_pen;
int X;
int Y;
int Width1;
int Height1;
int angle;
float f;
string path;
PointF _pointf;
Bitmap bm;
Graphics gr;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
Matrix mat = new Matrix();
public Form1()
{
InitializeComponent();
m_pen = new Pen(Color.Red,4);
X = 200;
Y = 100;
Width1 = 100;
Height1 = 100;
angle = 0;
path = @"CB047003.jpg";
bm = new Bitmap(path);
f = 250;
_pointf = new PointF(f,150);
this.SetStyle(ControlStyles.DoubleBuffer,true);
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
}
private void RedrawPicture(object sender, System.EventArgs e)
{
pictureBox1.CreateGraphics().Clear(pictureBox1.BackColor);
pictureBox1.Refresh();
gr = pictureBox1.CreateGraphics();
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBilinear;
SolidBrush brush = new SolidBrush(Color.Yellow);
mat.RotateAt(angle,_pointf);
gr.Transform = mat;
gr.DrawRectangle(m_pen,X,Y,Width1,Height1);
gr.FillRectangle(brush,X,Y,Width1,Height1);
label1.Text = "Position : "+angle.ToString();
gr.Dispose();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(X == pictureBox1.ClientSize.Width - 105)
{
timer1.Stop();
}
else
{
if(angle == 360)
angle = 0;
X += 5;
angle += 1;
f += 5;
RedrawPicture(this,new System.EventArgs());
}
}
|
|
|
|
|
Changing f won't change _pointf automatically.
---
b { font-weight: normal; }
|
|
|
|
|
Hello, may be some of you could help me. I have a winform with FormBorderStyle asigned to None (I need it in that way), and an AXWebBrowser with Dock asigned to Fill. My problem is that the MouseEnter event for the winform only work if I have a part of it visible, but in this case I must have the webbroser filling all the winform, so the event MouseEnter never work. And the webbrowser doen't have a MouseEnter event, so I can't call that event too. I need the event for enter and leaving.
I try:
((Control)this.browser).MouseEnter += new System.EventHandler(this.AlertWindow_MouseEnter);
But doesn't work !!!
Any ideas??
Thansk a lot.
|
|
|
|
|
You could try finding the coordinates of the form and then see if the mouse is within range(this is what MouseEnter does.)
|
|
|
|
|
I haven't try that, but I want to do it through events.
Thanks very much ExpertComing. If you have some other idea it will be welcome, thanks a lot.
|
|
|
|
|
Make your own event that does the MouseEnter event's job.
|
|
|
|
|
Cuold you give me an example of that please?
Thanks a lot Expertoming !!
|
|
|
|
|
Cuold you give me an example of that please?.
Thanks a lot Expertoming !!
|
|
|
|
|
I posted a similar question a while back but wasnt able to find a solution. The issue I'm seeing is bizzare and yet there is no documentation of this in .Net.
Background: A control has "Visible" property and "VisibleChanged" event. This even is fired whenever the control is no longer "Visible".
Problem: I get the VisibleChanged event when I programmatically set a value for the "Visible" property. However, I do NOT get the "VisibleChnaged" event when the control becomes invisible when the tab page it is on is no longer the selected tab page. When the control is no longer "displayed" (e.g. not visible implicitely) the "Visible" property returns "false" but there is no event.
Stpes:
1) Create a form -- Form1 with a TabControl (tabControl1) and two tab pages (tabPage1, tabPage2).
2) On the tabPage1, add a control (button - b1) and add a b1_VisibleChanged handler. In the b1_VisibleChange method, write the following: "this.Text = b1.Visible.ToString(); " .
3) Add another button on Form1, b2 on the form which is for toggeling the visible property and add the following code for the b2_Click handler: "b1.Visible = !b1.Visible; "
4) Add another button on Form1, b3 on the form which displays the value of the Visible property of button b1. Add the following code in the b3_Click handler: "MessageBox.Show(b1.Visible.ToString()); "
Testing:
Make sure tabPage1 is selected and button b1 is being displayed.
Click on b3: it will show "true" message.
Click on b2: it will hide b1. The Form1 title will display "False".
Click on b3: it will show "false" message.
Click on b2 again: it will show b1. The Form1 title will display "True".
Now switch to tabPage2.
Click on b3: it will show "false" message. However, the Form1 title is still "true" implying that "b1_VisibleChanged" was never triggered.
Is there a way to assure a consistant behavior for VisibleChnaged?
Thanks for your help / suggestions.
- Malhar
|
|
|
|
|
I'm writing a service that copies specific MS Office documents(.doc, .xls,.mdb) after any event triggers a file system watcher (Change, Deleted, Renamed, Changed) and logs those file operations to a xml file. In order to copy those files to a hidden folder located in the user's My Documents Folder I was using in my windows form testing environment
Environment.GetFolderPath(Environment.SpecialFolder.Personal) and it worked perfectly and in order to determine the logged user I was using WindowsIdentity.GetCurrent().Name and when I tryied using them in the Windows Service code none of them worked as expected, Environment.GetFolderPath(Environment.SpecialFolder.Personal) returns nothing at all and WindowsIdentity.GetCurrent().Name returns the account name that the windows service is using.
Is there any way in which I can accomplish the recolection of this information using perhaps
another class or some other way of calling the class?
Thanks in advance, Mario
|
|
|
|
|
Hi all.
Please help me out with this.
What i am doing is that, in a small form i am displaying the current time. To keep it updated, i am using a timer and calling Invalidate on the form, after every 1000 ms, to paint the surface clean for me.
Now the problem is that, the time does not display at all. It sometimes appears, but for such a short duration that it is hardly visible.
Here's what i am doing:
class MyClock
{
.....
public MyClock()
{
m_timer = new System.Windows.Forms.Timer();
m_timer.Interval = 1000;
m_timer.Tick += new EventHandler(Start);
m_timer.Start();
}
private static void Start(Object o, EventArgs e)
{
m_timer.Stop();
m_form.Invalidate(true);
m_time = DateTime.Now;
string time = m_time.Hour + ":" + m_time.Minute + ":" + m_time.Second;
Brush b = Brushes.White;
m_gFormGraphic.DrawString(time, new Font("Verdana", 15), b, new PointF(20, 40));
m_timer.Start();
}
.....
}
I tried adding handlers for Validating and Validated, where i set a boolean variable and display the time only when the form has been validated. But this doesnt work too.
Any pointers will be highly appreciated.
Thanks in advance.
*** Who said nothing is impossible? I have been doing it for a long time ***
-- modified at 13:56 Wednesday 29th March, 2006
|
|
|
|
|
I would recommend moving the painting to your form's Paint handler, which will (eventually) be invoked as a result of calling Invalidate() or at any other time Windows deems it necessary to repaint the form.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Hey, thanks buddy...
I'll try that and let you know.
Thanks a lot.
*** Who said nothing is impossible? I have been doing it for a long time ***
|
|
|
|
|
CoolASL wrote: It sometimes appears, but for such a short duration that it is hardly visible.
That's because you are not doing the drawing in the Paint event. Now you are just drawing it on the screen, and when the form is being redrawn because you invalidated it, the time that you drew is painted over.
Why are you drawing the time as graphics anyway? Put the string in a label, and it will draw itself. When you change the Text property of the label, it will also invalidate itself so that it's redrawn.
---
b { font-weight: normal; }
|
|
|
|
|
Ok, please refresh me on this.
I call Invalidate; that repaints my form. Then i use DrawString to draw my string on the form.
Both are seperate entities... so whats the catch ????
Can you please brief me on what happening that i am not able to understand?
Thanks.
*** Who said nothing is impossible? I have been doing it for a long time ***
|
|
|
|
|
No, the Invalidate call doesn't repaint the form. It only flags that the form needs repainting. The actual repaint can't be done until the method call has returned, so the system can call the Paint event for all controls in the form.
---
b { font-weight: normal; }
|
|
|
|
|
Hello, may be some of you could help me. I have a winform with FormBorderStyle asigned to None (I need it in that way), and an AXWebBrowser with Dock asigned to Fill. My problem is that the MouseEnter event for the winform only work if I have a part of it visible, but in this case I must have the webbroser filling all the winform, so the event MouseEnter never work. And the webbrowser doen't have a MouseEnter event, so I can't call that event too. I need the event for enter and leaving.
I try:
((Control)this.browser).MouseEnter += new System.EventHandler(this.AlertWindow_MouseEnter);
But doesn't work !!!
Any ideas??
Thansk a lot.
|
|
|
|
|
I am relatively inexperienced in using delegates and asynchronous methods. I read several articles, and then I developed my own code with a mission to improve the performance. Wow! I cannot believe the difference in speed! However, the asynch operation fails sometimes, despite of the fact that it works most of the time. I am really at a loss how to fix this sporadic and erratic behavior.
This is a web application developed with Visual Studio 2003 (.NET framework 1.1). The following C# code snippet shows that I use two delegates to provide a built-in way to call two mainframe DB2 stored procedures, SPX and SPY. I need to execute SPX and SPY many times, each time passing different parameters as per signature of the method. This code is in the server-side Business Logic layer (behind an ASP.NET page to service a client request of a long running report).
With those asynch methods shown in the example, the code compiles and runs incredibly fast when compared to the old-fashioned way of making one call at a time. I was very happy with the performance and the results from the asynch executions were correct.
However, I came across a really peculiar and sporadic error from mscorlib.dll with an error message “object reference not set to an instance of an object”. This error is totally incomprehensible to me. Here are the symptoms: (1) It seems to me the error only occurred sporadically or randomly (because the application runs fine most of the time). (2) Whenever the error occurred, the call stack pointed to one of the many EndInvoke statements (not necessarily a particular EndInvoke statement, but randomly one of the many EndInvoke statements).
I apologize for putting down so much writing and code snippet. I sincerely hope someone can shed some light on this mysterious error. Any help or suggestion is greatly appreciated!
Code Snippet:
public delegate string SPXCaller(int Division);
public delegate string[] SPYCaller(int TimePeriod, int CategoryType, int Division);
public class AsyncMethods
{
public AsyncMethods()
{
}
public string ExecSPX(int Division)
{
//This method contains code to exec DB2 stored procedure SPX
//SPX returns 1 output parameter as string
}
public string[] ExecSPY(int TimePeriod, int CategoryType, int Division)
{
//This method contains code to exec DB2 stored procedure SPY
//SPY returns 5 output parameters as string[5]
}
}
public class BusinessData
{
public DataSet GetData()
{
//Declares a new DataSet to hold all data
DataSet dsData = new DataSet();
//Initiate the Asynchronous calls to exec SPX
AsyncMethods Task1 = new AsyncMethods();
SPXCaller Task1Caller = new SPXCaller(Task1.ExecSPX);
IAsyncResult Result1 = Task1Caller.BeginInvoke(8,null,null);
AsyncMethods Task2 = new AsyncMethods();
SPXCaller Task2Caller = new SPXCaller(Task2.ExecSPX);
IAsyncResult Result2 = Task2Caller.BeginInvoke(10,null,null);
//Initiate the Asynchronous calls to exec SPY
AsyncMethods Task11T = new AsyncMethods();
SPYCaller Task11TCaller = new SPYCaller(Task11T.ExecSPY);
IAsyncResult Result11T = Task11TCaller.BeginInvoke(1,1,8,null,null);
AsyncMethods Task12T = new AsyncMethods();
SPYCaller Task12TCaller = new SPYCaller(Task12T.ExecSPY);
IAsyncResult Result12T = Task12TCaller.BeginInvoke(1,2,8,null,null);
AsyncMethods Task11M = new AsyncMethods();
SPYCaller Task11MCaller = new SPYCaller(Task11M.ExecSPY);
IAsyncResult Result11M = Task11MCaller.BeginInvoke(1,1,10,null,null);
AsyncMethods Task12M = new AsyncMethods();
SPYCaller Task12MCaller = new SPYCaller(Task12M.ExecSPY);
IAsyncResult Result12M = Task12MCaller.BeginInvoke(1,2,10,null,null);
AsyncMethods Task21T = new AsyncMethods();
SPYCaller Task21TCaller = new SPYCaller(Task21T.ExecSPY);
IAsyncResult Result21T = Task21TCaller.BeginInvoke(2,1,8,null,null);
AsyncMethods Task22T = new AsyncMethods();
SPYCaller Task22TCaller = new SPYCaller(Task22T.ExecSPY);
IAsyncResult Result22T = Task22TCaller.BeginInvoke(2,2,8,null,null);
AsyncMethods Task23T = new AsyncMethods();
SPYCaller Task23TCaller = new SPYCaller(Task23T.ExecSPY);
IAsyncResult Result23T = Task23TCaller.BeginInvoke(2,3,8,null,null);
AsyncMethods Task21M = new AsyncMethods();
SPYCaller Task21MCaller = new SPYCaller(Task21M.ExecSPY);
IAsyncResult Result21M = Task21MCaller.BeginInvoke(2,1,10,null,null);
AsyncMethods Task22M = new AsyncMethods();
SPYCaller Task22MCaller = new SPYCaller(Task22M.ExecSPY);
IAsyncResult Result22M = Task22MCaller.BeginInvoke(2,2,10,null,null);
AsyncMethods Task23M = new AsyncMethods();
SPYCaller Task23MCaller = new SPYCaller(Task23M.ExecSPY);
IAsyncResult Result23M = Task23MCaller.BeginInvoke(2,3,10,null,null);
//All calls to BeginInvoke return right away.
//We can immediately move on to do another task.
//The following independent, long running task retrieves PO data
//while waiting for all asynch executions (DB2 store proc) to finish.
DataTable dtPOData = GetPOData();
//Now, retrieve the return values by calling the EndInvoke method.
//Retrieve results from SPXCaller
string Ret1 = Task1Caller.EndInvoke(Result1);
string Ret2 = Task2Caller.EndInvoke(Result2);
//Retrieve results from SPYCaller
string[] Ret11T = Task11TCaller.EndInvoke(Result11T);
string[] Ret12T = Task12TCaller.EndInvoke(Result12T);
string[] Ret11M = Task11MCaller.EndInvoke(Result11M);
string[] Ret12M = Task12MCaller.EndInvoke(Result12M);
string[] Ret21T = Task21TCaller.EndInvoke(Result21T);
string[] Ret22T = Task22TCaller.EndInvoke(Result22T);
string[] Ret23T = Task23TCaller.EndInvoke(Result23T);
string[] Ret21M = Task21MCaller.EndInvoke(Result21M);
string[] Ret22M = Task22MCaller.EndInvoke(Result22M);
string[] Ret23M = Task23MCaller.EndInvoke(Result23M);
//Some source code omitted here. The code packs all results
//into a dataset dsData with multiple datatables.
return dsData;
}
}
swanmynova
|
|
|
|
|