Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi guys,
 
I have a server-client system going on with client commands controlling the server form.
 
The commands are connect, disconnect, send and receive.
 
When client wants to send data to/receive data from server (command == send/receive), there will be a progress bar (marquee style) shown on the same server form and hide when sending/receiving is done. The progress bar works until i start to invoke server commands (freeze).
 
The problem is that the server commands are (and must be) executed by server in the main thread.
 
So even if i run the command in a backgroundworker, it doesn’t help since the actual server commands are invoked on the main thread – the same thread that shows the progress bar.
 
Any ideas?
 
        
        private SynchronizationContext context = SynchronizationContext.Current ?? new SynchronizationContext();
	private Thread _serverThread = null;
      
	public bool startServer()
        {
 
            if (_serverThread != null)
            {
                return false;
            }
 
            Server _Server = null;
            _Server = new Server();
            _Server.callback += new EventHandler(server_callback);
 
            return true;
        }
 

        void server_callback(object sender, EventArgs e)
        {
            ServerEventArgs types = e as ServerEventArgs;
            if (types != null)
            {
                context.Send(new SendOrPostCallback(processCommand), e);
            }
        
        }
 

        public void processCommand(Object o)
        {
            ServerEventArgs args = o as ServerEventArgs;
            if (args != null) 
            {
                string input = args.command;
                string status = _revitWorker.ProcessCommand(app, input, true);
 
                statusUpdate(status);
 
                string output = _revitWorker.ProcessCommand(app, input, false);
                args.response = output;
            }
        }
 

        //update status
        private void statusUpdate(string status)
        {
            if (status == "disconnect")
            {
                disconnectFlag();
            }
            else if (status == "loading")
            {
                ProcessingFlag();
            }
            else
            {
				connectFlag();
            }
        }
        
 
        //disconnect
        public void disconnectFlag()
        {
            label1.Text = "disconnect";
            progressBar1.Visible = false;
        }
 

       	//processing
        public void processingFlag()
        {
            label1.Text = "processing";
            progressBar1.Visible = true;	//progress bar appear only when loading
        }
 

       	//connect
        public void connectFlag()
        {
            label1.Text = "ready";
            progressBar1.Visible = false;
        }
Posted 12-Feb-13 23:10pm
jkhann282
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

instead of progress bar use can use ajax loaders (.gif) files this is easier.
  Permalink  
Comments
jkhann at 14-Feb-13 2:27am
   
It is still the same. :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
Have you tried this.
//disconnect

public void SetStatusDelegate (bool state, string label);
 
public void disconnectFlag()
{
    new SetStatusDelegate(SetStatus).Invoke(false, "disconnect");
}
 

//processing
public void processingFlag()
{
    new SetStatusDelegate(SetStatus).Invoke(true, "processing");
    //progress bar appear only when loading
}
 

//connect
public void connectFlag()
{
    new SetStatusDelegate(SetStatus).Invoke(false, "ready");
}
 
private void SetStatus(bool status, string label)
{
    label1.Text = label;
    progressBar1.Visible = status;
}
 
Regards
Jegan
  Permalink  
v3
Comments
jkhann at 14-Feb-13 2:22am
   
Nope, still the same . Frozen when loading.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
That means you are blocking the main thread.
 
Try this:
 
public bool startServer()
{
    if (_serverThread != null)
    {
        return false;
    }
 
    Task taskA = Task.Factory.StartNew(() => 
     { 
         Server _Server = null;
         _Server = new Server();
         _Server.callback += new EventHandler(server_callback);
     });
 
     return true;
}
 
The Task factory will guarantee to start a separate thread, and I am not sure whether do you need to check the "_serverThread" at all.
 
Regards
Jegan
  Permalink  
Comments
jkhann at 15-Feb-13 1:02am
   
Thanks Jegan. How is Task better than backgrounworker? This code induced some sort of error but i will work from this direction.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi
Have a look at this article,
 
http://jeremybytes.blogspot.ca/2012/05/backgroundworker-component-im-not-dead.html[^]
 
This explains when to use background worker and when to use TPL. This article also has a link to solution similar to what you are looking for.
 
http://www.jeremybytes.com/Demos.aspx#KYUIR[^]
 

Regards
Jegan
  Permalink  
Comments
jkhann at 15-Feb-13 4:45am
   
Thanks again.I am looking at this tutorial and hope something good will happen soon !
http://www.rocksolidknowledge.com/ScreenCasts.mvc/Watch?video=TasksAndThreadAffinity.wmv

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 Kornfeld Eliyahu Peter 407
2 Maciej Los 369
3 DamithSL 221
4 OriginalGriff 218
0 OriginalGriff 6,383
1 DamithSL 4,879
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,056
4 Sergey Alexandrovich Kryukov 3,927


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100