Can I put a thread on a mouse event handler?
Calls_Calls.MouseUp += new MouseEventHandler(Calls_Calls_MouseUp);
How to add a thread over this?
I would set up the event handler in the same way, but in the Calls_Calls_MouseUp method you can launch a thread to do the work:
private void Calls_Calls_MouseUp(object sender, MouseEventArgs e)
{
ThreadPool.QueueUserWorkItem(state => {
// do the work here
});
}
However, I typically try to have my event handlers as unaware as possible, just calling some other method, often based on some condition:
private void Calls_Calls_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DoSomething();
}
}
private void DoSomething()
{
ThreadPool.QueueUserWorkItem(state => {
// do the work here
});
}
This gives you the ability to trigger the exact same behavior from something else than the MouseUp event on a certain control (so that you can have the same behavior on a menu item, a toolbar button and perhaps a regular command button). It may also open up the possibility to have unit tests on the functionality (even though that is somewhat trickier with asynchronous code).
you can also use BackgroundWorker for this in case you require any updation on the UI for progress and completion.
Calls_Calls.MouseUp+= new MouseEventHandler(delegate(System.Object o, System.EventArgs e) { new Thread(Calls_Call_MouseUp).Start(); });
should work for you. If you get brackets errors, fix them since I handwrote the code :) :)
Related
I Have a requirement to add a OnClick action to Shape object in Micro Soft Power Point Addin for Office 2010 and above which is built using C# language. There are events like
SlideSelectionChanged
WindowBeforeRightClick
Which doesn't work as needed, Right Click Event doesn't even work on the Shape Objects.
Is there a way to subscribe to such type of events, I would not prefer to use MACRO however if that is inevitable I will use it.
This solution would work.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WindowSelectionChange += OnWindowSelectionChanged;
}
void OnWindowSelectionChanged(PowerPoint.Selection Sel)
{
if (Sel.Type == PowerPoint.PpSelectionType.ppSelectionShapes)
{
PowerPoint.ShapeRange shapeRange = Sel.ShapeRange;
//Do some work
}
}
private void ThisAddIn_ShutDown(object sender, System.EventArgs e)
{
this.Application.WindowSelectionChange -= OnWindowSelectionChanged;
}
It's Good to have some flag to make sure you are doing the needful only on desired Shape Objects by setting some flag by using AltText like
if (Sel.ShapeRange.AlternativeText.Contains("SomeFlag"))
{
//Do some thing
}
I'm doing this custom control, for selecting letters from a focusable Panel.
Each letter is selected on MouseDown event.
What I've been trying to do is to create an only public event, (and thus know when a letter, any, has been clicked), let's say LetterClick , which is reached with any call to Letters_MouseDown. With this code, the event is reached form client, but the application hangs.
public event MouseEventHandler LetterClick;
private void LettersPanel_Load(object sender, EventArgs e)
{
foreach (var letter in collection)
{
// get all labels and suscribe each one to a single private event
letter.MouseDown += new MouseEventHandler(Letters_MouseDown);
}
// public event
this.LetterClick += Letters_MouseDown;
}
private void Letters_MouseDown(object sender, MouseEventArgs e)
{
// ... container get focus, draw selected effect image, formatting,
// set Letter property, etc...
if (LetterClick != null)
{
LetterClick(sender, e);
}
}
protected virtual void OnLetterClick(object sender, MouseEventArgs e)
{
MouseEventHandler handler = LetterClick;
if (handler != null)
{
handler(sender, e);
}
}
From client:
lettersPanel1.LetterClick += new MouseEventHandler(lettersPanel1_LetterClick);
private void lettersPanel1_LetterClick(object sender, MouseDownEventArgs e)
{
string letterSelected = myCustomControl1.Letter;
}
I think there is some kind of infinte looping here, but I'm pretty inexperienced about events and delegates, plus the MSDN documentation is not very clear for me yet.
If anyone can give me some help I'll be very thankful.
This line is causing the error -
// public event
this.LetterClick += Letters_MouseDown;
It is subscribing to the event and then again calling itself recursively -
if (LetterClick != null)
{
LetterClick(sender, e);
}
Remove the first line and it should be ok -
private void LettersPanel_Load(object sender, EventArgs e)
{
foreach (var letter in collection)
{
// get all labels and suscribe each one to a single private event
letter.MouseDown += new MouseEventHandler(Letters_MouseDown);
}
// public event
//this.LetterClick += Letters_MouseDown; //remove this line
}
NOTE: It is not a good idea to call the handlers directly, because it will hang the calling thread. So if the calling thread is the UI thread (for example a Click Event or a Load Event), it will be hanged until the process is completed.
A more better way would be to use async and await.
EDIT:
MSDN has a very good article on async and await. Try this link - Walkthrough: Accessing the Web by Using Async and Await (C# and Visual Basic). But it is always possible to get intimidated by the feature easily, so make sure you read some good articles on async and await when you have time. Here is a good one -
Best Practices in Asynchronous Programming
Letters_MouseDown is a handler on LetterClick, then inside that method you invoke LetterClick again.
That's the infinite loop.
private void AddMyScrollEventHandlers()
{
VScrollBar vScrollBar1 = new VScrollBar();
}
private void button1_Click(object sender, EventArgs e)
{
while (true)
{
if (vScrollBar1.Value + 1 < vScrollBar1.Maximum)
{
vScrollBar1.Value = vScrollBar1.Value + 1;
label1.Text = vScrollBar1.Value.ToString();
}
else
{
break;
}
System.Threading.Thread.Sleep(200);
}
}
private void button2_Click(object sender, EventArgs e)
{
// vScrollBar1.Scroll
}
I am new in C#. I was working on scroll. What I wanted here is, if anyone click button1 then scroll automatically move to the end and I wanted to show gradual value in label1. Also when someone click button2 scrolling stop.
Now the problem is label1 do not show gradual change in value. It shows value once when the scrolling stop.
Also when scrolling continue i,e when while loop is working I can not click on button2. Actually I can not click on the form even.
Someone please give me some idea how to do this.
This happens because the thread that is performing the task is busy, and it's the same thread that updates the UI. You can use a multithreading solution. Take a look at
BackgroundWorker
All the UI events run in the main thread of the application, so the application can only process one event at a time. When the application is processing an event, no other event will be processed.
Since you are doing a UI related work periodically, the best option is to use the Timer class:
Drop Timer from the toolbox into the form.
In the properties window, set the interval to 200.
Double click the timer object to create the Tick event handler.
Put this code in the newly created timer1_Tick method:
if (vScrollBar1.Value + 1 < vScrollBar1.Maximum)
{
vScrollBar1.Value = vScrollBar1.Value + 1;
label1.Text = vScrollBar1.Value.ToString();
}
else
{
timer1.Stop();
}
Change your methods as below:
private void AddMyScrollEventHandlers()
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
Now you're done.
I would recommend using BackgroundWorker control, as suggested by Agustin Meriles. However, one more important thing to note is that You should use Control.Invoke(...) method to update controls from another thread.
I've modified Your code, tested it in a sample application and it seems to work correctly.
First, add a new BackgroundWorker control to Your form and assign backgroundWorker1_DoWork to its DoWork event.
Then, You can use the code below:
private void button1_Click(object sender, EventArgs e)
{
//code from here is moved to BackgroundWorker control
backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//while (true)
//the condition directly in the while looks more clear to me
while (vScrollBar1.Value + 1 < vScrollBar1.Maximum)
{
//update controls using Invoke method and anonymous functions
vScrollBar1.Invoke((MethodInvoker)delegate() { vScrollBar1.Value += 1; });
label1.Invoke((MethodInvoker)delegate() { label1.Text = vScrollBar1.Value.ToString(); });
//when called inside BackgroundWorker, this sleeps the background thread,
//so UI should be responsive now
System.Threading.Thread.Sleep(200);
}
}
If You have any problems when using this code, please let me know.
Update
As mentioned in the comments, You could also use ProgressChanged event of the BackgroundWorker. It requires some more changes in the code, but is more suitable in this case. You can find some information about it here: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx.
If You are not going to add any other code with more processing in the while loop, You can also use Timer control, as suggested by MD.Unicorn in his answer.
I've a backgroundworker which take care of a timer in my application. This is the code:
protected override void OnLoad(EventArgs e)
{
// Inizializzo il backgroundworker
bgwTimer.WorkerReportsProgress = true;
bgwTimer.WorkerSupportsCancellation = true;
bgwTimer.DoWork += (bgwTimer_DoWork);
bgwTimer.RunWorkerCompleted +=(bgwTimer_RunWorkerCompleted);
bgwTimer.ProgressChanged += (bgwTimer_ProgressChanged);
}
void bgwTimer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
throw new NotImplementedException();
}
void bgwTimer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
throw new NotImplementedException();
}
Basically the event "ProgressChanged" is never fired and so I cannot update the status of a progressbar.
The event DoWork is linked to this method:
void bgwTimer_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;
if (bgwTimer.CancellationPending)
{
e.Cancel = true;
}
else
{
while (bgwTimer.IsBusy)
{
Thread.Sleep(1000);
bgwTimer.ReportProgress(i);
refreshTimer();
}
}
}
By my side the code looks good and it runs fine. As you can see the ReportProgress method is called but the event is not fired. Any hints?
UPDATE:
Whops! I found that the event "bgwTimer_ProgressChanged" is fired only if I run the RunWorkerAsync right after the declaration of event. Basically:
bgwTimer.ProgressChanged += (bgwTimer_ProgressChanged);
bgwTimer.RunWorkerAsync(); //this works!
Since I run the worker when the user press a button, the event is not triggered.
Here's the code of click event button:
private void btnNext_Click(object sender, EventArgs e)
{
this.TopMost = true;
btnNext.Enabled = false;
progressBar1.Step = 0;
if (_bgwTimer.IsBusy)
_bgwTimer.CancelAsync();
else
_bgwTimer.RunWorkerAsync();
}
Put a breakpoint, or a Debug.Print or System.Windows.Forms.Messagebox just before bgwTimer.ReportProgress(i), to verify that you're actually entering the while loop.
Note that the BackgroundWorker is not actually a timer; it's a wrapper for a thread that provides a threadsafe invoking layer for your user interface.
Your if (bgwTimer.CancellationPending) { } should be inside the while loop, not outside it. It will only get checked once in your current code.
Note that, if you're inside the DoWork event handler, then by definition you're running an asynchronous process, so IsBusy should always be true (according to the MSDN documentation), and therefore your while is an infinite loop. But check it with your debugger.
It's not raising the event because the value of i is always zero, which is helpfully undocumented but I found out the same thing when building a background worker a while back.
You forgot to start the worker. Add this line to your OnLoad() method:
bgwTimer.RunWorkerAsync();
In the DoWork-Method replace bgwTimer through ((BackgroundWorker)sender). Maybe this is the problem
I would like to use the following code in C# but I just can't seem to get out of it. I would like to terminate the application if the user presses a key or moves the rodent (aka mouse). Here is my code (no laughing!).
private void frmDots_KeyDown(object sender, KeyEventArgs e)
{
bgNotClicked = false;
Close();
}
private void frmDots_Click(object sender, EventArgs e)
{
bgNotClicked = false;
Close();
}
while (bgNotClicked)
{
// Clear the first element in our XY position. This is the reverse of the way I normally create the dots application
System.Drawing.Rectangle clearDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8);
// Create the black color and brush to clear dots
Color clearDotsColor = Color.Black;
SolidBrush clearDotsBrush = new SolidBrush(clearDotsColor);
// Finally clear the dot
e.Graphics.FillEllipse(clearDotsBrush, clearDots);
GetRandomPosition(iCounter);
// Fill the elements to display colors on the displays canvas
System.Drawing.Rectangle colorDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8);
// Create the color and brush to show dots
Color colorRandom = GetRandomColor();
SolidBrush colorBrush = new SolidBrush(colorRandom);
// Finally show the dot
e.Graphics.FillEllipse(colorBrush, colorDots);
Thread.Sleep(5);
iCounter++;
if (iCounter == 399)
{
iCounter = 0;
}
}
}
Your "busy waiting" strategy is poor design. Instead, you should use event handlers that are fired:
On keypress.
When the mouse is moved.
In either case, you can respond by terminating the application.
Edit:
After seeing your edit, this is definitely your problem. The issue is that your while loop blocks the main UI thread, so it never handles the Windows Messages which trigger your key press/mouse/etc handlers.
You have a couple of options - you can either move some of this onto a separate thread, do what I suggested below, or add a call to Application.DoEvents in your while loop. This would allow your event handlers to run, which would in turn set bgNotClicked = false;. Right now, that's never occurring because your UI is blocked entirely.
Original Post:
If you're doing this loop in your UI thread, you're going to need to rethink the design a bit.
Setting bgNotClicked = false; somewhere in an event handler will work, but only if your event handler is able to run. If you're doing the above code in the UI thread, it will block your UI thread indefinitely, preventing the event handler from firing.
I would recommend reworking this to be based off a timer (so it runs repeatedly on regular intervals), instead of locked into a while loop. This would allow your other UI events to fire between runs, and instead of setting bgNotClicked = false;, your event handler could just set the timer to be not enabled.
Your bgNotClicked variable needs to be set to false by your event handler for key-press.
If the rodent is moved by your mouse, you would need a similar mouse event handler.
The break keyword will terminate a loop. In this case, when you hit the case where you want to stop the loop, you would just use break;.
If you're looping like that you need to give the application a moment to process the events that you're hoping will cause the interruption. This is the job of the DoEvents method.
private bool WeAreDone = false;
private void DoingIt()
{
while (true)
{
Application.DoEvents();
if (WeAreDone)
{
break;
}
}
}
private void InterruptButton_Click(object sender, EventArgs e)
{
WeAreDone = true;
}
I think using a Timer fits the Windows event-driven model better than the busy wait while loop. You might try something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int iCounter = 0;
private void Draw()
{
// ....
}
private void timer1_Tick(object sender, EventArgs e)
{
Draw();
iCounter++;
if(iCounter == 399)
{
iCounter = 0;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 5;
timer1.Enabled = true;
}
private void Form1_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
Close();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
timer1.Enabled = false;
Close();
}
}
This does not seems to be the correct way. .Net Framework has provided you with the events to handle the KeyPress and MouseMove/Click actions. Why are you not using them?
Try moving the loop into a BackgroundWorker's DoWork event handler. Then your GUI will still be responsive and instead of that nasty variable, you can just call the CancelAsync method to stop the loop.
You can exit the loop using the break statement.
EDIT: OK, I take back the flag thing!
Use Environment.Exit(2); (or Environment.Exit(1) it really doesn't make a difference) to exit out of the application.
Exit While
...................