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

C#

 
GeneralRe: Can anyone help me? Pin
Not Active11-Jun-09 14:41
mentorNot Active11-Jun-09 14:41 
GeneralRe: Can anyone help me? Pin
Christian Graus11-Jun-09 14:44
protectorChristian Graus11-Jun-09 14:44 
GeneralRe: Can anyone help me? Pin
Christian Graus11-Jun-09 15:06
protectorChristian Graus11-Jun-09 15:06 
GeneralRe: Using of C# to do print,merge pdf Pin
daffy_200311-Jun-09 14:45
daffy_200311-Jun-09 14:45 
GeneralRe: Using of C# to do print,merge pdf Pin
Christian Graus11-Jun-09 14:48
protectorChristian Graus11-Jun-09 14:48 
GeneralRe: Using of C# to do print,merge pdf Pin
daffy_200311-Jun-09 14:55
daffy_200311-Jun-09 14:55 
GeneralRe: Using of C# to do print,merge pdf Pin
Christian Graus11-Jun-09 14:56
protectorChristian Graus11-Jun-09 14:56 
QuestionWhat do you think of this? Multithreaded remote registry handler Pin
jackgeek11-Jun-09 12:02
jackgeek11-Jun-09 12:02 
Here is my first attempt at writing a multi-threaded class.

The purpose of this class is to allow you to connect to a remote registry without things going bad if the remote machine doesn't respond.   It also holds open remote connections for a predefined period before destroying the connection to allow for reuse.

I'd really like some feedback on the threading.   Is it thread safe?   Have I made any errors that might make it blow up in certain circumstances?

Many thanks!

using Microsoft.Win32;
using System;
using System.Threading;
using System.Collections.Generic;

namespace JacksTools
{
      public class RemoteRegistryHandler
      {
            private class RegistryStub
            {
                  private RegistryKey _reference;
                  private int _timeout;
                  private DateTime _lastAccessed;

                  public RegistryKey Reference
                  {
                        get {
                              _lastAccessed = System.DateTime.Now;
                              return _reference;
                        }
                  }

                  public Boolean TimedOut
                  {
                        get
                        {
                              return (_lastAccessed.AddMilliseconds(_timeout) < System.DateTime.Now);
                        }
                  }

                  public RegistryStub(int timeout, RegistryKey reference)
                  {
                        _lastAccessed = System.DateTime.Now;
                        _timeout = timeout;
                        _reference = reference;
                  }
            }
           
            Dictionary<String, RegistryStub> _store = new Dictionary<string,RegistryStub>();

            private int _timeout;
            private Thread _cleaner;
            private Boolean _closingDown = false;

            private class AttemptGet
            {
                  private String _machineName;
                  private RegistryHive _hive;
                  public RegistryKey ResultKey;
                  public Exception Error;

                  public void Attempt()
                  {
                        try
                        {
                              ResultKey = RegistryKey.OpenRemoteBaseKey(_hive, _machineName);
                        }
                        catch (Exception e) {
                              Error = e;
                              Console.WriteLine("The following exception occured when attempting to connect a remote registry (" + _hive.ToString() + " on " + _machineName + ")\n" +
                                    e.ToString());
                        }
                  }

                  public AttemptGet(String machineName, RegistryHive hive)
                  {
                        _machineName = machineName; _hive = hive;
                  }
            }

            public RegistryKey GetRemoteKey(String machineName, RegistryHive hive, int timeout)
            {
                  lock (_store)
                  {
                        String key = machineName + hive.ToString();
                        if (_store.ContainsKey(key))
                        {
                              if (!_store[key].TimedOut)
                              {
                                    return _store[key].Reference;
                              }
                              else
                              {
                                    _store.Remove(key);
                                    Console.WriteLine("RemoteRegistryHandler cleaned " + machineName + hive.ToString());
                              }
                        }
                        AttemptGet ag = new AttemptGet(machineName, hive);
                        Thread attemptThread = new Thread(new ThreadStart(ag.Attempt));
                        attemptThread.Name = "Attempting connection to registry " + hive.ToString() + " on " + machineName;
                        attemptThread.IsBackground = true;
                        attemptThread.Start();
                        attemptThread.Join(timeout);
                        RegistryKey reference = ag.ResultKey;
                        if (attemptThread.IsAlive) throw new TimeoutException();
                        if (reference == null) throw ag.Error;

                        RegistryStub newStub = new RegistryStub(_timeout, reference);
                        _store.Add(key, newStub);
                        Console.WriteLine("RemoteRegistryHandler added " + key);
                       
                        if (_store.Count == 1)
                        {
                              _cleaner = new Thread(new ThreadStart(cleaner));
                              _cleaner.Start();
                        }

                        return newStub.Reference;
                  }
            }

            public void cleaner()
            {
                  while (!_closingDown)
                  {
                        try
                        {
                              Thread.Sleep(_timeout);
                              lock (_store)
                              {
                                    List<String> keys2Del = new List<String>();
                                    foreach (KeyValuePair<String, RegistryStub> pair in _store)
                                    {
                                          if (pair.Value.TimedOut)
                                          {
                                                keys2Del.Add(pair.Key);
                                          }
                                    }
                                    foreach (String key in keys2Del)
                                    {
                                          _store.Remove(key);
                                          Console.WriteLine("RemoteRegistryHandler cleaned " + key);
                                    }
                              }
                              if (_store.Count == 0) break;
                        }
                        catch (ThreadInterruptedException e)
                        {
                              if (_closingDown)
                              {
                                    foreach (KeyValuePair<String, RegistryStub> pair in _store)
                                    {
                                          Console.WriteLine("RemoteRegistryHandler cleaned " + pair.Key);
                                    }
                                    _store.Clear();
                              }
                        }
                  }
            }

            public RemoteRegistryHandler(int timeout)
            {
                  _timeout = timeout;
            }

            public void Close()
            {
                  _closingDown = true;
                  if (_cleaner != null)
                  {
                        try
                        {
                              _cleaner.Interrupt();
                              _cleaner.Join();
                        }
                        catch (NullReferenceException e) { }
                  }
            }
      }
}
AnswerRe: What do you think of this? Multithreaded remote registry handler Pin
jackgeek11-Jun-09 12:04
jackgeek11-Jun-09 12:04 
AnswerRe: What do you think of this? Multithreaded remote registry handler Pin
harold aptroot11-Jun-09 12:09
harold aptroot11-Jun-09 12:09 
Questioncannot insert into database 2005 using visual c# 2008 Pin
Adekolurejo11-Jun-09 11:33
Adekolurejo11-Jun-09 11:33 
AnswerRe: cannot insert into database 2005 using visual c# 2008 Pin
Colin Angus Mackay11-Jun-09 12:24
Colin Angus Mackay11-Jun-09 12:24 
QuestionRe: cannot insert into database 2005 using visual c# 2008 Pin
Adekolurejo11-Jun-09 12:42
Adekolurejo11-Jun-09 12:42 
AnswerRe: cannot insert into database 2005 using visual c# 2008 Pin
Colin Angus Mackay11-Jun-09 13:08
Colin Angus Mackay11-Jun-09 13:08 
GeneralRe: cannot insert into database 2005 using visual c# 2008 Pin
Adekolurejo11-Jun-09 13:20
Adekolurejo11-Jun-09 13:20 
Questionwhat is the difference between Dictionary<> and SortedList<> Pin
Seraph_summer11-Jun-09 10:26
Seraph_summer11-Jun-09 10:26 
AnswerRe: what is the difference between Dictionary<> and SortedList<> Pin
Abhijit Jana11-Jun-09 10:33
professionalAbhijit Jana11-Jun-09 10:33 
AnswerRe: what is the difference between Dictionary<> and SortedList<> Pin
Luc Pattyn11-Jun-09 10:36
sitebuilderLuc Pattyn11-Jun-09 10:36 
GeneralRe: what is the difference between Dictionary<> and SortedList<> Pin
led mike11-Jun-09 10:43
led mike11-Jun-09 10:43 
QuestionRichTextBoxStreamType.PlainText Does not work. Pin
Baeltazor11-Jun-09 10:11
Baeltazor11-Jun-09 10:11 
AnswerRe: RichTextBoxStreamType.PlainText Does not work. Pin
Baeltazor11-Jun-09 11:02
Baeltazor11-Jun-09 11:02 
QuestionHow to disable JavaScript in webControl hosted in a WinForm Pin
kozu11-Jun-09 10:06
kozu11-Jun-09 10:06 
AnswerRe: How to disable JavaScript in webControl hosted in a WinForm Pin
kozu11-Jun-09 10:25
kozu11-Jun-09 10:25 
GeneralRe: How to disable JavaScript in webControl hosted in a WinForm Pin
kozu11-Jun-09 13:32
kozu11-Jun-09 13:32 
QuestionText formatting Pin
Skymir11-Jun-09 9:26
Skymir11-Jun-09 9:26 

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.