473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Forms.Timer start & Threads

I've got an incrementTimer()-Function, which is called on mous click.
It increments Windows.Forms.Timer interval and starts the timer... No
problem so far.

Now I want to do the same when IR-remote button is pushed. Therefore
I've got a component in my project, which fires an event when user
pushes the button on his control. No problem so far either.

When I get the event from the remote, I start incrementTimer-Function
in exactly the same way as when mouse button is pushed (just calling
incrementTimer())

The first problem was, that an update to textBox.Text inside
incrementTimer() wasn't threadsafe anymore, but I solved this
following msdn instructions exactly:

// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void setButtonTextCallback(string text);

// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// setButtonTextCallback and calls itself asynchronously using
the
// Invoke method.
//
// If the calling thread is the same as the thread that
created
// the TextBox control, the Text property is set directly.
private void setButtonText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.button.InvokeRequired)
{
setButtonTextCallback d = new
setButtonTextCallback(setButtonText);
this.Invoke(d, new object[] { text });
}
else
{
this.button.Text = text;
}
}

the text updates perfectly, but my problem is that I do not receive
any Timer_Tick events anymore, although there is a call to
timer.Start() inside incrementTimer() and with mouse click it works
perfectly.

Very upset by this... please help

Feb 7 '07 #1
2 1830
Found out how to solve this!

You (actually I :) have to use System.Timers.Timer instead of
System.Windows.Forms.Timer (the second one manages only single-
threaded applications)

look here: http://msdn2.microsoft.com/en-us/library/tb9yt5e6.aspx

programatically it almost doesn't make any difference

in VisualStudio you can add System.Timers.Timer to your toolbar by
clicking Add/Remove Toolbox Items on the Tools menu

hope it healps someone

Feb 7 '07 #2
Hi Anatoli,

The safest bet is to use System.Threading.Timer as the System.Timers.Timer
may stop firing events in windows services. Only use Forms.Timer for
simple events where you don't need all the ticks (if the tick is unable to
reach your event handler it will get eaten by windows).
On Wed, 07 Feb 2007 12:09:39 +0100, Anatoli
<an************@googlemail.comwrote:
Found out how to solve this!

You (actually I :) have to use System.Timers.Timer instead of
System.Windows.Forms.Timer (the second one manages only single-
threaded applications)

look here: http://msdn2.microsoft.com/en-us/library/tb9yt5e6.aspx

programatically it almost doesn't make any difference

in VisualStudio you can add System.Timers.Timer to your toolbar by
clicking Add/Remove Toolbox Items on the Tools menu

hope it healps someone


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andy B | last post by:
Hi, I am very new to JavaScript and my first assignment is to create a simple game. Below is my code which works fine in IE, but not in any other browser (Opera, Netscape, Mozilla). It has no form...
3
by: Scott | last post by:
I have written windows applications using MFC for several years and have frequently used MFC techniques. Now I'm moving to C# .NET WinForm. Mostly C# books describes C# language (sometimes...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
13
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
5
by: Jakub Moskal | last post by:
Hi, I want to write a benchmark that will measure performance of several different algorithms for the same problem. There is a set time bound though, the algorithm cannot run longer than n...
5
by: John A. Bailo | last post by:
From a Windows service (NET 2.0) I want to launch serveral threads in a for loop that invokes a method using: new Thread(delegate() { myMethod(248);}).Start(); Will those threads stay...
5
by: Kate77 | last post by:
Hello, Im trying to do something very simple but having problems.. What I want to accomplish sounds simple but I cant find anywhere on the web answer of how to do that so it will work. I want...
4
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, Does the System.Windows.Forms.Timer invoke the main form thread? Or does it start another thread so that I need to lock my objects on which it operates in case the user of the program saves...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.