|
Your second example should look something like:
theValue = concurrentDict.GetOrAdd(theKey, key =>
{
DoSomeOtherStuff();
DoAFewMoreThings();
var theNewObject = NewUpAnObject(key);
return theNewObject;
});
The entire delegate would be executed within the scope of a lock, as with your non-generic dictionary code. However, the lock would be more granular, based on the number of buckets and the concurrency level of the collection.
If you wanted to execute the "other stuff" outside of the lock, your code would look like:
if (!concurrentDict.TryGetValue(theKey, out theValue))
{
DoSomeOtherStuff();
DoAFewMoreThings();
var theNewObject = NewUpAnObject(theKey);
theValue = concurrentDict.GetOrAdd(theKey, theNewObject);
}
In this case, DoSomeOtherStuff , DoAFewMoreThings and NewUpAnObject would run outside of any lock. However, the insert to the dictionary would not suffer from a race condition, as the class is specifically written to cope with this type of code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Tricky . I didn't know about that GetOrAdd method. Thanks.
|
|
|
|
|
There are many, many areas that could contribute. Your competitor might delegate all calls to background threads, for instance, giving the appearance that they are processing quickly. They might be using highly optimised native code behind the scenes (after all, just because you've compiled to debug, it doesn't mean your competitor has shipped you a debug version of their code, so you're comparing apples with the Mona Lisa).
|
|
|
|
|
You mean compiling it to x86 or x64 rather then Any CPU? It just seems like I've got overhead calling empty functions. I'm not sure they can kick off background threads as they need to return an object from the method.
|
|
|
|
|
No, I mean that they might be using C++ (for instance). Also, if you compile your code to release version and then compare against theirs, you should get more realistic timings. I guarantee you that their version will be the release version.
|
|
|
|
|
SledgeHammer01 wrote: Then I lock a dictionary
Remove all locking of any sort. Then test.
|
|
|
|
|
Hi.
I am trying to get "Command" values of each table with [table row(tr) and standard cell(td) which has valiagn="top"] tags defined from this website: Console Command List - Valve Developer Community[^].
So, at the end I want a whole list of "Commands" names stored in a string separated with a new line character (CR+LF).
Here I took a screenshot[^].
Anyway to get them, all command names list using C++/C#/Java/VB/VB.NET/JavaScript/AutoIt/AutoHotkey, anything/anyhow? Please, I really need this, and I will appreciate your help A LOT!
|
|
|
|
|
You can use the WebBrowser class[^] to read the information from the website and then get the actual data from the HTMLElements inside the Document[^] in the returned web page(s). There are a number of articles here on CodeProject on the subject of "screen scrapers" which may help you further.
|
|
|
|
|
Hey. I'm looking for help with my autoclicker. I got it working with buttons, but I need keybinds instead.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace auto_click
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void MouseClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
MouseClick();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F") ;
timer1.Enabled = true;
}
}
}
Any idea why it doesn't work?
REEEEEEEEEEEEH
|
|
|
|
|
first confirm whether your timer is starting or not some times timer itself does not starts and people start messing with the code inside timer tick event handler
|
|
|
|
|
First off all, you are dealing with deprecated method (mouse_event).
Second, I think you will need KeyPreview enabled/set to true for your form (look on Form1 properties).
Third, "if (e.KeyCode.ToString() == "F") ;" wtf?
That "if" branch statement does nothing, why? You assigned it to empty (;) and by not using "{" and "}" it basically mean "if" is limited to single line.. code style..
Fourth, your if statement comparison should be made from "e.KeyCode.ToString() == "F" to "e.KeyCode == Keys.F".
Fifth, are you sure your events (timer1_Tick/Form1_KeyDown) are assigned, that could be a main cause, "this code does nothing", as I don't see anything in your constructor, "InitializeComponent();", and you didn't posted code of your (*.Designer.cs) Form1.Designer.cs file in this case.
Sixth, it is a lot of bad practice in here so I will jump to seventh.
Seventh, you can't just copy/paste into C# always double-check everything such as Win32 native API you deal with, it maybe have wrong signature from my experience there are a lot of examples also 90% of all Windows native APIs from pinvoke.net are NOT correctly defined, so sad...
I have fixed your code, and it works fine for me, all you need to do is keep up with my friendly warnings.
If you are not that smart, well, here is my smart suggestion for you, why not do:
"timer1.Enabled = Keys.F;" in Form1_KeyDown event (Keys.F will be 1/true when hold down; otherwise 0/false).
And yeah, do not forget mouse_event() is deprecated method and is no longer officially supported (so I should not be even giving you away these nice warnings).
Switch to SendInput function with structures, it is not that hard.
Best regards, OmegaExtern.
modified 17-Aug-14 2:55am.
|
|
|
|
|
Thanks a lot. As you can tell, I know almost nothing. I'm just trying to pick up on the basics by trying to make things.
|
|
|
|
|
Re-Edited my post, refresh your page.
I glad I can help you
|
|
|
|
|
i have a function which returns a string array. the array may or may not have value.
how to check if the array has any value. In the below code how to check in if codition.
i want to keep the looping option as last. I would like to check in 1 line statement.
I tried indexof, contain and exits option but all of them are for checking a specific value in array and not to check the whole array if it has any value presence.
below is the sample code.
main()
{
string[]victory=war();
if(victory as any value except null) then
{
}
else
{
}
}
public string[] war()
{
step 1.....
return some string array
}
|
|
|
|
|
|
|
Try:
if (victory != null && victory.Length > 0)
{
...
}
You looking for sympathy?
You'll find it in the dictionary, between sympathomimetic and sympatric
(Page 1788, if it helps)
|
|
|
|
|
I have implemented a backgroundworker which simulates a turn in a game I have created:
try
{
GameObjectHelpers.SimulateTurn();
}
catch (Exception ex)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(AppSettings.getCommonApplicationDataPath() + "\\log.log");
file.WriteLine("{0}: {1} {2}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), ex.StackTrace);
file.WriteLine(ex.Message);
file.Close();
}
My problem is that the message from the exception is not very useable:
8/15/2014: 5:25 PM at Model.GeneralModel.Helpers.GameObjectHe lpers.DoDailyUpdate()
at Model.GeneralModel.Helpers.GameObjectHe lpers.SimulateTurn()
at Model.GeneralModel.Helpers.WorkersModel .GameObjectWorker.bw_DoWork(Object sender, DoWorkEventArgs e)
Is there a better way of catching and logging the exception?
|
|
|
|
|
What is the message from the exception?
|
|
|
|
|
It is just:
at Model.GeneralModel.Helpers.GameObjectHe lpers.DoDailyUpdate()
at Model.GeneralModel.Helpers.GameObjectHe lpers.SimulateTurn()
at Model.GeneralModel.Helpers.WorkersModel .GameObjectWorker.bw_DoWork(Object sender, DoWorkEventArgs e)
|
|
|
|
|
That's the stack trace not the message. Unless you know what exception has been caught the stack trace is not much use.
|
|
|
|
|
Yes, by logging all the information of the exception, not just part of it. Simlpy use "Exception.ToString()" and log those results. It will include the message (if any), a stacktrace (that tells you where the exception came from), and the message type, which is now missing.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You know there are logging libraries that already handle all of what you are attempting to do within a catch block?
And never attempt to handle an exception in a catch/finally block with code that can also throw an exception. (Files can throw exceptions.)
|
|
|
|
|
I didn't knew that but I will try to look into it
|
|
|
|
|
Hi,
Kindly note that, while capturing network incoming tcp packets using following codes/methods, missing some packets. It is sure that all packets all coming on my network perfectly fine but unfortunately I am missing some packets. Please let me know, what and where, I am doing mistake in my following mentioned codes.
Thank you & Regards
(Riaz)
public void Capture()
{
_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
_mainSocket.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.10"), 0));
var byTrue = new byte[] { 1, 0, 0, 0 };
var byOut = new byte[] { 1, 0, 0, 0 };
_mainSocket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
_mainSocket.EnableBroadcast = true;
_mainSocket.BeginReceive(_data, 0, _data.Length, SocketFlags.None, OnReceive, null);
}
private void Parse(byte[] data, int size)
{
ipH = new IPHeader(data, size);
if (ipH.ProtocolType == Protocol.TCP)
{
FilterData(data, size);
}
System.Threading.Thread.Sleep(5);
}
private void FilterData(byte[] data, int size)
{
}
|
|
|
|
|