473,216 Members | 1,358 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,216 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 fr 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 Grsse / Best regards / Votre bien devoue
Kerem Gmrkc
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 5681

"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
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.