473,379 Members | 1,184 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,379 software developers and data experts.

Stopping System.Windows.Forms.Timer from another Thread Problem,...

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 everyting
is secured by Invoke/BeginInvoke, and synchronisation
primitves like WaitHandles, Evetns, Semaphores, etc...
All works good and with no race conditions or locks.
I have a System.Windows.Forms.Timer in the main
Thread that must stopped from the second thread.
Using a code like does not stop the Timer and the
Message is obviousy clear to me, but how can i
stop the Timer on the Form from another Thread
that still s running? Thats what i get with that code:

private delegate void DisableTimerDelegate();
private void DisableTimer()
{
this.timerServiceTimeoutTimer.Enabled = false;
}

this.Invoke(new DisableTimerDelegate(this.DisableTimer));

Leads to this Exception:

System.InvalidOperationException: Invoke oder BeginInvoke kann für ein
Steuerelement erst aufgerufen werden, wenn das Fensterhandle erstellt wurde.
bei System.Windows.Forms.Control.MarshaledInvoke(Contr ol caller, Delegate
method, Object[] args, Boolean synchronous)
bei System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
bei System.Windows.Forms.Control.Invoke(Delegate method)
bei
DeviceRemover.Forms.DRKernelCommandProgressClass.S erviceCommandThread(Object
parameter) in
C:\projects\DeviceRemover\Forms\DRKernelCommandPro gressClass.cs:Zeile 986.
bei System.Threading.ThreadHelper.ThreadStart_Context( Object state)
bei System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart(Object obj)

Translated says something like ""Invoke or BeginInvoke can only be called
for a Control if the WindowHandle has been created for it"

How can i "safely" stop that Timer from my second thread
in the main thread?

Thanks in advance,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Sep 8 '08 #1
4 5694

"Kerem Gümrükcü" wrote:
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 everyting
is secured by Invoke/BeginInvoke, and synchronisation
primitves like WaitHandles, Evetns, Semaphores, etc...
All works good and with no race conditions or locks.
I have a System.Windows.Forms.Timer in the main
Thread that must stopped from the second thread.
Using a code like does not stop the Timer and the
Message is obviousy clear to me, but how can i
stop the Timer on the Form from another Thread
that still s running? Thats what i get with that code:

private delegate void DisableTimerDelegate();
private void DisableTimer()
{
this.timerServiceTimeoutTimer.Enabled = false;
}

this.Invoke(new DisableTimerDelegate(this.DisableTimer));

Leads to this Exception:

System.InvalidOperationException: Invoke oder BeginInvoke kann für ein
Steuerelement erst aufgerufen werden, wenn das Fensterhandle erstellt wurde.
bei System.Windows.Forms.Control.MarshaledInvoke(Contr ol caller, Delegate
method, Object[] args, Boolean synchronous)
bei System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
bei System.Windows.Forms.Control.Invoke(Delegate method)
bei
DeviceRemover.Forms.DRKernelCommandProgressClass.S erviceCommandThread(Object
parameter) in
C:\projects\DeviceRemover\Forms\DRKernelCommandPro gressClass.cs:Zeile 986.
bei System.Threading.ThreadHelper.ThreadStart_Context( Object state)
bei System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart(Object obj)

Translated says something like ""Invoke or BeginInvoke can only be called
for a Control if the WindowHandle has been created for it"

How can i "safely" stop that Timer from my second thread
in the main thread?

Thanks in advance,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Hi Kerem,

Stopping a Timer by invoking the main thread should be fine. It looks like
the Control holding the timer object has been closed, is not fully created,
or something causing it to not have a window handle.

You could try calling the method directly and have it check for
InvokeRequired and Handle existance etc.

private void DisableTimer()
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(DisableTimer));
return;
}

if (Disposing || IsDisposed || Handle == IntPtr.Zero)
return;
else
this.timerServiceTimeoutTimer.Enabled = false;
}

You may want to use a System.Timers.Timer or System.Threading.Timer instead,
although then you may have to check for handle existance

PS! You don't need your own delegate to invoke void methods.

--
Happy Coding!
Morten Wennevik [C# MVP]

Sep 8 '08 #2
Hi Morten,

thanks for the answer. you say "PS! You don't need your own delegate to
invoke void methods.", so how
should or can i call a void delegate without a own delegate method?
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comschrieb im
Newsbeitrag news:B9**********************************@microsof t.com...
>
"Kerem Gümrükcü" wrote:
>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 everyting
is secured by Invoke/BeginInvoke, and synchronisation
primitves like WaitHandles, Evetns, Semaphores, etc...
All works good and with no race conditions or locks.
I have a System.Windows.Forms.Timer in the main
Thread that must stopped from the second thread.
Using a code like does not stop the Timer and the
Message is obviousy clear to me, but how can i
stop the Timer on the Form from another Thread
that still s running? Thats what i get with that code:

private delegate void DisableTimerDelegate();
private void DisableTimer()
{
this.timerServiceTimeoutTimer.Enabled = false;
}

this.Invoke(new DisableTimerDelegate(this.DisableTimer));

Leads to this Exception:

System.InvalidOperationException: Invoke oder BeginInvoke kann für ein
Steuerelement erst aufgerufen werden, wenn das Fensterhandle erstellt
wurde.
bei System.Windows.Forms.Control.MarshaledInvoke(Contr ol caller,
Delegate
method, Object[] args, Boolean synchronous)
bei System.Windows.Forms.Control.Invoke(Delegate method, Object[]
args)
bei System.Windows.Forms.Control.Invoke(Delegate method)
bei
DeviceRemover.Forms.DRKernelCommandProgressClass. ServiceCommandThread(Object
parameter) in
C:\projects\DeviceRemover\Forms\DRKernelCommandPr ogressClass.cs:Zeile
986.
bei System.Threading.ThreadHelper.ThreadStart_Context( Object state)
bei System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart(Object obj)

Translated says something like ""Invoke or BeginInvoke can only be called
for a Control if the WindowHandle has been created for it"

How can i "safely" stop that Timer from my second thread
in the main thread?

Thanks in advance,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."


Hi Kerem,

Stopping a Timer by invoking the main thread should be fine. It looks
like
the Control holding the timer object has been closed, is not fully
created,
or something causing it to not have a window handle.

You could try calling the method directly and have it check for
InvokeRequired and Handle existance etc.

private void DisableTimer()
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(DisableTimer));
return;
}

if (Disposing || IsDisposed || Handle == IntPtr.Zero)
return;
else
this.timerServiceTimeoutTimer.Enabled = false;
}

You may want to use a System.Timers.Timer or System.Threading.Timer
instead,
although then you may have to check for handle existance

PS! You don't need your own delegate to invoke void methods.

--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 8 '08 #3

"Kerem Gümrükcü" wrote:
Hi Morten,

thanks for the answer. you say "PS! You don't need your own delegate to
invoke void methods.", so how
should or can i call a void delegate without a own delegate method?
Regards

Kerem
Hi Kerem,

..Net Framework has a built-in delegate for empty methods called MethodInvoker

Invoke(new MethodInvoker(VoidMethod));

This won't work if you need parameters, though. You don't have to use it if
you don't want to.

--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 8 '08 #4
Hi Morten,
>This won't work if you need parameters, though. You >don't have to use it
if
you don't want to.
yes, i know this way of invoking methods, but i thought
you might talk about something different, i did not know.
Thanks for the Code and Reply,...

Take care,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comschrieb im
Newsbeitrag news:E3**********************************@microsof t.com...
>
"Kerem Gümrükcü" wrote:
>Hi Morten,

thanks for the answer. you say "PS! You don't need your own delegate to
invoke void methods.", so how
should or can i call a void delegate without a own delegate method?
Regards

Kerem

Hi Kerem,

.Net Framework has a built-in delegate for empty methods called
MethodInvoker

Invoke(new MethodInvoker(VoidMethod));

This won't work if you need parameters, though. You don't have to use it
if
you don't want to.

--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 8 '08 #5

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

Similar topics

4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
4
by: Bilo | last post by:
I have a Windows Forms Class MainGUI I have declared MainGUI maingui; public System.ComponentModel.Container components = new Container(); in the Class I call another class MediaDriver with...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
10
by: Zytan | last post by:
I made a program using the Timer class, and I start the timer with Timer.Start(), but don't stop it with Timer.Stop(), and I assumed this was ok, but, Process Explorer informs me, after running it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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.