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

how to pause between commands without gui lock

Hi

wondering what is the best way to do this

Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link

I can connect to the ip device no problems, am using indy sockets, and
can send information ok

but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click

The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time

EG to IP device

command 1

pause 200ms

command 2

pause 250ms

coomand 3

pause 300ms

command 4

pause 200ms
and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.

Can anyone point me to atechnique or algorithm to do the pausing
without gui lock
thanks
Mar 15 '07 #1
6 4021
On 15 Mar, 10:55, Peted wrote:
Hi

wondering what is the best way to do this

Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link

I can connect to the ip device no problems, am using indy sockets, and
can send information ok

but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click

The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time

EG to IP device

command 1

pause 200ms

command 2

pause 250ms

coomand 3

pause 300ms

command 4

pause 200ms

and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.

Can anyone point me to atechnique or algorithm to do the pausing
without gui lock

thanks
This should work. I've used a console app purely because it means I
can get a nice little text output to add here. The technique will work
just as well in a gui. I also didn't actually add any net code, it
just prints to the console, but it sounds like you already have that.
Just paste this over a new console app and hit run. If you get any
errors check for line breaks.
Finally if you're using framework 2, there's a
ParameterizedThreadStart class that might be of interest.
Here's the output the code generates.

In main thread, sleeping for 100ms
Sending Command 3 then sleeping for 700ms
Sending Command 2 then sleeping for 1000ms
Sending Command 1 then sleeping for 500ms
In main thread, woken after 100ms
In main thread, sleeping for 500ms
Completed Command 1
In main thread, woken after 500ms
In main thread, sleeping for 600ms
Completed Command 3
Completed Command 2
In main thread, woken after 600ms

and here's the code

using System;
using System.Threading;
using System.Collections;

namespace ConsoleApplication2
{
class Demo
{
[STAThread]
static void Main(string[] args)
{

ThreadLauncher.ThreadBasics("Command 1",500);
ThreadLauncher.ThreadBasics("Command 2",1000);
ThreadLauncher.ThreadBasics("Command 3",700);

Console.WriteLine("In main thread, sleeping for 100ms");
Thread.Sleep(100);
Console.WriteLine("In main thread, woken after 100ms");
Console.WriteLine("In main thread, sleeping for 500ms");
Thread.Sleep(500);
Console.WriteLine("In main thread, woken after 500ms");
Console.WriteLine("In main thread, sleeping for 600ms");
Thread.Sleep(600);
Console.WriteLine("In main thread, woken after 600ms");
Console.ReadLine();
}
}
public class ThreadLauncher
{

private ThreadLauncher()
{}
public static void ThreadBasics(string pCommandString, int pWait)
{
ThreadClass t = new ThreadClass(pCommandString, pWait);
t.Start();
}
private class ThreadClass
{
private string _commandString;
private int _wait;

public ThreadClass(string pCommandString, int pWait)
{
_commandString = pCommandString;
_wait = pWait;
}
public void Start()
{
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
// You could also investigate ThreadPool.QueueUserWorkItem
Thread myThread = new Thread(myThreadStart);
myThread.Start();
}
private void ThreadMethod()
{
string commandString=_commandString;
int wait = _wait;
Console.WriteLine("Sending {0} then sleeping for
{1}ms",commandString, wait);
Thread.Sleep(wait);
Console.WriteLine("Completed {0}",commandString);
}
}
}
}

Mar 15 '07 #2
On 15 Mar, 10:55, Peted wrote:
Hi

wondering what is the best way to do this

Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link

I can connect to the ip device no problems, am using indy sockets, and
can send information ok

but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click

The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time

EG to IP device

command 1

pause 200ms

command 2

pause 250ms

coomand 3

pause 300ms

command 4

pause 200ms

and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.

Can anyone point me to atechnique or algorithm to do the pausing
without gui lock

thanks
Actually, just reread your post and what I've done isn't quite what
you specified. Give me two minutes and I'll rewrite it :)

Mar 15 '07 #3
On 15 Mar, 11:22, "DeveloperX" <nntp...@operamail.comwrote:
On 15 Mar, 10:55, Peted wrote:


Hi
wondering what is the best way to do this
Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link
I can connect to the ip device no problems, am using indy sockets, and
can send information ok
but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click
The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time
EG to IP device
command 1
pause 200ms
command 2
pause 250ms
coomand 3
pause 300ms
command 4
pause 200ms
and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.
Can anyone point me to atechnique or algorithm to do the pausing
without gui lock
thanks

Actually, just reread your post and what I've done isn't quite what
you specified. Give me two minutes and I'll rewrite it :)- Hide quoted text -

- Show quoted text -
Ok, this will Queue the commands and execute them serially. I've
modified the test case so the main thread will pause long enough for
the queue to empty out, then I add another command. The output looks
like this:

In main thread, sleeping for 200ms
Sending Command 1 then sleeping for 500ms
In main thread, woken after 200ms
In main thread, sleeping for 500ms
Slept for 500 moving to next command
Sending Command 2 then sleeping for 1000ms
In main thread, woken after 500ms
In main thread, sleeping for 900ms
Slept for 1000 moving to next command
Sending Command 3 then sleeping for 700ms
In main thread, woken after 900ms
In main thread, sleeping for 800ms
Slept for 700 moving to next command
No more commands
In main thread, woken after 800ms
Sending Command 4 then sleeping for 400ms
Slept for 400 moving to next command
No more commands
Here's the code

using System;
using System.Threading;
using System.Collections;

namespace ConsoleApplication2
{
class Demo
{
[STAThread]
static void Main(string[] args)
{

ThreadLauncher.ThreadBasics("Command 1",500);
ThreadLauncher.ThreadBasics("Command 2",1000);
ThreadLauncher.ThreadBasics("Command 3",700);

Console.WriteLine("In main thread, sleeping for 200ms");
Thread.Sleep(100);
Console.WriteLine("In main thread, woken after 200ms");
Console.WriteLine("In main thread, sleeping for 500ms");
Thread.Sleep(500);
Console.WriteLine("In main thread, woken after 500ms");
Console.WriteLine("In main thread, sleeping for 900ms");
Thread.Sleep(900);
Console.WriteLine("In main thread, woken after 900ms");
Console.WriteLine("In main thread, sleeping for 800ms");
Thread.Sleep(800);
Console.WriteLine("In main thread, woken after 800ms");
ThreadLauncher.ThreadBasics("Command 4",400);
Console.ReadLine();
}
}
public class ThreadLauncher
{
private static ThreadClass _tc = new ThreadClass();
private ThreadLauncher()
{}
public static void ThreadBasics(string pCommandString, int pWait)
{
_tc.Add(pCommandString, pWait);
}
private class ThreadClass
{
private Queue _queue;
private Thread _myThread;
private object _lockMe = new object();

public ThreadClass()
{
_queue = new Queue();
}

public void Add(string pCommandString, int pWait)
{
lock(_lockMe)
{
_queue.Enqueue(new TheCommand(pCommandString,pWait));
if (_myThread == null)
{
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
_myThread = new Thread(myThreadStart);
_myThread.Start();
}
}
}
private void ThreadMethod()
{
TheCommand tc;
lock(_lockMe)
{
while(_queue.Count 0)
{
tc = (TheCommand)_queue.Dequeue();
Console.WriteLine("Sending {0} then sleeping for
{1}ms",tc.CommandString, tc.WaitForNext);
Thread.Sleep(tc.WaitForNext);
Console.WriteLine("Slept for {0} moving to next
command",tc.WaitForNext);
}
Console.WriteLine("No more commands");
_myThread = null;
}
}
}
private struct TheCommand
{
public TheCommand(string pCommandString, int pWait)
{
CommandString = pCommandString;
WaitForNext = pWait;
}
public string CommandString;
public int WaitForNext;
}

}
}
Mar 15 '07 #4
On Mar 15, 10:55 am, Peted wrote:

<snip>
Can anyone point me to atechnique or algorithm to do the pausing
without gui lock
You should do all the long-running work in a background thread,
reporting progress to the UI thread.
See http://pobox.com/~skeet/csharp/threads/winforms.shtml

Jon

Mar 15 '07 #5
On 15 Mar, 11:44, "DeveloperX" <nntp...@operamail.comwrote:
On 15 Mar, 11:22, "DeveloperX" <nntp...@operamail.comwrote:


On 15 Mar, 10:55, Peted wrote:
Hi
wondering what is the best way to do this
Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link
I can connect to the ip device no problems, am using indy sockets, and
can send information ok
but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click
The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time
EG to IP device
command 1
pause 200ms
command 2
pause 250ms
coomand 3
pause 300ms
command 4
pause 200ms
and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.
Can anyone point me to atechnique or algorithm to do the pausing
without gui lock
thanks
Actually, just reread your post and what I've done isn't quite what
you specified. Give me two minutes and I'll rewrite it :)- Hide quoted text -
- Show quoted text -

Ok, this will Queue the commands and execute them serially. I've
modified the test case so the main thread will pause long enough for
the queue to empty out, then I add another command. The output looks
like this:

In main thread, sleeping for 200ms
Sending Command 1 then sleeping for 500ms
In main thread, woken after 200ms
In main thread, sleeping for 500ms
Slept for 500 moving to next command
Sending Command 2 then sleeping for 1000ms
In main thread, woken after 500ms
In main thread, sleeping for 900ms
Slept for 1000 moving to next command
Sending Command 3 then sleeping for 700ms
In main thread, woken after 900ms
In main thread, sleeping for 800ms
Slept for 700 moving to next command
No more commands
In main thread, woken after 800ms
Sending Command 4 then sleeping for 400ms
Slept for 400 moving to next command
No more commands

Here's the code

using System;
using System.Threading;
using System.Collections;

namespace ConsoleApplication2
{
class Demo
{
[STAThread]
static void Main(string[] args)
{

ThreadLauncher.ThreadBasics("Command 1",500);
ThreadLauncher.ThreadBasics("Command 2",1000);
ThreadLauncher.ThreadBasics("Command 3",700);

Console.WriteLine("In main thread, sleeping for 200ms");
Thread.Sleep(100);
Console.WriteLine("In main thread, woken after 200ms");
Console.WriteLine("In main thread, sleeping for 500ms");
Thread.Sleep(500);
Console.WriteLine("In main thread, woken after 500ms");
Console.WriteLine("In main thread, sleeping for 900ms");
Thread.Sleep(900);
Console.WriteLine("In main thread, woken after 900ms");
Console.WriteLine("In main thread, sleeping for 800ms");
Thread.Sleep(800);
Console.WriteLine("In main thread, woken after 800ms");
ThreadLauncher.ThreadBasics("Command 4",400);
Console.ReadLine();
}
}
public class ThreadLauncher
{
private static ThreadClass _tc = new ThreadClass();
private ThreadLauncher()
{}
public static void ThreadBasics(string pCommandString, int pWait)
{
_tc.Add(pCommandString, pWait);
}
private class ThreadClass
{
private Queue _queue;
private Thread _myThread;
private object _lockMe = new object();

public ThreadClass()
{
_queue = new Queue();
}

public void Add(string pCommandString, int pWait)
{
lock(_lockMe)
{
_queue.Enqueue(new TheCommand(pCommandString,pWait));
if (_myThread == null)
{
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
_myThread = new Thread(myThreadStart);
_myThread.Start();
}
}
}
private void ThreadMethod()
{
TheCommand tc;
lock(_lockMe)
{
while(_queue.Count 0)
{
tc = (TheCommand)_queue.Dequeue();
Console.WriteLine("Sending {0} then sleeping for
{1}ms",tc.CommandString, tc.WaitForNext);
Thread.Sleep(tc.WaitForNext);
Console.WriteLine("Slept for {0} moving to next
command",tc.WaitForNext);
}
Console.WriteLine("No more commands");
_myThread = null;
}
}
}
private struct TheCommand
{
public TheCommand(string pCommandString, int pWait)
{
CommandString = pCommandString;
WaitForNext = pWait;
}
public string CommandString;
public int WaitForNext;
}

}

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
One point, I've just noticed that I've got my thread sleep wrapped in
the lock which is silly. It means if you try and add a command while
there's already stuff on the queue it will block until the queue is
empty.
I think the locks can be removed safely anyway, I wasn't sure if Queue
was thread safe, but I can't find anything to suggest it isn't, so you
should just be able to remove the lock statements.

If it isn't threadsafe, then the following change will deal with that
(watch line wrap):

public void Add(string pCommandString, int pWait)
{
lock(_lockMe)
{
_queue.Enqueue(new TheCommand(pCommandString,pWait));
}
if (_myThread == null)
{
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
_myThread = new Thread(myThreadStart);
_myThread.Start();
}
}
private void ThreadMethod()
{
TheCommand tc;

while(QueueCount 0)
{
lock(_lockMe)
{
tc = (TheCommand)_queue.Dequeue();
}
Console.WriteLine("Sending {0} then sleeping for
{1}ms",tc.CommandString, tc.WaitForNext);
Thread.Sleep(tc.WaitForNext);
Console.WriteLine("Slept for {0} moving to next
command",tc.WaitForNext);
}
Console.WriteLine("No more commands");
_myThread = null;
}
private int QueueCount
{
get
{
lock(_lockMe)
{
return _queue.Count;
}
}
}

Mar 15 '07 #6
On Thu, 15 Mar 2007 18:55:45 +0800, Peted wrote:

thanks both for the info will check it out
thanks heaps




>Hi

wondering what is the best way to do this

Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link

I can connect to the ip device no problems, am using indy sockets, and
can send information ok

but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click

The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time

EG to IP device

command 1

pause 200ms

command 2

pause 250ms

coomand 3

pause 300ms

command 4

pause 200ms
and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.

Can anyone point me to atechnique or algorithm to do the pausing
without gui lock
thanks

Mar 16 '07 #7

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

Similar topics

7
by: Stephen Williams | last post by:
I want to add a 2 secondish pause to a program, i can't find any nice and easy commands that will do it. I looked at the timer control but i can't seem to see how it will help. Any ideas?
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
5
by: Ghazan Haider | last post by:
Hi all, I have a server from which keeping clients off for maintenance is difficult. They all have VPN connections and can be online any time they want, and uptime as always is important. Now...
11
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
19
by: C# Learner | last post by:
What's a nice way to create a non-blocking pause in execution? Will I need to use some kind of timer for this, or is there a nicer way?
8
by: Wim | last post by:
My GUI application starts a process (a console program) when the user hits Play. I would like to add an option to pause that process. The code I've added to detect if the user hit pause/unpause...
15
by: dylpkls91 | last post by:
I have been researching this topic and come up with some code to make it work. It uses SSL and requires the 3rd party package Paramiko (which requires PyCrypto). However, at this moment I have no...
6
by: Zytan | last post by:
I have code running in the debugger as I type. I press pause, and it pauses on: Application.Run(new myForm()); *I believe* a worker thread is in deadlock (it's in a lock, but calls another...
16
by: Kapteyn's Star | last post by:
Hello all, I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much. --...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.