473,670 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stop a function

I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...
Nov 16 '05 #1
26 4683
There are a few ways, but you could use IAsyncResult for instance, and use a
while loop. the while could read in a bool. When the user hits the button,
set the bool to false. By using another thread, you won't freeze up the UI.
http://www.knowdotnet.com/articles/delegates.html
"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...

Nov 16 '05 #2
> I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???

You need to run that method in a separate Thread. If you press the button,
set a flag which you test in the method and return in this case.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #3
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the
random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoE vents() this process the events queries, you can then use a
Stop button that set a flag to false, before the next number is generated yo
check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoE vents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can pause
the thread execution, remember that if you use this approach you have to
make sure that the call to update the interface be called in the main thread
using Control.Invoke

Pd: I will not post code for this as its a little more complex, just drop
me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
I made a program that generate random numbers and put it in a listbox when
the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...

Nov 16 '05 #4
Scenario #1 is preferable to spawning a thread, which would be difficult to
manage, test, debug, etc.

Brad Williams
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:u7******** ******@tk2msftn gp13.phx.gbl...
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the
random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoE vents() this process the events queries, you can then use a
Stop button that set a flag to false, before the next number is generated yo check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoE vents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can pause
the thread execution, remember that if you use this approach you have to
make sure that the call to update the interface be called in the main thread using Control.Invoke

Pd: I will not post code for this as its a little more complex, just drop
me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
I made a program that generate random numbers and put it in a listbox when the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...


Nov 16 '05 #5
Hi,

It's as long as the operation to be performed is not lengthy, if it's the
UI will hang until the call to DoEvents() .
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Brad Williams" <sp**@spam.co m> wrote in message
news:c7******** **@news01.intel .com...
Scenario #1 is preferable to spawning a thread, which would be difficult to manage, test, debug, etc.

Brad Williams
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:u7******** ******@tk2msftn gp13.phx.gbl...
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the
random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoE vents() this process the events queries, you can then use a Stop button that set a flag to false, before the next number is generated
yo
check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoE vents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can

pause the thread execution, remember that if you use this approach you have to
make sure that the call to update the interface be called in the main

thread
using Control.Invoke

Pd: I will not post code for this as its a little more complex, just drop me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
I made a program that generate random numbers and put it in a listbox

when the user click go.
The problem is: how can i made a button stop, to stop the method that is running???
[]s...



Nov 16 '05 #6
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > a
écrit dans le message de news:u7******** ******@tk2msftn gp13.phx.gbl...
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the
random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoE vents() this process the events queries, you can then use a
Stop button that set a flag to false, before the next number is generated yo check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoE vents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can pause
the thread execution, remember that if you use this approach you have to
make sure that the call to update the interface be called in the main thread using Control.Invoke

Pd: I will not post code for this as its a little more complex, just drop
me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
I made a program that generate random numbers and put it in a listbox when the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...



Hi.

The use of DoEvent is a very risky practice. Indeed, any event can occur
during the call of doevent. It might change some input you are dealing with
in your generateRandom method.
You must be very cautious of not encounter some incoherences.
Althought it may look a little bit more complex, you should definitely use a
worker thread. It is much easier to control.

Fred
Nov 16 '05 #7
"Iceman" <fr************ ************@ne uf.fr> wrote in message
news:c7******** **@aphrodite.gr ec.isp.9tel.net ...
The use of DoEvent is a very risky practice.


This is exactly how we handled this in Windows 3.1, and the code was ugly
but actually much less error prone than trying to herd threads.
Multithreaded programming is harder than most people realize, unfortunately.

Brad Williams
Nov 16 '05 #8
could you send the code of scenario 2 to my e-mail:
dr*******@terra .com.br

bye...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:u7******** ******@tk2msftn gp13.phx.gbl...
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the
random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoE vents() this process the events queries, you can then use a
Stop button that set a flag to false, before the next number is generated yo check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoE vents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can pause
the thread execution, remember that if you use this approach you have to
make sure that the call to update the interface be called in the main thread using Control.Invoke

Pd: I will not post code for this as its a little more complex, just drop
me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
I made a program that generate random numbers and put it in a listbox when the user click go.
The problem is: how can i made a button stop, to stop the method that is
running???
[]s...


Nov 16 '05 #9
The UI is hanging, the scenario 1 not work...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:eM******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

It's as long as the operation to be performed is not lengthy, if it's the
UI will hang until the call to DoEvents() .
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Brad Williams" <sp**@spam.co m> wrote in message
news:c7******** **@news01.intel .com...
Scenario #1 is preferable to spawning a thread, which would be difficult to
manage, test, debug, etc.

Brad Williams
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >

wrote
in message news:u7******** ******@tk2msftn gp13.phx.gbl...
Hi Ricardo,

There are a few ways of doing it , it all depend of how/where you run the random generator, I will show you two escenarios
1- gen. runs on the same thread.

while you are generating numbers , between number generation you call
Application.DoE vents() this process the events queries, you can then use a
Stop button that set a flag to false, before the next number is generated
yo
check for this flag, like this:

bool generate = true;
void generaterandom( object sender, EventArgs e )
{
while ( generate)
{
// generate it and update the UI
Application.DoE vents();
}

}
void Stop_OnClick( object sender, EventArgs e )
{
generate = false;
}

2- run the generator on another thread and then the Stop button can pause the thread execution, remember that if you use this approach you have
to make sure that the call to update the interface be called in the main thread
using Control.Invoke

Pd: I will not post code for this as its a little more complex, just

drop me a note if you need it

HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ricardo" <dr*******@terr a.com.br> wrote in message
news:O9******** ******@TK2MSFTN GP12.phx.gbl...
> I made a program that generate random numbers and put it in a
listbox when
> the user click go.
> The problem is: how can i made a button stop, to stop the method

that is > running???
>
>
> []s...
>
>



Nov 16 '05 #10

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

Similar topics

2
5195
by: engsol | last post by:
I'm using Python to parse a bunch of s/w test files and make csv files for later report generation by MS ACCESS....(my boss loves the quick turn-around compared to C). Each log file may contain one or more 'sessions', and each session may contain one or more 'nodes'. Each session in the log has an ASCII start and stop time, as does each node. I have the basic parse part done for parameters, errors, etc., but noticed my routine for...
8
2034
by: Eric Osman | last post by:
My javascript program has reason to want to stop. For example, function A has 5 lines, the second of which calls function B, which has 5 lines, the first of which calls function C. But function C discovers that something is very wrong so it does an "alert" saying something like Sorry, couldn't make the necessary connection
2
2110
by: Adrian MacNair | last post by:
Hi I need some help if anyone can understand my crap javascript. The problem is that after the slideshow ends (reaches the end of array) it should stop, but the timeout doesn't clear and I can see the layer flashing. I wrote a slideshow script. When you click a hypertext link it calls the function speed() and passes the variable 5000: var myvar = 0; // Starting variable at zero var myTimeout;
4
3098
by: David | last post by:
Hi everyone, I am trying to stop an image preload sequence by the click of a mouse but have been unsuccessful trying several methods. Imagine this simple script below that loads 50 images to cache. If the stopPreload() function is activated and the ret val set to false, the preload() function still continues to the end. Any suggestions on how to stop the preload() function in its process, what conditions are necessary?
7
2266
by: jab3 | last post by:
Hello. I'm wondering if someone can answer something I'm sure has been answered a thousand times before. I am apparently just too dumb to find the answer. :) I've found information about the 'onstop' event, but it's not behaving as expected. (And it also seems to be a proprietary attribute) That is, my defined function is not being run when I click stop. I've 'inserted' it like this: <body onstop="stopped_clicked()"> And the function...
2
2269
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Is there a way to start, pause and resume a recurrsive search exactly where you left off, say in the registry programmatically? -- Michael Bragg, President eSolTec, Inc. a 501(C)(3) organization MS Authorized MAR looking for used laptops for developmentally disabled.
1
1070
by: Simone | last post by:
Hello In visual studio when I am debugging my ASP. net code if I hit "stop" the procedure I am in will still finish itself and doesn't stop where I expect it to stop. I remember in VB when you stop your code it stop at the exact line you're on. Now if I stop at the beginning at the procedure or function the code will still continue until the end sub/end function.
4
2507
by: ravindarjobs | last post by:
hi...... i am using ms access 2003,vb6 i have a form. in that i have 2 buttons 1. start search 2 stop search when i click the "start search" button the fucntion SearchSystem() is called, it will search for a particular file in the computer(searches entire drives).
8
12597
praclarush
by: praclarush | last post by:
Ok, I'm new to JavaScript and I'm taking a class for it the assignment in it I'm supposed to create edit a pre-made page to display a marquee that automatically scrolls for the user, as well as give an option to start, stop and reset the marquee. Now I have most of this done already, what I'm having problems with is that when i start the marquee it moves to the right, but i need to have it move from the bottom, upwards. heres my code (I'm not...
4
2088
by: Rajneesh Chellapilla | last post by:
I wrote this program. Its kinda of strange when I make a reset function reset(){c=0} its doest reset the setTimeout. However if I directly pass c=0 to the onclick button it does reset the timer. What is the logic of this? Here is the program: <html> <head> <script type="text/javascript"> var c=0; var t; function timedCount()
0
8469
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.