Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
} 

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
} 

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
} 

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
} 

But based on the comment comment from Lyle's Mug Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
} 

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
} 

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

added 694 characters in body
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
}

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
}

At a quick glance, this

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) return false;
 try
 {
 Process.GetProcessById((int) processId).Kill();
 }
 catch (ArgumentException)
 {
 return false;
 }
 catch (Win32Exception)
 {
 return false;
 }
 catch (NotSupportedException)
 {
 return false;
 }
 catch (InvalidOperationException)
 {
 return false;
 }
 return true;
} 

could be dry like so

public static bool TryKillProcessByMainWindowHwnd(int hWnd)
{
 uint processId;
 GetWindowThreadProcessId((IntPtr) hWnd, out processId);
 if (processId == 0) { return false; }
 try
 {
 Process.GetProcessById((int) processId).Kill();
 return true;
 }
 catch (ArgumentException)
 { }
 catch (Win32Exception)
 { }
 catch (NotSupportedException)
 { }
 catch (InvalidOperationException)
 { }
 return false;
}

But based on the comment from Lyle's Mug you never use the return value, so you could just change the method return type to void.


Instead of using the Dictionary.ContainsKey() method you should better use TryGetValue() like

private Worksheet GetSheet(string name)
{
 Worksheet sheet = null;
 if (_worksheets.TryGetValue(name, out sheet))
 {
 return sheet;
 }
 
 sheet = _workbook.Sheets[name];
 _worksheets.Add(name, sheet);
 return sheet;
} 

see also what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem

The same is true for the GetCell() method.

Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /