Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
GeneralRe: Adding parameters to a table Pin
Nick Parker8-Feb-04 6:02
protectorNick Parker8-Feb-04 6:02 
QuestionMost efficient way of doing this? Pin
Alex Korchemniy7-Feb-04 22:31
Alex Korchemniy7-Feb-04 22:31 
GeneralZooming approach Pin
Meysam Mahfouzi7-Feb-04 18:08
Meysam Mahfouzi7-Feb-04 18:08 
GeneralRe: Zooming approach Pin
Heath Stewart7-Feb-04 19:00
protectorHeath Stewart7-Feb-04 19:00 
GeneralCombo Box problem... Pin
je_gonzalez7-Feb-04 17:27
je_gonzalez7-Feb-04 17:27 
GeneralDatabase Pin
ASGill7-Feb-04 17:16
ASGill7-Feb-04 17:16 
GeneralRe: Database Pin
Heath Stewart7-Feb-04 18:50
protectorHeath Stewart7-Feb-04 18:50 
GeneralMore Thread Troubles; not sure if it's deadlock Pin
Peter Mills7-Feb-04 14:41
Peter Mills7-Feb-04 14:41 
I'm creating a tool that queries game servers on the internet.
Though I seem to be having a weird problem when it comes to threading.
I managed to get round the problem of not being able to create threads and pass parameters to those methods.

There is a public variable called queryThread. For example it is: 64. The following code loops 64 times creating threads called queryServerRange. Each with the name of its thread number. (I use this number later on for the thread to figure out which servers it will query).






int i = 0;
while (i < queryThread)
{
i++;
Thread queryRangeThread = new Thread( new ThreadStart( queryServerRange ) );
queryRangeThread.Name = i.ToString();
queryRangeThread.Start();
Random R = new Random();
Thread.Sleep(R.Next(200));
}


Now the following code then first of all figures out which thread number it is,
how many servers there are to be queried in total (from the hashtable servers.Count).
In knowing its thread number, I do some maths to figure out the range this thread will handle.
i.e. server number 0 to 128.

I did it in this way also in an attempt to minimize deadlock/shared variable problems.
However my problem is this: intermittently the whole program will freeze in what I can only see as deadlock,
as yet I can't seem to find a solution.

There are occasions when the problem will not occur, but I can only see this
as being a fluke and down to servers responding in time, etc.

An earlier problem I had was in adding items to the listview below (some items were not appearing),
but I solved this problem with a monitor (Monitor.Enter(servers);).

I looked at doing the same whilst assigning myServer and calling .Refresh() however this slowed the program
right down and made it no faster than querying the servers one by one.

Another strange thing I've noticed, is that the lockups only seem to occur inside the Visual Studio IDE,
when I run the program completely outside of the IDE it doesn't appear to lock up ever.

Does anyone have any smart ideas of how I can stop my code locking up?

Thanks,
Peter.

Here's the code...

private void queryServerRange()
{
int remainder;
int number = Math.DivRem(int.Parse(servers.Count.ToString()), queryThread, out remainder);
int threadNumber = int.Parse(Thread.CurrentThread.Name);
int startPoint;
if (threadNumber == 1)
startPoint = number * (threadNumber - 1);
else
startPoint = number * (threadNumber - 1) + 1;
int endPoint;
if (threadNumber.Equals(queryThread) && !remainder.Equals(0))
endPoint = (number * threadNumber) + remainder;
else
endPoint = number * threadNumber;
//MessageBox.Show("start: " + startPoint.ToString() + " end: " + endPoint.ToString());
try
{
while (startPoint < endPoint)
{
if (status.pressedCancel)
{
Thread.CurrentThread.Abort();
}
Random R = new Random();

Thread.Sleep(R.Next(50));

int listViewIndex = -1;
object obj1 = servers[(long)startPoint];
GameServer myServer = new GameServer();
myServer = (GameServer) obj1;

// Set the servers' time until it gives up trying to contact it.
myServer.Timeout = timeout;

myServer.Refresh();
Monitor.Enter(servers);

// If the server is not in listViewServers and the server has the same filter,
// then add this server into the listView

if (listViewIndex == -1 && (filter.Equals(myServer.Filter.ToString()) || filter.Equals("") ))
{
if (! myServer.HostName.Equals("Timed Out"))
{
//Create a listItem so that the server can be added in one go to the listViewServer control
System.Windows.Forms.ListViewItem theServer = new System.Windows.Forms.ListViewItem(myServer.HostName.ToString());

//Add the servers' host name.
System.Windows.Forms.ListViewItem.ListViewSubItem item1 = new System.Windows.Forms.ListViewItem.ListViewSubItem (theServer, myServer.Ip.ToString() + ":" + myServer.Port.ToString());
//Add the servers' ip address and port number.
System.Windows.Forms.ListViewItem.ListViewSubItem item2 = new System.Windows.Forms.ListViewItem.ListViewSubItem (theServer, myServer.MapName.ToString());
//Add the number of players and total number of players allowed.
System.Windows.Forms.ListViewItem.ListViewSubItem item3 = new System.Windows.Forms.ListViewItem.ListViewSubItem (theServer, myServer.NumPlayers.ToString() +"/" + myServer.MaxPlayers.ToString());
//Add the servers' ping time.
System.Windows.Forms.ListViewItem.ListViewSubItem item4 = new System.Windows.Forms.ListViewItem.ListViewSubItem (theServer, myServer.Ping.ToString()+ "ms");
//Add if the server is a favourite.
System.Windows.Forms.ListViewItem.ListViewSubItem item5 = new System.Windows.Forms.ListViewItem.ListViewSubItem (theServer, "");

//Add the SubItems to the item.
theServer.SubItems.Add(item1);
theServer.SubItems.Add(item2);
theServer.SubItems.Add(item3);
theServer.SubItems.Add(item4);
theServer.SubItems.Add(item5);
theServer.Tag = myServer.serverID.ToString();

//Add the item to the listView.
listViewServers.Items.Add(theServer);
}
}
Monitor.Exit(servers);

status.labelRefresh.Text = "Refreshing Servers (" + (status.progressBarDownloading.Value) +"/" + status.progressBarDownloading.Maximum.ToString() + ")...";
status.progressBarDownloading.Value = status.progressBarDownloading.Value + 1;
startPoint++;
}
Thread.CurrentThread.Abort();
}

catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}
}
GeneralRe: More Thread Troubles; not sure if it's deadlock Pin
Heath Stewart7-Feb-04 18:58
protectorHeath Stewart7-Feb-04 18:58 
GeneralWell it's got me stumped Pin
Rob Manderson7-Feb-04 14:08
protectorRob Manderson7-Feb-04 14:08 
GeneralRe: Well it's got me stumped Pin
Rob Manderson7-Feb-04 16:25
protectorRob Manderson7-Feb-04 16:25 
GeneralRe: Well it's got me stumped Pin
Heath Stewart7-Feb-04 19:08
protectorHeath Stewart7-Feb-04 19:08 
GeneralRe: Well it's got me stumped Pin
Rob Manderson7-Feb-04 22:25
protectorRob Manderson7-Feb-04 22:25 
GeneralRe: Well it's got me stumped Pin
Heath Stewart8-Feb-04 5:40
protectorHeath Stewart8-Feb-04 5:40 
QuestionHow to make panel scroll when scrollbar is moved? Pin
rul3037-Feb-04 13:45
rul3037-Feb-04 13:45 
AnswerRe: How to make panel scroll when scrollbar is moved? Pin
Heath Stewart7-Feb-04 19:12
protectorHeath Stewart7-Feb-04 19:12 
GeneralRe: How to make panel scroll when scrollbar is moved? Pin
rul3038-Feb-04 7:02
rul3038-Feb-04 7:02 
GeneralRe: How to make panel scroll when scrollbar is moved? Pin
Heath Stewart8-Feb-04 7:07
protectorHeath Stewart8-Feb-04 7:07 
GeneralGUID Conversion Pin
Tristan Rhodes7-Feb-04 8:34
Tristan Rhodes7-Feb-04 8:34 
GeneralRe: GUID Conversion Pin
Heath Stewart7-Feb-04 19:19
protectorHeath Stewart7-Feb-04 19:19 
GeneralRe: GUID Conversion Pin
Tristan Rhodes7-Feb-04 21:08
Tristan Rhodes7-Feb-04 21:08 
GeneralRe: GUID Conversion Pin
leppie7-Feb-04 23:23
leppie7-Feb-04 23:23 
GeneralRe: GUID Conversion Pin
Heath Stewart8-Feb-04 5:32
protectorHeath Stewart8-Feb-04 5:32 
GeneralRe: GUID Conversion Pin
Tristan Rhodes8-Feb-04 8:36
Tristan Rhodes8-Feb-04 8:36 
GeneralRe: GUID Conversion Pin
Nick Parker8-Feb-04 5:45
protectorNick Parker8-Feb-04 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.