asmyatt
asked on
looping a BackgroundWorker
I have a background worker that recieves data from a server and writes it to a text box. How can I loop this so that after it recieves one set of data and writes it to the text box it will continue to listed for more data.
What I current;y do is call this:
backgroundWorker2.RunWorke rAsync();
when the form loads. It will work fine but of course doesn't continue to listen for more data.
Thanks
What I current;y do is call this:
backgroundWorker2.RunWorke
when the form loads. It will work fine but of course doesn't continue to listen for more data.
Thanks
ASKER
I read network data, no data from a database. The data comes across a socket as string, and I do some processing with received string. You can think of it like a chat program. A client sends data to the server and the server sends the message to all of the clients connected to it.
I did manage to get it to work. At the end of RunWorkerCompleted I just called backgroundWorker2.RunWorke rAsync(); again and it starts the background worker like I wanted it too.
I'm new to C# so I would like to hear ideas of how this should be done if this way is not recommended.
Thanks
I did manage to get it to work. At the end of RunWorkerCompleted I just called backgroundWorker2.RunWorke
I'm new to C# so I would like to hear ideas of how this should be done if this way is not recommended.
Thanks
Generally, for network operations, BackgroundWorker DoWork function must look like this (pseudo-code):
while ( true )
{
Wait for data
Handle data
}
DoWork function executes endless loop handling all incoming data from network connection.
while ( true )
{
Wait for data
Handle data
}
DoWork function executes endless loop handling all incoming data from network connection.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it is an example which connects to host.contoso.com on client site.
so you will have to change the IPHostEntry to the machine, were you run the server.
so you will have to change the IPHostEntry to the machine, were you run the server.
normally, the backgroundworker class is only used do run a single asny operation, not for using network listening.
what you could do is run the worker again after RunWorkerCompleted event is fired.
how does the data comes from the server?
do you read some date from a database with your backgroudnworker or do you read some network data?