how to pause between commands without gui lock

P

Peted

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
 
D

DeveloperX

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);
}
}
}
}
 
D

DeveloperX

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 :)
 
D

DeveloperX

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;
}

}
}
 
D

DeveloperX

On 15 Mar, 10:55, Peted wrote:
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;
}
}
}
 
P

Peted

On Thu, 15 Mar 2007 18:55:45 +0800, Peted wrote:

thanks both for the info will check it out


thanks heaps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top